All Components

DropDownList Overview

The DropDownList is a form component that lets you choose a single predefined value from a list.

It is a richer version of the <select> element and supports data binding, filtering, templates, and default items.

Basic Usage

The following example demonstrates the DropDownList in action.

@Component({
  selector: 'my-app',
  template: `
    <div class="example-wrapper">
      <p>T-shirt size:</p>
      <kendo-dropdownlist [data]="listItems">
      </kendo-dropdownlist>
    </div>
  `
})
class AppComponent {
    public listItems: Array<string> = ["X-Small", "Small", "Medium", "Large", "X-Large", "2X-Large"];
}

Configuration

The ComboBox provides configuration options for:

Set the Default Item

The defaultItem property type has to match the data type. For example, if the data property contains a list of objects, the defaultItem has to be defined as an object with the same textField and valueField as the data items.

The following example demonstrates how to define a defaultItem as a primitive value.

@Component({
  selector: 'my-app',
  template: `
    <kendo-dropdownlist
        [defaultItem]="'Select item...'"
        [data]="listItems"
        [valuePrimitive]="true"
    >
    </kendo-dropdownlist>
  `
})
class AppComponent {
    public listItems: Array<string> = ["Item 1", "Item 2", "Item 3"];
}

The following example demonstrates how to define a defaultItem as an object value.

@Component({
  selector: 'my-app',
  template: `
    <kendo-dropdownlist
        [defaultItem]="defaultItem"
        [data]="listItems"
        [textField]="'text'"
        [valueField]="'value'"
    >
    </kendo-dropdownlist>
  `
})
class AppComponent {
    public defaultItem: { text: string, value: number } = { text: "Select item...", value: null };

    public listItems: Array<{ text: string, value: number }> = [
        { text: "Small", value: 1 },
        { text: "Medium", value: 2 },
        { text: "Large", value: 3 }
    ];
}

Handle the Templates

To set the templates, the DropDownList provides configuration options for:

To customize the list item, the selected value, the popup footer or header content, use the item, value, footer, or header template respectively.

Set the Item Template

Use the item template to customize the content of every list item. To define an item template, nest a <template> tag with an kendoDropDownListItemTemplate directive inside a <kendo-dropdownlist> tag.

The template context is set to the current DropDownList component. To get a reference to the current data item, use the let-dataItem directive.

@Component({
  selector: 'my-app',
  styles: ['.template { display: inline-block; background: #333; color: #fff; border-radius: 50%; width: 18px; height: 18px; text-align: center; } '],
  template: `
    <kendo-dropdownlist
        [data]="listItems"
        [textField]="'text'"
        [valueField]="'value'"
        [value]="selectedValue"
        [valuePrimitive]="true"
    >
        <template kendoDropDownListItemTemplate let-dataItem>
            <span class="template">{{ dataItem.value }}</span> {{ dataItem.text }}
        </template>
    </kendo-dropdownlist>
  `
})
class AppComponent {
    public listItems: Array<{ text: string, value: number }> = [
        { text: "Small", value: 1 },
        { text: "Medium", value: 2 },
        { text: "Large", value: 3 }
    ];

    public selectedValue: number = 2;
}

Set the Value Template

Use the value template to customize the element content that holds the selected text. To define a value template, nest a <template> tag with a kendoDropDownListValueTemplate directive inside a <kendo-dropdownlist> tag.

The template context is set to the current DropDownList component. To get a reference to the current data item, use the let-dataItem directive.

@Component({
  selector: 'my-app',
  styles: ['.template { display: inline-block; background: #333; color: #fff; border-radius: 50%; width: 18px; height: 18px; text-align: center; } '],
  template: `
    <kendo-dropdownlist
        [data]="listItems"
        [textField]="'text'"
        [valueField]="'value'"
        [value]="selectedValue"
    >
        <template kendoDropDownListValueTemplate let-dataItem>
            <span class="template">{{ dataItem?.value }}</span>&nbsp;{{ dataItem?.text }}
        </template>
    </kendo-dropdownlist>
  `
})
class AppComponent {
    public listItems: Array<{ text: string, value: number }> = [
        { text: "Small", value: 1 },
        { text: "Medium", value: 2 },
        { text: "Large", value: 3 }
    ];

