Introduction

In my previous article, I have discussed about remote data binding in the Kendo combo box for Angular 2. This article is in continuation of that. Here, I’m going to discuss how to use the events and templates in Kendo combo box.

Please check on the list of Kendo UI components for Angular 2

Content

  • Filter an event in Kendo Combo box
  • Template in Kendo Combo Box

Filter Event in Kendo Combo box

Before Implementing the filtering event, we need to set a filtering property as true for the Kendo Combo box.

  1. import { Component, OnInit, Inject, ViewChild } from '@angular/core';
  2. import { Jsonp, JsonpModule } from '@angular/http';
  3. import { Observable } from 'rxjs/Rx';
  4. import { Technolgy } from './technologies.model';
  5. import { DataService } from './data.service';
  6. import { ComboBoxComponent } from '@progress/kendo-angular-dropdowns';
  7. interface Item {
  8. text?: string,
  9. value?: number
  10. }
  11. @Component({
  12. selector: 'app-combobox',
  13. styleUrls: ['./app.component.scss'],
  14. providers: [DataService],
  15. template: `
  16. <h1>{{title}}</h1>
  17. <div class="example-config">
  18. Selected Item: {{selectedItem}}
  19. </div>
  20. <div class="example-wrapper">
  21. <p>Favorite Technolgy:</p>
  22. <kendo-combobox
  23. #combo
  24. [data]="listItems"
  25. [textField]="'text'"
  26. [valueField]="'value'"
  27. [valuePrimitive]="true"
  28. [(ngModel)]="selectedItem"
  29. [filterable]="true"
  30. (filterChange)="handleFilter($event)"
  31. >
  32. </kendo-combobox>
  33. </div>
  34. `
  35. })
  36. export class AppComponent {
  37. @ViewChild('combo') public combo: ComboBoxComponent;
  38. title = 'Kendo Combo Box';
  39. public listItems: Array<Technolgy> = [];
  40. constructor(private dataService: DataService) {
  41. }
  42. ngOnInit() {
  43. this.dataService.getList().subscribe(
  44. (data) => this.listItems = data
  45. )
  46. }
  47. public selectedItem: number=1;
  48. handleFilter(value) {
  49. alert("Filter Event")
  50. }
  51. }

From the above code, you can notice the following changes -

  1. We have set the filter property as true.
  2. handleFilter is the filter event definition which is fired when the user modifies input value.
Result


Figure 1

Templates in Kendo Combo box for Angular 2

Kendo combo box consists of four template,

  1. Header Template
  2. Footer Template
  3. Item Template
  4. No data Template

Header Template

Use this template if you need to add the header to the kendo combo box list items. To define a header template, nest an <ng-template> tag with a kendoComboBoxHeaderTemplate directive inside a <kendo-combobox> tag.

  1. import { Component, OnInit, Inject, ViewChild } from '@angular/core';
  2. import { Jsonp, JsonpModule } from '@angular/http';
  3. import { Observable } from 'rxjs/Rx';
  4. import { Technolgy } from './technologies.model';
  5. import { DataService } from './data.service';
  6. import { ComboBoxComponent } from '@progress/kendo-angular-dropdowns';
  7. interface Item {
  8. text?: string,
  9. value?: number
  10. }
  11. @Component({
  12. selector: 'app-combobox',
  13. styleUrls: ['./app.component.scss'],
  14. providers: [DataService],
  15. template: `
  16. <h1>{{title}}</h1>
  17. <div class="example-config">
  18. Selected Item: {{selectedItem}}
  19. </div>
  20. <div class="example-wrapper">
  21. <p>Favorite Technolgy:</p>
  22. <kendo-combobox
  23. #combo
  24. [data]="listItems"
  25. [textField]="'text'"
  26. [valueField]="'value'"
  27. [valuePrimitive]="true"
  28. [(ngModel)]="selectedItem"
  29. [filterable]="true"
  30. (filterChange)="handleFilter($event)"
  31. >
  32. <ng-template kendoComboBoxHeaderTemplate>
  33. <h4>Technolgy</h4>
  34. </ng-template>
  35. </kendo-combobox>
  36. </div>
  37. `
  38. })
  39. export class AppComponent {
  40. @ViewChild('combo') public combo: ComboBoxComponent;
  41. title = 'Kendo Combo Box';
  42. public listItems: Array<Technolgy> = [];
  43. constructor(private dataService: DataService) {
  44. }
  45. ngOnInit() {
  46. this.dataService.getList().subscribe(
  47. (data) => this.listItems = data
  48. )
  49. }
  50. public selectedItem: number=1;
  51. handleFilter(value) {
  52. alert("Filter Event")
  53. }
  54. }
