<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>Uncategorized Archives - Coding Tutorial</title>
	<atom:link href="https://codingizfun.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>https://codingizfun.com/category/uncategorized/</link>
	<description>Software Architecture and javascript concepts explained in detail with examples</description>
	<lastBuildDate>Fri, 23 Feb 2024 22:20:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.1</generator>
<site xmlns="com-wordpress:feed-additions:1">214557465</site>	<item>
		<title>Angular Material Virtual scrolling</title>
		<link>https://codingizfun.com/angular-material-virtual-scrolling/</link>
					<comments>https://codingizfun.com/angular-material-virtual-scrolling/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 23 Feb 2024 22:15:19 +0000</pubDate>
				<category><![CDATA[Angular and Javascript Interview questions]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">https://codingizfun.com/?p=142</guid>

					<description><![CDATA[<p>In web applications, virtual scrolling is used to render and display large lists or grids efficienty. Angular Material, a popular UI component library for Angular, provides built-in support for virtual scrolling. Let&#8217;s understand virtual scrolling, it&#8217;s benefits, and how to implement it using Angular Material. What is Virtual Scrolling? Virtual scrolling renders only the visible...</p>
<p>The post <a href="https://codingizfun.com/angular-material-virtual-scrolling/">Angular Material Virtual scrolling</a> appeared first on <a href="https://codingizfun.com">Coding Tutorial</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="">In web applications, virtual scrolling is used to render and display large lists or grids efficienty. Angular Material, a popular UI component library for Angular, provides built-in support for virtual scrolling. Let&#8217;s understand virtual scrolling, it&#8217;s benefits, and how to implement it using Angular Material.</p>



<h3 class="wp-block-heading">What is Virtual Scrolling?</h3>



<p class="">Virtual scrolling renders only the visible items in a list or grid, instead of loading the entire dataset at once. It dynamically creates and removes elements from the DOM as the user scrolls through the list. This approach is useful when dealing with huge data because it significantly improves performance and reduces memory consumption.</p>



<h3 class="wp-block-heading">Benefits of Virtual Scrolling:</h3>



<ol class="wp-block-list">
<li class=""><strong>Improved Performance</strong>: Virtual scrolling dramatically enhances the performance of web applications by rendering only the visible items. This results in faster initial loading times and smoother scrolling.</li>



<li class=""><strong>Reduced Memory Usage</strong>: Since only a subset of items is in the DOM at any given time, memory usage reduces significantly, making it feasible to work with large datasets without causing memory-related issues.</li>



<li class=""><strong>Consistent User Experience</strong>: Virtual scrolling provides a consistent user experience, regardless of the data size, as the scrolling remains smooth and responsive.</li>
</ol>



<h3 class="wp-block-heading">Implementing Virtual Scrolling in Angular Material:</h3>



<p class="">Angular Material provides a <strong>CdkVirtualScrollViewport </strong>directive to implement virtual scrolling.&nbsp;</p>



<p class="">Below are the steps for the same</p>



<ul class="wp-block-list">
<li class=""><strong>Install Angular Material using the below command</strong></li>
</ul>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<pre class="wp-block-code"><code>      <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">  ng add @angular/material</mark></code></pre>



<ul class="wp-block-list">
<li class=""><strong>In your Angular module import the ScrollingModule:</strong></li>
</ul>
</div></div>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<pre class="wp-block-code"><code>    <mark style="background-color:#ffffff" class="has-inline-color has-vivid-red-color">import { ScrollingModule } from '@angular/cdk/scrolling';
    @NgModule({
       imports: &#91;ScrollingModule],
    })</mark></code></pre>
</div></div>
</div></div>
</div></div>



<ul class="wp-block-list">
<li class=""><strong>Create the Virtual Scroll Container:</strong></li>
</ul>



<p class="">      In your component&#8217;s template, create a container element (e.g., &lt;cdk-virtual-scroll-viewport&gt;) to host       the virtual scroll:</p>



<pre class="wp-block-code"><code>
<mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">&lt;cdk-virtual-scroll-viewport itemSize="50" minBufferPx="200" maxBufferPx="400"
                             class="example-viewport"&gt;
  &lt;div *cdkVirtualFor="let item of items" class="example-item"&gt;{{item}}&lt;/div&gt;
&lt;/cdk-virtual-scroll-viewport&gt;--&gt;</mark></code></pre>



<ul class="wp-block-list">
<li class=""><em>itemSize<strong>:</strong> This attribute specifies the size of each item in pixels.</em></li>
</ul>



<ul class="wp-block-list">
<li class=""><strong>Provide Data:</strong></li>
</ul>



<p class="">Just pass the data and Angular Material will handle the rest. As we scroll through the list, virtual scroll will efficiently create and delete elements as needed, providing a seamless user experience.</p>



<p class="">Now let&#8217;s compare the performance with and without virtual scrolling</p>



<p class="">Considering the example of displaying a list of 20,000 strings.</p>



<ul class="wp-block-list">
<li class=""><strong>With initial scrolling</strong></li>
</ul>



<p class=""><strong>Code</strong></p>



<pre class="wp-block-code"><code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">&lt;h4>Items list with Virtual scrolling enabled&lt;/h4>
&lt;cdk-virtual-scroll-viewport itemSize="50" minBufferPx="200" maxBufferPx="400"
                             class="example-viewport">
  &lt;div *cdkVirtualFor="let item of items" class="example-item">{{item}}&lt;/div>
&lt;/cdk-virtual-scroll-viewport></mark>      </code></pre>
</div></div>



<p class=""><strong>Performance:</strong></p>



<figure class="wp-block-image size-full"><img decoding="async" width="640" height="27" src="https://i0.wp.com/codingizfun.com/wp-content/uploads/2024/02/image-1.png?resize=640%2C27&#038;ssl=1" alt="" class="wp-image-147" srcset="https://i0.wp.com/codingizfun.com/wp-content/uploads/2024/02/image-1.png?w=984&amp;ssl=1 984w, https://i0.wp.com/codingizfun.com/wp-content/uploads/2024/02/image-1.png?resize=300%2C13&amp;ssl=1 300w, https://i0.wp.com/codingizfun.com/wp-content/uploads/2024/02/image-1.png?resize=768%2C33&amp;ssl=1 768w, https://i0.wp.com/codingizfun.com/wp-content/uploads/2024/02/image-1.png?resize=850%2C36&amp;ssl=1 850w" sizes="(max-width: 640px) 100vw, 640px" data-recalc-dims="1" /></figure>



<ul class="wp-block-list">
<li class=""><strong>Displaying plain list without virtual scrolling</strong></li>
</ul>



<p class=""><strong>Code</strong></p>



<pre class="wp-block-code"><code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">&lt;div>
  &lt;h2>List of Items&lt;/h2>
  &lt;ul>
    &lt;li *ngFor="let item of items">{{ item }}&lt;/li>
  &lt;/ul>
&lt;/div></mark>
</code></pre>



<p class=""><strong>Performance:</strong></p>



<figure class="wp-block-image size-full"><img decoding="async" width="640" height="23" src="https://i0.wp.com/codingizfun.com/wp-content/uploads/2024/02/image-2.png?resize=640%2C23&#038;ssl=1" alt="Angular virtual scroll" class="wp-image-148" srcset="https://i0.wp.com/codingizfun.com/wp-content/uploads/2024/02/image-2.png?w=985&amp;ssl=1 985w, https://i0.wp.com/codingizfun.com/wp-content/uploads/2024/02/image-2.png?resize=300%2C11&amp;ssl=1 300w, https://i0.wp.com/codingizfun.com/wp-content/uploads/2024/02/image-2.png?resize=768%2C28&amp;ssl=1 768w, https://i0.wp.com/codingizfun.com/wp-content/uploads/2024/02/image-2.png?resize=850%2C31&amp;ssl=1 850w" sizes="(max-width: 640px) 100vw, 640px" data-recalc-dims="1" /></figure>



<p class=""><strong>References</strong><br><a href="https://material.angular.io/cdk/scrolling/overview">https://material.angular.io/cdk/scrolling/overview</a></p>



<p class="">If you like this post and are interested to learn more about MInimal API, please go through below links for other related posts</p>



<p class=""><a href="https://codingizfun.com/">https://codingizfun.com/</a></p>
<p>The post <a href="https://codingizfun.com/angular-material-virtual-scrolling/">Angular Material Virtual scrolling</a> appeared first on <a href="https://codingizfun.com">Coding Tutorial</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codingizfun.com/angular-material-virtual-scrolling/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">142</post-id>	</item>
	</channel>
</rss>
