<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Haidar Zeineddine </title>
    <description>The latest articles on DEV Community by Haidar Zeineddine  (@haidarz).</description>
    <link>https://dev.to/haidarz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F94388%2F18da5de3-6c4e-4a4a-91f1-4c6e2d902879.jpeg</url>
      <title>DEV Community: Haidar Zeineddine </title>
      <link>https://dev.to/haidarz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/haidarz"/>
    <language>en</language>
    <item>
      <title>Adding Infinite Scroll Functionality to Angular Material Select Component
</title>
      <dc:creator>Haidar Zeineddine </dc:creator>
      <pubDate>Wed, 18 Dec 2019 15:48:39 +0000</pubDate>
      <link>https://dev.to/haidarz/adding-infinite-scroll-functionality-to-angular-material-select-component-1lho</link>
      <guid>https://dev.to/haidarz/adding-infinite-scroll-functionality-to-angular-material-select-component-1lho</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;mat-select&amp;gt;&lt;/code&gt; is a form control for selecting a value from a set of options, similar to the native  element. You can read more about selects in the Material Design spec. It is designed to work inside of a  element.&lt;br&gt;
——&lt;a href="https://material.angular.io/components/select/overview"&gt;Angular Material Docs&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;MatSelect API is robust having vast capabilities, but there's an issue here.&lt;br&gt;
What if my DATASET is very large, should I load it all into my &lt;code&gt;mat-select&lt;/code&gt; component.&lt;br&gt;
Sure not , it'll slow down my app! &lt;br&gt;
My data should be lazy loaded i.e. I load part of my data and do load more when I only need it. In case of &lt;code&gt;select&lt;/code&gt; controls, things must be done as of infinite scroll manner , where you load extra content when you reach the bottom of the options scroll panel.&lt;/p&gt;

&lt;p&gt;That's why I've created the angular library &lt;a href="https://github.com/HaidarZ/ng-mat-select-infinite-scroll"&gt;ng-mat-select-infinite-scroll&lt;/a&gt; to handle big dataset issues with angular material select.&lt;/p&gt;

&lt;p&gt;If you're familiar with angular , this library is easy to use , it represents an angular directive with some input properties.&lt;/p&gt;
&lt;h3&gt;
  
  
  Getting Started
&lt;/h3&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i ng-mat-select-infinite-scroll &lt;span class="nt"&gt;--save&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Import &lt;code&gt;MatSelectInfiniteScrollModule&lt;/code&gt; inside the app.module.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MatFormFieldModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;MatSelectModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/material/select&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;MatSelectInfiniteScrollModule&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ng-mat-select-infinite-scroll&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;declarations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;AppComponent&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;BrowserModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;BrowserAnimationsModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;MatFormFieldModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;MatSelectModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;MatSelectInfiniteScrollModule&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="na"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;AppComponent&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then place the &lt;code&gt;msInfiniteScroll&lt;/code&gt; directive on the &lt;code&gt;mat-select&lt;/code&gt; component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;mat-form-field&lt;/span&gt; &lt;span class="na"&gt;appearance=&lt;/span&gt;&lt;span class="s"&gt;"outline"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;mat-label&amp;gt;&lt;/span&gt;Select&lt;span class="nt"&gt;&amp;lt;/mat-label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;mat-select&lt;/span&gt; &lt;span class="na"&gt;msInfiniteScroll&lt;/span&gt; &lt;span class="na"&gt;(infiniteScroll)=&lt;/span&gt;&lt;span class="s"&gt;"getNextBatch()"&lt;/span&gt; &lt;span class="na"&gt;[complete]=&lt;/span&gt;&lt;span class="s"&gt;"offset === data.length"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;mat-option&lt;/span&gt; &lt;span class="na"&gt;*ngFor=&lt;/span&gt;&lt;span class="s"&gt;"let option of options$ | async"&lt;/span&gt; &lt;span class="na"&gt;[value]=&lt;/span&gt;&lt;span class="s"&gt;"option"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;{{option}}&lt;span class="nt"&gt;&amp;lt;/mat-option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/mat-select&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/mat-form-field&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above example , whenever we reach the bottom of the select panel , &lt;code&gt;infiniteScroll&lt;/code&gt; event will be called and it will still doing that until&lt;br&gt;
the &lt;code&gt;complete&lt;/code&gt; input is set to &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Suppose that you have a paginated API, you'll keep fetching until the count of options in the select is equal the total number of the resource.&lt;/p&gt;

&lt;h4&gt;
  
  
  Parameters
&lt;/h4&gt;

&lt;h3&gt;
  
  
  Inputs
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Property&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Default&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;complete&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;If &lt;code&gt;true&lt;/code&gt;, the &lt;code&gt;infinitScroll&lt;/code&gt; output will no longer be triggered&lt;/td&gt;
&lt;td&gt;&lt;code&gt;boolean&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;threshold&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The threshold distance from the bottom of the options list to call the &lt;code&gt;infiniteScroll&lt;/code&gt; output event when scrolled. The threshold value can be either in percent, or in pixels. For example, use the value of &lt;code&gt;10%&lt;/code&gt; for the &lt;code&gt;infiniteScroll&lt;/code&gt; output event to get called when the user has needs 10% to reach the bottom.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;string&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'15%'&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;debounceTime&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The threshold time before firing the &lt;code&gt;infiniteScroll&lt;/code&gt; event&lt;/td&gt;
&lt;td&gt;&lt;code&gt;number&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;150&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Outputs
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Property&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;infinitScroll&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Emitted when the scroller inside the &lt;code&gt;mat-select&lt;/code&gt; reaches the required distance&lt;/td&gt;
&lt;td&gt;&lt;code&gt;EventEmitter&amp;lt;void&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For a full example , please have a look at &lt;a href="https://stackblitz.com/edit/ng-mat-select-infinite-scroll"&gt;StackBlitz working example&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Don't forget to keep a star for my repository if you liked it :)&lt;br&gt;
&lt;a href="https://github.com/HaidarZ/ng-mat-select-infinite-scroll"&gt;ng-mat-select-infinite-scroll&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>matselect</category>
      <category>infinitescroll</category>
      <category>material</category>
    </item>
  </channel>
</rss>
