Templates In Kendo AutoComplete For Angular 2

Introduction

Recently, Telerik by Progress has released a RC version of Kendo UI for Angular 2. With this article, I would like to share how to implement Kendo autocomplete for Angular 2 from the recently released Kendo for Angular 2 package.

Please check on the list of kendo UI component for Angular 2

Content
  1. Setup Kendo UI for Angular 2.
  2. Configuring the Kendo auto complete control for Angular 2.
  3. Implementing the template in Kendo auto complete control for Angular 2. 

Quick setup of Kendo UI for Angular 2

Before going to the initial setup of the project, just make sure that the latest versions of Node.js and NPM are installed in your system

Step 1

Install Angular-CLI package, open the command prompt and write the command given below to install Angular CLI package

npm install -g angular-cli 

Step 2

Go to the respective directory where you need to save your project, and give the command given below in the command prompt to create a project.

ng new kendoangular --style=scss

Kendoangular is our project name.
 
Step 3

Build the Application, using the command given below. 
 
ng serve
 
Result in browser
 
 
Configuring Kendo auto complete control for angular 2 

Step 4

Let’s create a new component. In this article, we are going to see Kendo autocomplete for Angular 2. To use the autocomplete control, first we need to install the Kendo dropdowns module, give the command given below to install the Kendo dropdowns

npm install -S @progress/kendo-angular-dropdowns

Step 5

Create a separate component to organize our Application, use the command given below to install the component.

ng g component dropdown-autocomplete

Check out the figure given below to get the list of the files created to organize our autocomplete component.



The separate dropdown-autocomplete folder is created to maintain out autocomplete component.



Step 5

Open app.module.ts file and add the information of Kendo dropdowns, which are installed.
  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  3. import { FormsModule } from '@angular/forms';  
  4. import { HttpModule } from '@angular/http';  
  5. import { DropDownsModule } from '@progress/kendo-angular-dropdowns';  
  6. import { AppComponent } from './app.component';  
  7. import { DropdownAutocompleteComponent } from './dropdown-autocomplete/dropdown-autocomplete.component';  
  8.   
  9. @NgModule({  
  10.   declarations: [  
  11.     AppComponent,  
  12.       DropdownAutocompleteComponent  
  13.   
  14.   ],  
  15.   imports: [  
  16.     BrowserModule,  
  17.     FormsModule,  
  18.       HttpModule,  
  19.       DropDownsModule  
  20.   ],  
  21.   providers: [],  
  22.   bootstrap: [AppComponent]  
  23. })  
  24. export class AppModule { }   

Step 6

Open dropdown-autocomplete.compoenent.html and write the code given below in it 

  1. <h3>Dropdown-AutoComplete</h3>  
  2.   
  3. <kendo-autocomplete  
  4. [data]="countryList"  
  5.             [placeholder]="'e.g. India'"  
  6.              
  7.  ></kendo-autocomplete>   

Step 7

Open dropdown-autocomplete.compoenent.ts and write the code given below in it.

  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-dropdown-autocomplete',  
  5.   templateUrl: './dropdown-autocomplete.component.html',  
  6.   styleUrls: ['./dropdown-autocomplete.component.scss']  
  7. })  
  8. export class DropdownAutocompleteComponent implements OnInit {  
  9.     public countryList: Array<string> = [  
  10.         "India""China","Australia" 
  11.   constructor() { }  
  12.   
  13.   ngOnInit() {  
  14.   }  
  15.   
  16.  

The countrylist array is created, which is used to populate the auto complete control and the selector(app-dropdown-autocomplete) is defined.

Step 8

Open app.component.html file and write the code given below. 

  1. <h1>  
  2.   {{title}}  
  3. </h1>  
  4. <app-dropdown-autocomplete></app-dropdown-autocomplete>   

App.componenent.html is the main page, which is used to display the control based on the selector.

Step 9

Configure the theme

Open the style.scss and write the code given below in it

  1. @import "~@progress/kendo-theme-default/scss/all";   

The code given above is used to integrate Kendo's default theme into our Application.

Step 10
 
Build the application 
 
Give the command given below to build the Application.
 
ng serve 
 
Result in the Browser
 
 
Template in Kendo auto complete for angular 2
 
Header Template
 
Implementing the header template is much simpler, as we just need to include the <template> tag with KendoAutoCompleteHeaderTemplate Directive inside a <kendo-autocomplete> tag.

Write the code given below in dropdown-autocomplete.compoenent.html

  1. <h3>Dropdown-AutoComplete</h3>  
  2.   
  3. <kendo-autocomplete  
  4. [data]="countryList"  
  5.             [placeholder]="'e.g. India'"  
  6.             
  7.  >  
  8.     <template kendoAutoCompleteHeaderTemplate>  
  9.         <h4>Countries</h4>  
  10.     </template>  
  11.      
  12.   
  13. </kendo-autocomplete>   
Result in the Browser
 
 
Footer Template
 
Include the <template> tag with KendoAutoCompleteHeaderTemplate Directive inside a <kendo-autocomplete> tag
 
Write the code given below in dropdown-autocomplete.compoenent.html. 
  1. <h3>Dropdown-AutoComplete</h3>  
  2.   
  3. <kendo-autocomplete  
  4. [data]="countryList"  
  5.             [placeholder]="'e.g. India'"  
  6.   
  7.  >  
  8.     <template kendoAutoCompleteHeaderTemplate>  
  9.         <h4>Countries</h4>  
  10.     </template>  
  11.     <template kendoAutoCompleteFooterTemplate>  
  12.         <h4>Total: {{countryList.length}} countries</h4>  
  13.     </template>  
  14.     
  15. </kendo-autocomplete>   
Result in the Browser
 


No-Data Template

Include the <template> tag with KendoDropDownListNoDataTemplate Directive inside a <kendo-autocomplete> tag

Write the code given below in dropdown-autocomplete.compoenent.html

  1. <h3>Dropdown-AutoComplete</h3>  
  2.   
  3. <kendo-autocomplete  
  4. [data]="countryList"  
  5.             [placeholder]="'e.g. India'"  
  6.   
  7.  >  
  8.     <template kendoAutoCompleteHeaderTemplate>  
  9.         <h4>Countries</h4>  
  10.     </template>  
  11.     <template kendoAutoCompleteFooterTemplate>  
  12.         <h4>Total: {{countryList.length}} countries</h4>  
  13.     </template>  
  14.     <template kendoDropDownListNoDataTemplate>  
  15.         <h4><span class="k-icon k-i-warning"></span><br /><br /> No data here</h4>  
  16.     </template>  
  17. </kendo-autocomplete>   
dropdown-autocomplete.compoenent.ts 
  1. import { Component, OnInit } from '@angular/core';  
  2.   
  3. @Component({  
  4.   selector: 'app-dropdown-autocomplete',  
  5.   templateUrl: './dropdown-autocomplete.component.html',  
  6.   styleUrls: ['./dropdown-autocomplete.component.scss'],  
  7.    styles: ['.k-i-warning { font-size: 2.5em; } h4 { font-size: 1em;}'],  
  8. })  
  9. export class DropdownAutocompleteComponent implements OnInit {  
  10.     public countryList: Array<string> = []  
  11.   constructor() { }  
  12.   
  13.   ngOnInit() {  
  14.   }  
  15.   
  16. }   
Result in the Browser
 
 
I hope you have enjoyed this article. Your valuable feedback, questions or comments about this article are always welcome.

Up Next
    Ebook Download
    View all
    Learn
    View all