Angular SelectBox - Getting Started
Drop-down editors allow users to navigate through a list of items, select one or multiple items, and search through the list. To learn how to choose a DevExtreme drop-down editor and for more details about the component's features, refer to the following article: How to Choose a Drop-Down Editor.
SelectBox is an editor that allows users to select a value from a drop-down list or add a new value.
In this tutorial, we will create the SelectBox and configure its basic features. The created UI component has a populated drop-down list, allows users to search through it, and logs the previous and current selected items to the console.
Each section in this tutorial describes a single configuration step. You can also find the full code in the GitHub repository.
Create the SelectBox
Add DevExtreme to your Angular application and use the following code to create a SelectBox:
- <dx-select-box id="select-box">
- <!-- Configuration goes here -->
- </dx-select-box>
- import { Component } from '@angular/core';
- import { DxSelectBoxModule } from 'devextreme-angular/ui/select-box';
- @Component({
- imports: [DxSelectBoxModule],
- // ...
- })
- export class AppComponent {
- }
Bind the SelectBox to Data
The SelectBox can load data from different data source types. To use a local array, assign it to the dataSource property. If array elements are objects, set the fields that supply the SelectBox's value (valueExpr) and displayed value (displayExpr). For information on other data source types, refer to the following articles:
- <dx-select-box
- [dataSource]="data"
- valueExpr="ID"
- displayExpr="Name"
- ></dx-select-box>
- import { Component } from '@angular/core';
- import { DxSelectBoxModule } from 'devextreme-angular/ui/select-box';
- import { AppService, Item } from './app.service';
- // ...
- export class AppComponent {
- data: Item[];
- constructor(service: AppService) {
- this.data = service.getItems();
- }
- }
- import { Injectable } from '@angular/core';
- export class Item {
- ID: number;
- Name: string;
- Category: string;
- }
- const items: Item[] = [{
- ID: 1,
- Name: 'Banana',
- Category: 'Fruits',
- }, {
- ID: 2,
- Name: 'Cucumber',
- Category: 'Vegetables',
- }, {
- ID: 3,
- Name: 'Apple',
- Category: 'Fruits',
- }, {
- ID: 4,
- Name: 'Tomato',
- Category: 'Vegetables',
- }, {
- ID: 5,
- Name: 'Apricot',
- Category: 'Fruits',
- }]
- @Injectable()
- export class AppService {
- getItems(): Item[] {
- return items;
- }
- }
If you run this code and open the SelectBox, you will see the the populated drop-down list. Next, we will enable search.
Enable Search
Read Tutorial: Configure Search Parameters
To allow users to search through SelectBox values, set searchEnabled to true:
- <dx-select-box
- [searchEnabled]="true"
- ></dx-select-box>
For more information about DevExtreme SelectBox search capabilities, review the following demo:
Handle the Value Change Event
Use onValueChanged to specify a function that the component executes when users change the SelectBox value. The following example calls the notify utility method within this function:
- <dx-select-box
- (onValueChanged)="onValueChanged($event)"
- ></dx-select-box>
- import { DxSelectBoxModule, type DxSelectBoxTypes } from 'devextreme-angular/ui/select-box';
- import notify from 'devextreme/ui/notify';
- // ...
- export class AppComponent {
- onValueChanged(e: DxSelectBoxTypes.ValueChangedEvent): void {
- notify(
- `Previous Value: ${e.previousValue}, Current Value: ${e.value}`,
- 'info',
- 2000,
- );
- }
- }
Add a Label
Set the label property to specify label text. To enable floating labels, set labelMode to "floating". In floating mode, the label acts as a placeholder and moves above the input field when the editor receives focus.
- <dx-select-box
- label="Product"
- labelMode="floating">
- </dx-select-box>
Group Data
Read Tutorial: Grouping in the Data Source
SelectBox can display grouped data. To implement this capability using a flat data source, follow these steps:
- Bind the component to a DataSource instance
- Specify the group field in the component DataSource
- Enable the grouped property
- <dx-select-box
- [dataSource]="dataSource"
- [grouped]="true"
- ></dx-select-box>
- import { DxSelectBoxModule } from 'devextreme-angular/ui/select-box';
- import { DataSource } from 'devextreme-angular/common/data';
- // ...
- export class AppComponent {
- data: Item[];
- dataSource: DataSource;
- constructor(service: AppService) {
- this.data = service.getItems();
- this.dataSource = new DataSource({
- store: {
- data: this.data,
- type: 'array',
- key: 'ID',
- },
- group: 'Category',
- });
- }
- }
SelectBox can also display grouped data from a nested data source with one nesting level. Each object in the data source contains a key field and an items array. For more information, refer to the following guide: SelectBox - Grouping in the Data Source.
Customize the Drop-Down Menu
SelectBox uses the Popup component as a drop-down menu. To customize the menu, specify Popup properties in the dropDownOptions object:
- <dx-select-box>
- <dxo-select-box-drop-down-options
- [height]="150"
- ></dxo-select-box-drop-down-options>
- </dx-select-box>
You have configured basic SelectBox features. To take a more detailed look at this UI component, explore the following resources: