SlideShare a Scribd company logo
1 of 7
Download to read offline
Learn How to Implement Typeahead 
with ng-grid 
A few weeks ago we posted a blog series entitled ng-grid and 
REST API. In the first blog you were given step-by-step instruction 
on how to build ng-grid with a Simple REST. In the second blog 
you learned how to use a Real REST when creating ng-grid. In 
this blog post, we will continue the series by showing you how 
to implement Typeahead by using the Real REST API. 
Objective 
This tutorial begins by showing you how to get a list of Customers using 
Backand’s REST API while populating ng-grid. Next you will filter the list of 
Customers based on Title. And finally, as you type in the filter box you will 
use Typeahead to pre-populate the list with Titles by getting autocomplete 
data from Backand’s REST API. The end result will look something like this: 
You can also see it up and 
running here in Plunker before 
going through the tutorial. 
www.backand.com
Assumptions 
1. You have gone through the ng-grid blog post which explains how to set 
up ng-grid with Backand’s REST service. 
2. You will use Backand’s REST service. For production projects please 
register at http://www.backand.com/ and follow the steps in connecting 
your database. 
Typeahead 
Typeahead is a feature where in you get a pre-populated list of items as soon 
as you enter data into a text box – similar to Google’s search box. For this 
tutorial post you will use Angular-ui’s Typeahead directive. 
The first step you need to do is include Angular-ui’s JS library which has the 
Typeahead directive. 
Index.html: 
You also need to add ‘ui-bootstrap’ Angular module dependency to our main 
module. 
script.js: 
Backand autocomplete API 
Backand has an autocomplete API where you pass a query term along with 
the intended object. This API will then return an array of results based on 
the search query. The autocomplete API looks like this when you query for a 
Job_Title on Customers: 
www.backand.com
Let’s start by writing actual code on top of your ng-grid (see this previous 
blog post to get your started). 
In the previous blog, you get Employee details from Backand’s REST service. 
In this example, you will now get Customer details. Also some of ng-grid’s 
features can be cut down for Typeahead as you don’t need the create/update/ 
delete functionality. Let’s strip down ng-grid as follows: 
Now, let’s change the service code to look as follows: 
www.backand.com
www.backand.com
You need to update the controller code to authenticate the user and get 
customer details as soon as the controller is instantiated. Let’s add the 
following code to controller to authenticate the user and fill up the grid with 
customer data. 
The above lines of code make sure that as soon as this controller is instantiated, 
the user is authenticated; customer details are obtained. 
Now let’s add an <input /> tag that can act as the Typeahead to show the 
matching customer titles on text enter. 
The Typeahead directive looks for a value similar to the ng-repeat directive. 
As soon as this controller is instantiated and there is a value on the input 
tag, getCustomerTitles() function will be called with the current value in the 
input tag. And your controller code makes an asynchronous call to Backand’s 
autocomplete API using the backandService to get the autocomplete data. 
And Typeahead allows you to pass a function name that will be called 
when the user selects an item inside the Typeahead dropdown. This is the 
typeahead-on-select attribute. Let’s create a method in your controller to 
hook the text from the input tag and filter the ng-grid so that only Customers 
with matching title are shown in the grid. 
www.backand.com
We already added a filterOptions property to our gridOptions while setting 
up ng-grid. So as soon as the filterOptions object is updated, ng-grid is also 
updated to show filtered data. 
Your Typeahead filter and ng-grid are working perfectly right now. But if you 
look deep there is a problem. If the user clears the input filter, ng-grid doesn’t 
update. This is because you are only updating the grid when the user selects 
a dropdown value from Typeahead. Now you need to be able to give an option 
to clear the filter applied on the grid. Let’s add a <button /> that says “clear” 
which will clear the filterOptions when the user clicks it. 
Add a “clearFilter()” method in the controller to do the necessary actions on 
the filterOptions. 
Now it’s all wired up nice and good. When the user enters something into 
the input filter box, a list of matches will be shown by getting the data from 
Backand’s autocomplete API using the Typeahead directive. When an option 
is selected from the list given by Typeahead, you apply the filter on the list of 
customers to show only the customers with matching title. And you can also 
remove the filtered information from the grid when pressing the clear button. 
There you go. Typeahead with ng-grid using Backand’s REST service. 
www.backand.com
References 
ng-grid Documentation: http://angular-ui.github.io/ng-grid/ 
Typeahead: http://angular-ui.github.io/bootstrap/#/typeahead 
Plunker Code: http://plnkr.co/edit/uMXjvK?p=preview 
Build your Bootstrap + Angular app with Backand today. – Get started now. 
www.backand.com 
Contact Information 
Backand Inc. 
info@backand.com 
www.backand.com