Result

Figure 2
Footer Template
Use this template if you need to add the footer for the Kendo Combo box list items. To define a footer template, nest an <ng-template> tag with a kendoComboBoxFooterTemplate directive inside a <kendo-combobox> tag.
  1. import { Component, OnInit, Inject, ViewChild } from '@angular/core';
  2. import { Jsonp, JsonpModule } from '@angular/http';
  3. import { Observable } from 'rxjs/Rx';
  4. import { Technolgy } from './technologies.model';
  5. import { DataService } from './data.service';
  6. import { ComboBoxComponent } from '@progress/kendo-angular-dropdowns';
  7. interface Item {
  8. text?: string,
  9. value?: number
  10. }
  11. @Component({
  12. selector: 'app-combobox',
  13. styleUrls: ['./app.component.scss'],
  14. providers: [DataService],
  15. template: `
  16. <h1>{{title}}</h1>
  17. <div class="example-config">
  18. Selected Item: {{selectedItem}}
  19. </div>
  20. <div class="example-wrapper">
  21. <p>Favorite Technolgy:</p>
  22. <kendo-combobox
  23. #combo
  24. [data]="listItems"
  25. [textField]="'text'"
  26. [valueField]="'value'"
  27. [valuePrimitive]="true"
  28. [(ngModel)]="selectedItem"
  29. [filterable]="true"
  30. (filterChange)="handleFilter($event)"
  31. >
  32. <ng-template kendoComboBoxHeaderTemplate>
  33. <h4>Technolgy</h4>
  34. </ng-template>
  35. <ng-template kendoComboBoxFooterTemplate>
  36. <h4>Total: {{listItems.length}}</h4>
  37. </ng-template>
  38. </kendo-combobox>
  39. </div>
  40. `
  41. })
  42. export class AppComponent {
  43. @ViewChild('combo') public combo: ComboBoxComponent;
  44. title = 'Kendo Combo Box';
  45. public listItems: Array<Technolgy> = [];
  46. constructor(private dataService: DataService) {
  47. }
  48. ngOnInit() {
  49. this.dataService.getList().subscribe(
  50. (data) => this.listItems = data
  51. )
  52. }
  53. public selectedItem: number=1;
  54. handleFilter(value) {
  55. alert("Filter Event")
  56. }
  57. }
Result


Figure 3
Item Template
Use this template if you need to customize the list which is populated in Kendo Combo box. To define an item template, nest an <ng-template> tag with a kendoComboBoxItemTemplate directive inside a <kendo-combobox> tag.
  1. import { Component, OnInit, Inject, ViewChild } from '@angular/core';
  2. import { Jsonp, JsonpModule } from '@angular/http';
  3. import { Observable } from 'rxjs/Rx';
  4. import { Technolgy } from './technologies.model';
  5. import { DataService } from './data.service';
  6. import { ComboBoxComponent } from '@progress/kendo-angular-dropdowns';
  7. interface Item {
  8. text?: string,
  9. value?: number
  10. }
  11. @Component({
  12. selector: 'app-combobox',
  13. styleUrls: ['./app.component.scss'],
  14. styles: ['.template { display: inline-block; background: #333; color: #fff; border-radius: 50%; width: 18px; height: 18px; text-align: center; } '],
  15. providers: [DataService],
  16. template: `
  17. <h1>{{title}}</h1>
  18. <div class="example-config">
  19. Selected Item: {{selectedItem}}
  20. </div>
  21. <div class="example-wrapper">
  22. <p>Favorite Technolgy:</p>
  23. <kendo-combobox
  24. #combo
  25. [data]="listItems"
  26. [textField]="'text'"
  27. [valueField]="'value'"
  28. [valuePrimitive]="true"
  29. [(ngModel)]="selectedItem"
  30. [filterable]="true"
  31. (filterChange)="handleFilter($event)"
  32. >
  33. <ng-template kendoComboBoxItemTemplate let-dataItem>
  34. <span class="template">{{ dataItem.value }}</span> {{ dataItem.text }}
  35. </ng-template>
  36. </kendo-combobox>
  37. </div>
  38. `
  39. })
  40. export class AppComponent {
  41. @ViewChild('combo') public combo: ComboBoxComponent;
  42. title = 'Kendo Combo Box';
  43. public listItems: Array<Technolgy> = [];
  44. constructor(private dataService: DataService) {
  45. }
  46. ngOnInit() {
  47. this.dataService.getList().subscribe(
  48. (data) => this.listItems = data
  49. )
  50. }
  51. public selectedItem: number=1;
  52. handleFilter(value) {
  53. alert("Filter Event")
  54. }
  55. }
Result

Figure 4
No Data Template
Use this template if you need to display a customized message and there is no list in Kendo Combo box. To define a no data template, nest an <ng-template> tag with a kendoDropDownListNoDataTemplate directive inside a <kendo-combobox> tag.
  1. import { Component, OnInit, Inject, ViewChild } from '@angular/core';
  2. import { Jsonp, JsonpModule } from '@angular/http';
  3. import { Observable } from 'rxjs/Rx';
  4. import { Technolgy } from './technologies.model';
  5. import { DataService } from './data.service';
  6. import { ComboBoxComponent } from '@progress/kendo-angular-dropdowns';
  7. interface Item {
  8. text?: string,
  9. value?: number
  10. }
  11. @Component({
  12. selector: 'app-combobox',
  13. styleUrls: ['./app.component.scss'],
  14. providers: [DataService],
  15. template: `
  16. <h1>{{title}}</h1>
  17. <div class="example-config">
  18. Selected Item: {{selectedItem}}
  19. </div>
  20. <div class="example-wrapper">
  21. <p>Favorite Technolgy:</p>
  22. <kendo-combobox
  23. #combo
  24. [data]="listItem"
  25. [textField]="'text'"
  26. [valueField]="'value'"
  27. [valuePrimitive]="true"
  28. [(ngModel)]="selectedItem"
  29. [filterable]="true"
  30. (filterChange)="handleFilter($event)"
  31. >
  32. <ng-template kendoDropDownListNoDataTemplate>
  33. <h4><span class="k-icon k-i-warning"></span><br /><br /> No data here</h4>
  34. </ng-template>
  35. </kendo-combobox>
  36. </div>
  37. `
  38. })
  39. export class AppComponent {
  40. @ViewChild('combo') public combo: ComboBoxComponent;
  41. title = 'Kendo Combo Box';
  42. public listItems: Array<Technolgy> = [];
  43. constructor(private dataService: DataService) {
  44. }
  45. ngOnInit() {
  46. this.dataService.getList().subscribe(
  47. (data) => this.listItems = data
  48. )
  49. }
  50. public selectedItem: number=1;
  51. handleFilter(value) {
  52. alert("Filter Event")
  53. }
  54. }
Result


Figure 5
References
  1. http://www.telerik.com/kendo-angular-ui/getting-started
  2. http://www.telerik.com/kendo-angular-ui/components/dropdowns/combobox/data-binding/
I hope you have learned from this article. Your valuable feedback, questions, or comments about this article are always welcome. Download the source code here.