<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Udit Gogoi on Medium]]></title>
        <description><![CDATA[Stories by Udit Gogoi on Medium]]></description>
        <link>https://medium.com/@uditgogoi1?source=rss-51f12811c1d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*T7NyWVaV2l87LgLh</url>
            <title>Stories by Udit Gogoi on Medium</title>
            <link>https://medium.com/@uditgogoi1?source=rss-51f12811c1d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 07:09:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@uditgogoi1/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Search by filter with autocomplete using Angular and Angular material]]></title>
            <link>https://medium.com/@uditgogoi1/search-bar-with-autocomplete-using-angular-5-and-angular-material-c5a77a429da7?source=rss-51f12811c1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5a77a429da7</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular-material]]></category>
            <category><![CDATA[angular-5]]></category>
            <dc:creator><![CDATA[Udit Gogoi]]></dc:creator>
            <pubDate>Sat, 15 Sep 2018 21:01:28 GMT</pubDate>
            <atom:updated>2019-05-08T05:47:14.745Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*t1RhLp4tU2M6bS-HS05jdQ.jpeg" /></figure><p>While trying to build an Angular project, I have search a lot for any possible tutorial to build a search bar having auto-suggest using angular material, but haven’t found any. So I came up with a solution of my own. Its one of the many ways to build a search bar in angular. The reason behind using angular material is, it makes our life lot easier while tinkering with Observable and design elements. Here I am using Angular 5, but I believe any version of Angular would work. Also we need Angular material, a fake REST API from <a href="https://jsonplaceholder.typicode.com/posts">https://jsonplaceholder.typicode.com/posts</a> .</p><blockquote><em>The GitHub code is available </em><a href="https://github.com/uditgogoi/search-bar-demo/"><em>here</em></a><em>.</em></blockquote><blockquote><em>Stackblitz code is available </em><a href="https://stackblitz.com/edit/angular-jbgf8a?file=src%2Fapp%2Fapp.module.ts"><em>here</em></a><em>.</em></blockquote><h3>Step 1. Lets get started</h3><p>Well, I am assuming, if you have reached here, you might as well be comfortable with getting started with <a href="https://github.com/angular/angular-cli">Angular CLI</a> and <a href="https://nodejs.org/en/download/">npm</a>. If not, I would suggest you to go through this <a href="https://angular.io/guide/quickstart">link</a>, for a detail view.</p><p>Considering, you have your angular CLI and npm installed, browse through the desired folder location where you would like to keep your project. Once inside the location, lets create an angular project and install angular material using following set of instruction</p><pre>&gt; cd Desktop/angular-projects/<br>&gt; ng new search-bar<br>&gt; npm install --save @angular/material @angular/cdk @angular/animations</pre><p>We will be needing two components in this project. One is to show all the data, other is the search bar component. The reason behind keeping search bar in different component is to make it a reusable component. First browse inside our project and lets create our components :</p><pre>&gt; cd search-bar<br>&gt; ng g c component/home-page --spec=false<br>&gt; ng g c component/search-bar --spec=false</pre><p>If you want to generate the spec files , you can remove the command “— spec=false” from the above command.</p><p>we will also be needing a service to share data between components. Lets generate a service file.</p><pre>&gt; ng g s data --spec=false</pre><p>The file name with the above command will come up as <strong>data.service.ts</strong> . Import the service file in app.module.ts file and add it to providers :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b4226158eb7e2ce98a5da0437d701fc/href">https://medium.com/media/3b4226158eb7e2ce98a5da0437d701fc/href</a></iframe><p>Lastly open style.css file and import an angular material <a href="https://material.angular.io/guide/theming">theme</a> along with material icons :</p><pre>@import &#39;~@angular/material/prebuilt-themes/deeppurple-amber.css&#39;;</pre><pre>@import url(https://medium.com/&#39;https://fonts.googleapis.com/css?family=Roboto:400,700|Material+Icons&#39;);</pre><h3>Step 2. Import all the modules</h3><p>Since we have created our project and all its components, its time to import all the installed component modules in our <strong>app.module.ts</strong> file. The components we have generated using angular CLI (home-page, search-bar) will be automatically imported to app.module.ts file. But we need to import angular material components. Since we are using only <a href="https://angular.io/guide/animations">BrowserAnimationsModule</a>, <a href="https://material.angular.io/components/autocomplete/overview">MatAutocompleteModule</a><a href="https://material.angular.io/components/chips/overview">, MatChipsModule</a>, <a href="https://material.angular.io/components/form-field/api">MatFormFieldModule</a>, <a href="https://material.angular.io/components/icon/overview">MatIconModule</a> and <a href="https://material.angular.io/components/input/api">MatInputModule</a>.</p><p>lets import these components:</p><p><strong>App.module.ts</strong> till this point :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b10c573c322b18250c67b89e78b6ce7a/href">https://medium.com/media/b10c573c322b18250c67b89e78b6ce7a/href</a></iframe><h3>Step 3. Get the data from API</h3><p>Since we will be using a fake REST API, its time to bring the data to our application. We will be using a service to get our desired data. In this case, it will be handled by the <strong>data.service.ts</strong> file that we have generated above. Before getting the data from the API, let’s first declare an <a href="https://www.typescriptlang.org/docs/handbook/interfaces.html">interface</a> corresponding to the data that we will be using. Create a new file called <strong>post.ts:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad9ab208199403d8434784cd1c743e6c/href">https://medium.com/media/ad9ab208199403d8434784cd1c743e6c/href</a></iframe><p>Now we are ready to get the data. The service file <strong>data.service.ts</strong> will look like this at this point:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c2a03a1ee1f336ad1aa482eefe496c3/href">https://medium.com/media/5c2a03a1ee1f336ad1aa482eefe496c3/href</a></iframe><p>Remember to include HttpClientModule in <strong>app.module.ts</strong></p><pre>import { HttpClientModule } from &#39;@angular/common/http&#39;;</pre><pre>imports: [HttpClientModule]</pre><p>Data is now ready to be viewed in the app.</p><h3>Step 4. Build the app view and search bar</h3><p>We have to display our data and home page is going to display the data as well as hold the search bar component. I have kept the CSS very basic.</p><p><strong>home-page.component.html</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e49c7daefe8c7abe851d2a1a0f0ca69/href">https://medium.com/media/5e49c7daefe8c7abe851d2a1a0f0ca69/href</a></iframe><p><strong>home-page.component.ts</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e3b541a9ea882ae1bac4d0aa3976e6b/href">https://medium.com/media/4e3b541a9ea882ae1bac4d0aa3976e6b/href</a></iframe><p>There might off course be better ways to get the data on load, but lets keep it beyond the scope of this article. Now, lets design the search bar. <strong>Note: </strong>whenever we select an option from auto-suggest list, we will be showing that option inside a chip. That’s the reason of using <strong>mat-chip</strong>. The HTML for search bar ( search-bar.component.html) will be something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54b8aecd1d27efc58d0a9feec6220df9/href">https://medium.com/media/54b8aecd1d27efc58d0a9feec6220df9/href</a></iframe><p>Now lets edit the <strong>search-bar.component.ts</strong> file where most of the autocomplete logic will go.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3ded3d95d121a0fc468e4ca36d61568/href">https://medium.com/media/c3ded3d95d121a0fc468e4ca36d61568/href</a></iframe><p>Till here, we have completed our auto-suggest feature of our application. On selecting an option <strong>filterPostList()</strong> function will run and the option will be pushed to an array. On removing the option from input, <strong>removeOption()</strong> will trigger and will remove that particular option from the array. It will look something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QMIkx5lt1OGJXMzLzKZyuw.png" /></figure><p>Important thing to note is whatever option we are selecting from the auto-suggest, we are pushing that value inside an array called <strong>dataService.searchOption </strong>as seen inside the function <strong>filterPostList(). </strong>It will help us to filter the post list in correspondence with the selected options. The reason behind keeping the array in service is that we might need it in different component.</p><h3><strong>Step 5. Trigger the search</strong></h3><p>Now we have a decision to make in front of us. When user selects any option from auto-suggest list how do we trigger the search event ? One option is to give a button. In that case user will select an option and click the button to create an event that will filter his search. But in this example , we will take a different approach to this problem. We will use Angular <a href="https://angular.io/api/core/EventEmitter">EventEmitter</a>.</p><p>Now we will import <strong>Output</strong> and <strong>EventEmitter</strong> inside <strong>search-bar.component.ts</strong></p><pre>import { EventEmitter, Output } from &#39;@angular/core&#39;;</pre><pre>export class SearchBarComponent implements OnInit {</pre><pre>           @Output() onSelectedOption = new EventEmitter();</pre><pre>}</pre><p>The idea behind this is whenever user select any option from the auto-suggest list, we will emit the array that is holding the option. In this case <strong>this.dataService.searchOption </strong>is holding the selected options and <strong>this.onSelectedOption </strong>is emitting it as:</p><pre>this.onSelectedOption.emit(this.dataService.searchOption)</pre><p>Add <strong>onSelectedOption </strong>as input<strong> </strong>to the home-page.component.html as:</p><pre>&lt;app-search-bar (onSelectedOption)=&quot;onSelectedFilter($event)&quot;&gt;&lt;/app-search-bar&gt;</pre><p>Now lets define the function <strong>onSelectedFilter() </strong>in home-page.component.ts. We need to change the array which is responsible for showing the data in home component as soon as any option is selected in search bar component. The home-page.component.ts file will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/289fb4156c224f817de0bbb33a80d7e9/href">https://medium.com/media/289fb4156c224f817de0bbb33a80d7e9/href</a></iframe><p>According to above code, if we select any option from auto-suggest, array <strong>dataService.searchOption</strong> will replace the array <strong>post </strong>and in the view we will see the results matching the selected option.</p><p>Now , Its time for the final step and that is to write the search logic.</p><h3>Step 6. Search logic</h3><p>For the sake of simplicity we will search all the posts based on title. Since we will be calling search logic from different components , its necessary to write the logic in a service<strong>, </strong>In this case<strong>, data.service.ts</strong> file so that we can inject in any component. The function <strong>filteredListOptions() </strong>will hold up the search logic and return an array <strong>filteredPostsList </strong>which has the filtered result we need. The file will now look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d2745bb80e75d2eb1ade66c1fcfe8b2/href">https://medium.com/media/2d2745bb80e75d2eb1ade66c1fcfe8b2/href</a></iframe><h3>Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*swxrQ26K7I_HhJCpXsUkpw.png" /><figcaption>Final view of the app after selecting an option</figcaption></figure><p>Now we have completed our auto complete filter project. Here is the <a href="https://github.com/uditgogoi/search-bar-demo">link</a> to the code in GitHub. Its my first ever article online. I would appreciate any feedback to improve further.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5a77a429da7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>