More Related Content

More from Backand Cohen

Five Common Angular Mistakes
Five Common Angular MistakesFive Common Angular Mistakes
Five Common Angular MistakesBackand Cohen
 
How to Build Dynamic Forms in Angular Directive with a Backend
How to Build Dynamic Forms in Angular Directive with a BackendHow to Build Dynamic Forms in Angular Directive with a Backend
How to Build Dynamic Forms in Angular Directive with a BackendBackand Cohen
 
REST Easy with AngularJS - ng-grid CRUD Example
REST Easy with AngularJS - ng-grid CRUD ExampleREST Easy with AngularJS - ng-grid CRUD Example
REST Easy with AngularJS - ng-grid CRUD ExampleBackand Cohen
 
Testing Two-Way Data Binding in AngularJS
Testing Two-Way Data Binding in AngularJSTesting Two-Way Data Binding in AngularJS
Testing Two-Way Data Binding in AngularJSBackand Cohen
 
Backand Presentation
Backand PresentationBackand Presentation
Backand PresentationBackand Cohen
 
AngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro FrameworkAngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro FrameworkBackand Cohen
 
How-to Increase User Engagement by 25% with User Segmented Push Notifications
How-to Increase User Engagement by 25% with User Segmented Push NotificationsHow-to Increase User Engagement by 25% with User Segmented Push Notifications
How-to Increase User Engagement by 25% with User Segmented Push NotificationsBackand Cohen
 
The Enterprise Excel Hell
The Enterprise Excel HellThe Enterprise Excel Hell
The Enterprise Excel HellBackand Cohen
 

More from Backand Cohen (8)

Five Common Angular Mistakes
Five Common Angular MistakesFive Common Angular Mistakes
Five Common Angular Mistakes
 
How to Build Dynamic Forms in Angular Directive with a Backend
How to Build Dynamic Forms in Angular Directive with a BackendHow to Build Dynamic Forms in Angular Directive with a Backend
How to Build Dynamic Forms in Angular Directive with a Backend
 
REST Easy with AngularJS - ng-grid CRUD Example
REST Easy with AngularJS - ng-grid CRUD ExampleREST Easy with AngularJS - ng-grid CRUD Example
REST Easy with AngularJS - ng-grid CRUD Example
 
Testing Two-Way Data Binding in AngularJS
Testing Two-Way Data Binding in AngularJSTesting Two-Way Data Binding in AngularJS
Testing Two-Way Data Binding in AngularJS
 
Backand Presentation
Backand PresentationBackand Presentation
Backand Presentation
 
AngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro FrameworkAngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro Framework
 
How-to Increase User Engagement by 25% with User Segmented Push Notifications
How-to Increase User Engagement by 25% with User Segmented Push NotificationsHow-to Increase User Engagement by 25% with User Segmented Push Notifications
How-to Increase User Engagement by 25% with User Segmented Push Notifications
 
The Enterprise Excel Hell
The Enterprise Excel HellThe Enterprise Excel Hell
The Enterprise Excel Hell
 

Recently uploaded

Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Mark Simos
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Jeffrey Haguewood
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...BookNet Canada
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...Karmanjay Verma
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 

Learn How to Implement Typeahead with ng-grid

  • 1. Learn How to Implement Typeahead with ng-grid A few weeks ago we posted a blog series entitled ng-grid and REST API. In the first blog you were given step-by-step instruction on how to build ng-grid with a Simple REST. In the second blog you learned how to use a Real REST when creating ng-grid. In this blog post, we will continue the series by showing you how to implement Typeahead by using the Real REST API. Objective This tutorial begins by showing you how to get a list of Customers using Backand’s REST API while populating ng-grid. Next you will filter the list of Customers based on Title. And finally, as you type in the filter box you will use Typeahead to pre-populate the list with Titles by getting autocomplete data from Backand’s REST API. The end result will look something like this: You can also see it up and running here in Plunker before going through the tutorial. www.backand.com
  • 2. Assumptions 1. You have gone through the ng-grid blog post which explains how to set up ng-grid with Backand’s REST service. 2. You will use Backand’s REST service. For production projects please register at http://www.backand.com/ and follow the steps in connecting your database. Typeahead Typeahead is a feature where in you get a pre-populated list of items as soon as you enter data into a text box – similar to Google’s search box. For this tutorial post you will use Angular-ui’s Typeahead directive. The first step you need to do is include Angular-ui’s JS library which has the Typeahead directive. Index.html: You also need to add ‘ui-bootstrap’ Angular module dependency to our main module. script.js: Backand autocomplete API Backand has an autocomplete API where you pass a query term along with the intended object. This API will then return an array of results based on the search query. The autocomplete API looks like this when you query for a Job_Title on Customers: www.backand.com
  • 3. Let’s start by writing actual code on top of your ng-grid (see this previous blog post to get your started). In the previous blog, you get Employee details from Backand’s REST service. In this example, you will now get Customer details. Also some of ng-grid’s features can be cut down for Typeahead as you don’t need the create/update/ delete functionality. Let’s strip down ng-grid as follows: Now, let’s change the service code to look as follows: www.backand.com
  • 5. You need to update the controller code to authenticate the user and get customer details as soon as the controller is instantiated. Let’s add the following code to controller to authenticate the user and fill up the grid with customer data. The above lines of code make sure that as soon as this controller is instantiated, the user is authenticated; customer details are obtained. Now let’s add an <input /> tag that can act as the Typeahead to show the matching customer titles on text enter. The Typeahead directive looks for a value similar to the ng-repeat directive. As soon as this controller is instantiated and there is a value on the input tag, getCustomerTitles() function will be called with the current value in the input tag. And your controller code makes an asynchronous call to Backand’s autocomplete API using the backandService to get the autocomplete data. And Typeahead allows you to pass a function name that will be called when the user selects an item inside the Typeahead dropdown. This is the typeahead-on-select attribute. Let’s create a method in your controller to hook the text from the input tag and filter the ng-grid so that only Customers with matching title are shown in the grid. www.backand.com
  • 6. We already added a filterOptions property to our gridOptions while setting up ng-grid. So as soon as the filterOptions object is updated, ng-grid is also updated to show filtered data. Your Typeahead filter and ng-grid are working perfectly right now. But if you look deep there is a problem. If the user clears the input filter, ng-grid doesn’t update. This is because you are only updating the grid when the user selects a dropdown value from Typeahead. Now you need to be able to give an option to clear the filter applied on the grid. Let’s add a <button /> that says “clear” which will clear the filterOptions when the user clicks it. Add a “clearFilter()” method in the controller to do the necessary actions on the filterOptions. Now it’s all wired up nice and good. When the user enters something into the input filter box, a list of matches will be shown by getting the data from Backand’s autocomplete API using the Typeahead directive. When an option is selected from the list given by Typeahead, you apply the filter on the list of customers to show only the customers with matching title. And you can also remove the filtered information from the grid when pressing the clear button. There you go. Typeahead with ng-grid using Backand’s REST service. www.backand.com
  • 7. References ng-grid Documentation: http://angular-ui.github.io/ng-grid/ Typeahead: http://angular-ui.github.io/bootstrap/#/typeahead Plunker Code: http://plnkr.co/edit/uMXjvK?p=preview Build your Bootstrap + Angular app with Backand today. – Get started now. www.backand.com Contact Information Backand Inc. info@backand.com www.backand.com