    public selectedValue: { text: string, value: number } = { text: "Foo", value: 1 };
}

Set the Header Template

Use the header template to customize the popup header element. To define a header template, nest a <template> tag with a kendoDropDownListHeaderTemplate directive inside a <kendo-dropdownlist> tag.

The template context is set to the current DropDownList component.

@Component({
  selector: 'my-app',
  styles: ['h4 { font-size: 1em; font-weight: bold; padding: 0 8px; }'],
  template: `
    <kendo-dropdownlist
        [data]="listItems"
        [textField]="'text'"
        [valueField]="'value'"
        [value]="selectedValue"
        [valuePrimitive]="true"
    >
        <template kendoDropDownListHeaderTemplate>
            <h4>T-shirt sizes</h4>
        </template>
    </kendo-dropdownlist>
  `
})
class AppComponent {
    public listItems: Array<{ text: string, value: number }> = [
        { text: "Small", value: 1 },
        { text: "Medium", value: 2 },
        { text: "Large", value: 3 }
    ];

    public selectedValue: number = 2;
}

Use the footer template to customize the popup footer element. To define a footer template, nest a <template> tag with a kendoDropDownListFooterTemplate directive inside a <kendo-dropdownlist> tag.

The template context is set to the current DropDownList component.

@Component({
  selector: 'my-app',
  styles: ['h4 { font-size: 1em; font-weight: bold; padding: 0 8px; }'],
  template: `
    <kendo-dropdownlist
        [data]="listItems"
        [textField]="'text'"
        [valueField]="'value'"
        [value]="selectedValue"
        [valuePrimitive]="true"
    >
        <template kendoDropDownListFooterTemplate>
            <h4>{{listItems.length}} sizes available</h4>
        </template>
    </kendo-dropdownlist>
  `
})
class AppComponent {
    public listItems: Array<{ text: string, value: number }> = [
        { text: "Small", value: 1 },
        { text: "Medium", value: 2 },
        { text: "Large", value: 3 }
    ];

    public selectedValue: number = 2;
}

Set the No-Data Template

Use the no-data template to customize the popup content when no data is present. To define a no-data template, nest a <template> tag with a kendoDropDownListNoDataTemplate directive inside a <kendo-dropdownlist> tag.

@Component({
  selector: 'my-app',
  styles: ['.k-i-warning { font-size: 2.5em; } h4 { font-size: 1em;}'],
  template: `
    <kendo-dropdownlist
        [data]="listItems"
    >
        <template kendoDropDownListNoDataTemplate>
            <h4><span class="k-icon k-i-warning"></span><br /><br /> No data here</h4>
        </template>
    </kendo-dropdownlist>
  `
})
class AppComponent {
    public listItems: Array<{ text: string, value: number }> = [];
}

Data Binding

For more information on how to handle data operation processing, refer to the article on data binding.

Value Binding

The DropDownList value can be either of the primitive (string, number) or the complex (objects) type.

To set the value, apply either of the following approaches:

  • Use the value property. If the value is set through the value property, you have to hook up to the valueChange event and manually update the value of the value property.
  • Use the ngModel value binding. If the value is set by the ngModel value binding, the framework automatically will update the corresponding field from the model after the value of the component changes.

The DropDownList does not support the simultaneous usage of the value property and the ngModel value binding.

When binding the DropDownList value, the component provides options for:

Use Primitive Type Values

If the DropDownList is bound to a dataset of primitives, its value will be of the primitive type (string, number).

@Component({
  selector: 'my-app',
  template: `
    <kendo-dropdownlist [data]="listItems" [value]="selectedItem"></kendo-dropdownlist>
  `
})
class AppComponent {
    public listItems: Array<string> = [ "Small", "Medium", "Large" ];
    public selectedItem: string = "Medium";
}

If the DropDownList is bound to a dataset of complex objects and the valuePrimitive property is set to true, its value will also be of the primitive type (string, number).

