Vue 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.

NOTE
Before you start the tutorial, ensure DevExtreme is installed in your application.

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.

View on GitHub

Create the SelectBox

Add DevExtreme to your Vue application and use the following code to create a SelectBox:

App.vue
  • <template>
  • <DxSelectBox id="select-box">
  • <!-- Configuration goes here -->
  • </DxSelectBox>
  • </template>
  •  
  • <script setup lang="ts">
  • import 'devextreme/dist/css/dx.fluent.blue.light.css';
  • import { DxSelectBox } from 'devextreme-vue/select-box';
  •  
  • </script>

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:

App.vue
data.js
  • <template>
  • <DxSelectBox
  • :data-source="data"
  • value-expr="ID"
  • display-expr="Name"
  • />
  • </template>
  •  
  • <script setup lang="ts">
  • import { DxSelectBox } from 'devextreme-vue/select-box';
  • import { data } from './data';
  •  
  • </script>
  • export const data = [{
  • 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',
  • }];

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:

App.vue
  • <template>
  • <DxSelectBox
  • :search-enabled="true"
  • />
  • </template>

For more information about DevExtreme SelectBox search capabilities, review the following demo:

View 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:

App.vue
  • <template>
  • <DxSelectBox
  • @value-changed="onValueChanged"
  • />
  • </template>
  •  
  • <script setup lang="ts">
  • import { DxSelectBox, type DxSelectBoxTypes } from 'devextreme-vue/select-box';
  • import notify from 'devextreme/ui/notify';
  •  
  • const onValueChanged = (e: DxSelectBoxTypes.ValueChangedEvent): void => {
  • notify(
  • `Previous Value: ${e.previousValue}, Current Value: ${e.value}`,
  • 'info',
  • 2000,
  • );
  • };
  • </script>

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.

App.vue
  • <template>
  • <DxSelectBox
  • label="Product"
  • label-mode="floating"
  • />
  • </template>
  •  
  • <script setup lang="ts">
  • import { DxSelectBox } from 'devextreme-vue/select-box';
  •  
  • </script>

View Demo

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:

  1. Bind the component to a DataSource instance
  2. Specify the group field in the component DataSource
  3. Enable the grouped property
App.vue
  • <template>
  • <DxSelectBox
  • :data-source="dataSource"
  • :grouped="true"
  • />
  • </template>
  •  
  • <script setup lang="ts">
  • import { DxSelectBox } from 'devextreme-vue/select-box';
  • import { DataSource } from 'devextreme-vue/common/data';
  •  
  • const dataSource = new DataSource({
  • store: {
  • data,
  • type: 'array',
  • key: 'ID',
  • },
  • group: 'Category',
  • });
  • </script>

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:

App.vue
  • <template>
  • <DxSelectBox>
  • <DxDropDownOptions :height="150" />
  • </DxSelectBox>
  • </template>
  •  
  • <script setup lang="ts">
  • import { DxSelectBox, DxDropDownOptions } from 'devextreme-vue/select-box';
  •  
  • </script>

You have configured basic SelectBox features. To take a more detailed look at this UI component, explore the following resources: