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

Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 

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