@Component({
  selector: 'my-app',
  template: `
    <kendo-dropdownlist
        [data]="listItems"
        [textField]="'text'"
        [valueField]="'value'"
        [valuePrimitive]="true"
        [value]="selectedItem"
    >
    </kendo-dropdownlist>
  `
})
class AppComponent {
    public listItems: Array<{ text: string, value: number }> = [
        { text: "Small", value: 1 },
        { text: "Medium", value: 2 },
        { text: "Large", value: 3 }
    ];

    public selectedItem: number = 2;
}

Use Complex Type Values

If the DropDownList is bound to a dataset of objects, its value will be of the complex type (objects).

@Component({
  selector: 'my-app',
  template: `
    <kendo-dropdownlist
        [data]="listItems"
        [textField]="'text'"
        [valueField]="'value'"
        [value]="selectedItem"
    >
    </kendo-dropdownlist>
  `
})
class AppComponent {
    public listItems: Array<{ text: string, value: number }> = [
        { text: "Small", value: 1 },
        { text: "Medium", value: 2 },
        { text: "Large", value: 3 }
    ];

    public selectedItem: { text: string, value: number } = { text: "Medium", value: 2 };
}

Handle Invalid Value Errors

If the value that is being provided does not match the expected type, the DropDownList will throw a JavaScript error. To fix it, you have to change the value type or to update the settings of the component.

The following table lists the valid configuration scenarios.

Value Data ValuePrimitive
primitive primitives Not set (automatically calculated as true).
object objects Not set (automatically calculated as false).
primitive objects true (manually set by the developer).

Forms Support

It is possible to use the DropDownList in template-driven or reactive forms. The component accepts values of the complex (objects) or the primitive (strings or numbers) type. To specify the value type, set the valuePrimitive property. By default, it is set to false—just like the defaultItem, the value has to match the data type.

Template-Driven Forms

The template-driven forms enable you to bind the DropDownList to the model by using the ngModel directive.

The following example demonstrates how to accomplish a two-way data binding when the model field is a complex value.

@Component({
  selector: 'my-app',
  template: `
    <form #form="ngForm">
        <div class="example-config">
            The selected gender is <strong>{{ gender | json }}</strong>
        </div>
        <label>
            Select Gender:
            <kendo-dropdownlist
                name="gender"
                [data]="genders"
                [textField]="'text'"
                [valueField]="'value'"
                [(ngModel)]="gender"
            >
            </kendo-dropdownlist>
        </label>
    </form>
  `
})
class AppComponent {
    public genders: Array<{ text: string, value: number }> = [
        { text: "Male", value: 1 },
        { text: "Female", value: 2 },
        { text: "Other", value: 3 }
    ];

    public gender: { text: string, value: number } = { text: "Female", value: 2 };
}

The following example demonstrates how to accomplish a two-way data binding when the model field is a primitive value.

@Component({
  selector: 'my-app',
  template: `
    <form #form="ngForm">
        <div class="example-config">
            The selected gender is <strong>{{ gender | json }}</strong>
        </div>
        <label>
            Select Gender:
            <kendo-dropdownlist
                name="gender"
                [data]="genders"
                [textField]="'text'"
                [valueField]="'value'"
                [valuePrimitive]="true"
                [(ngModel)]="gender"
            >
            </kendo-dropdownlist>
        </label>
    </form>
  `
})
class AppComponent {
    public genders: Array<{ text: string, value: number }> = [
        { text: "Male", value: 1 },
        { text: "Female", value: 2 },
        { text: "Other", value: 3 }
    ];

    public gender: { text: string, value: number } = 2;
}

Reactive Forms

The FormGroup provides a way to render reactive forms. For more details, refer to the Angular Documentation.

The following example demonstrates how to use the DropDownList in a reactive form with a primitive value binding.
 

import {
    FormsModule,
    ReactiveFormsModule,
    FormGroup,
    FormControl
} from '@angular/forms';

@Component({
  selector: 'my-app',
  template: `
    <form [formGroup]="myForm">
        <div class="example-config">
            <p>The form is valid: <strong>{{myForm.controls.gender.valid}}</strong></p>
            <p>The form.gender value is: <strong>{{myForm.controls.gender.value}}</strong></p>
        </div>
        <label>
            Select gender
            <kendo-dropdownlist
                formControlName="gender"
                [data]="genders"
                [(ngModel)]="gender"
                [defaultItem]="{ text: 'Select gender', value: null }"
                [textField]="'text'"
                [valueField]="'value'"
                [valuePrimitive]="true"
                required
            >
            </kendo-dropdownlist>
        </label>
    </form>
  `
})
class AppComponent {
    public genders: Array<{ text: string, value: number }> = [
        { text: "Male", value: 1 },
        { text: "Female", value: 2 }
    ];

    public gender: { text: string, value: number };

    public myForm: FormGroup = new FormGroup({
        gender: new FormControl()
    });
}

The following example demonstrates how to use the DropDownList in a reactive form with a complex value binding and required validation.

import { Component } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { requiredValidatorLogic } from './required.directive';

@Component({
  selector: 'my-app',
  template: `
    <form [formGroup]="myForm">
        <fieldset>
            The form.gender value is: <pre>{{myForm.controls.gender.value | json}}</pre>
            <br />
            The form is valid: {{myForm.controls.gender.valid}}
        </fieldset>
        <label>
            Select gender
            <kendo-dropdownlist
                isRequired="true"
                formControlName="gender"
                [data]="genders"
                [defaultItem]="{ text: 'Select gender', value: null }"
                [textField]="'text'"
                [valueField]="'id'"
            >
            </kendo-dropdownlist>
        </label>
    </form>
  `
})

export class AppComponent {
    public genders: Array<{ text: string, id: number }> = [
        { text: "Male", id: 1 },
        { text: "Female", id: 2 }
    ];

    public myForm: FormGroup = new FormGroup({
        gender: new FormControl({}, requiredValidatorLogic)
    });
}
import {
    forwardRef,
    Directive,
    Attribute
} from '@angular/core';

import {
    Validator,
    NG_VALIDATORS
} from '@angular/forms';

export const requiredValidatorLogic = (required) => (control) => {
    if (!control.value) return { valid: false }
    return control.value.id && control.value.id !== null ?
        null : { valid: false };
};

const Required_VALIDATOR: any = {
   provide: NG_VALIDATORS,
   useExisting: forwardRef(() => RequiredValidator),
   multi: true
};

@Directive({
     selector: '[isRequired]',
     providers: [Required_VALIDATOR]
})

export class RequiredValidator implements Validator {
    private _validator: any;
    constructor(@Attribute('isRequired') required: boolean) {
        this._validator = requiredValidatorLogic(required);
    }
    validate(c) {
        return this._validator(c);
    }
}
import { enableProdMode, NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { DropDownsModule } from '@progress/kendo-angular-dropdowns';
import { AppComponent }   from './app.component';
import { RequiredValidator }   from './required.directive';

@NgModule({
    declarations: [ AppComponent, RequiredValidator ],
    imports: [ BrowserModule, FormsModule, ReactiveFormsModule, DropDownsModule ],
    bootstrap: [ AppComponent ]
})

export class AppModule { }

enableProdMode();

const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);

The required built-in validator does not support object validation. If a defaultItem is set, the developer needs to implement the required custom validation, as demonstrated in the example.

Filtering

To enable the filtering functionality, set the filterable property to true. As a result, the component renders a filter input in the popup component. On every character input, the component triggers a filterChange event. The event argument contains the typed string value that you can use to filter the source.

@Component({
  selector: 'my-app',
  template: `
    <kendo-dropdownlist
        [data]="data"
        [filterable]="true"
        [textField]="'text'"
        [valueField]="'value'"
        (filterChange)="handleFilter($event)"
    >
    </kendo-dropdownlist>
  `
})
class AppComponent {
    public source: Array<{ text: string, value: number }> = [
        { text: "Small", value: 1 },
        { text: "Medium", value: 2 },
        { text: "Large", value: 3 }
    ];

    public data: Array<{ text: string, value: number }>;

    constructor() {
        this.data = this.source.slice();
    }

    handleFilter(value) {
        this.data = this.source.filter((s) => s.text.toLowerCase().indexOf(value.toLowerCase()) !== -1);
    }
}

To filter the data after a delay, use a similar implementation.

import { Component, ViewChild } from '@angular/core';
import { Observable, BehaviorSubject } from 'rxjs/Rx';

@Component({
  selector: 'my-app',
  template: `
    <kendo-dropdownlist
        #list
        [data]="data"
        [filterable]="true"
        [textField]="'text'"
        [valueField]="'value'"
    >
    </kendo-dropdownlist>
  `
})
export class AppComponent {
    @ViewChild("list") list;

    public source: Array<{ text: string, value: number }> = [
        { text: "Small", value: 1 },
        { text: "Medium", value: 2 },
        { text: "Large", value: 3 }
    ];

    public data: Array<{ text: string, value: number }>;

    constructor() {
        this.data = this.source.slice();
    }

    ngAfterViewInit() {
      const contains = value => s => s.text.toLowerCase().indexOf(value.toLowerCase()) !== -1);

      this.list.filterChange.asObservable().switchMap(value => Observable.from([this.source])
        .delay(1000)
        .map((data) =>  data.filter(contains(value))))
        .subscribe(x => this.data = x);
    }

}

Cascading DropDownLists

For more information on how to filter a series of DropDownLists based on the selected option in each of the previous ones, refer to the article on cascading ComboBoxes.

Keyboard Navigation

For more information on the keyboard shortcuts the DropDownList supports, refer to the article on keyboard navigation.

Accessibility

For more information on the accessibility features of the DropDownList, refer to the article on accessibility.

Events

On Changing the Item Selection

The selectionChange event is triggered when the item selection is changed.

@Component({
  selector: 'my-app',
  template: `
    <div class="example-wrapper">
        <p>T-shirt size:</p>
        <kendo-dropdownlist
            [data]="data"
            [textField]="'text'"
            [valueField]="'value'"
            (selectionChange)="handleSelection($event)"
        >
        </kendo-dropdownlist>
    </div>
    <div class="example-config">
        <h4>Last 10 calls:</h4>
        <p *ngFor="let call of eventCalls">{{ call.name }}: <strong>{{ call.value | json }}</strong></p>
    </div>
  `
})
class AppComponent {
    public data: Array<{ text: string, value: number }> = [
        { text: "Small", value: 1 },
        { text: "Medium", value: 2 },
        { text: "Large", value: 3 }
    ];

    public eventCalls: Array<any> = [];

    last10Calls() {
        const end = this.eventCalls.length;

        return this.eventCalls.slice(Math.max(0, end - 10), end);
    }

    handleSelection(value) {
        this.eventCalls = this.last10Calls().concat([ { name: 'select', value: value } ]);
    }
}

On Changing the Value

The valueChange event is triggered when the selected value is changed. If the popup is open, the valueChange event will not fire. To raise the change event when the popup is still open, either blur the component or press ENTER.

@Component({
  selector: 'my-app',
  template: `
    <div class="example-wrapper">
        <p>T-shirt size:</p>
        <kendo-dropdownlist
            [data]="data"
            [textField]="'text'"
            [valueField]="'value'"
            (valueChange)="handleValue($event)"
        >
        </kendo-dropdownlist>
    </div>
    <div class="example-config">
        <h4>Last 10 calls:</h4>
        <p *ngFor="let call of eventCalls">{{ call.name }}: <strong>{{ call.value | json }}</strong></p>
    </div>
  `
})
class AppComponent {
    public data: Array<{ text: string, value: number }> = [
        { text: "Small", value: 1 },
        { text: "Medium", value: 2 },
        { text: "Large", value: 3 }
    ];

    public eventCalls: Array<any> = [];

    last10Calls() {
        const end = this.eventCalls.length;

        return this.eventCalls.slice(Math.max(0, end - 10), end);
    }

    handleValue(value) {
        this.eventCalls = this.last10Calls().concat([ { name: 'change', value: value } ]);
    }
}

On Changing the Filter

The filterChange event fires when the user types inside the filter input. For more details, refer to the section on filtering.

On Looping through Items

By default, it is possible to select a DropDownList item by pressing a keyboard key. For example, if the DropDownList items are Foo, Bar, and Baz, and the user presses the B letter key, the first of the DropDownList items, based on their alphabetical order, starting with a B will be selected.

The keyboard selection is available only if filtering is disabled.

In this article