JavaScript/jQuery 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

Use the code below to create an empty SelectBox:

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

index.js
index.html
  • $(function() {
  • $("#selectBox").dxSelectBox({ });
  • });
  • <html>
  • <head>
  • <!-- ... -->
  • <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  • <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/24.2.3/css/dx.light.css">
  • <script type="text/javascript" src="https://cdn3.devexpress.com/jslib/24.2.3/js/dx.all.js"></script>
  • <script type="text/javascript" src="index.js"></script>
  • </head>
  • <body>
  • <div id="selectBox"></div>
  • </body>
  • </html>

Run the code and ensure the SelectBox is displayed, and its drop-down list says "No data to display." In the next step, we will add a data source.

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:

index.js
  • $(function() {
  • 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'
  • }]
  •  
  • $("#selectBox").dxSelectBox({
  • dataSource: data,
  • valueExpr: "ID",
  • displayExpr: "Name"
  • });
  • });

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

To allow users to search through SelectBox values, set searchEnabled to true:

index.js
  • $(function() {
  • $("#selectBox").dxSelectBox({
  • // ...
  • searchEnabled: true
  • });
  • });

This demo shows additional search properties:

View Demo

In the next step, we will process the SelectBox's value change event.

Handle the Value Change Event

Implement the onValueChanged handler to perform an action when a user selects an item. In the code below, this function logs the IDs of the currently and previously selected items.

index.js
  • $(function() {
  • $("#selectBox").dxSelectBox({
  • // ...
  • onValueChanged: function(e) {
  • console.log(e.value);
  • console.log(e.previousValue);
  • },
  • });
  • });

Add a Label

To specify label text, set the label property. If you want to enable floating labels, set the labelMode property to "floating". In this case, the label acts as a placeholder, but when the editor gets focus, the label moves to the position above the input field.

index.js
  • $(function() {
  • $("#selectBox").dxSelectBox({
  • // ...
  • label: "Product",
  • labelMode: "floating",
  • });
  • });

View Demo

Group Data

The SelectBox can display data grouped by category. To implement this, we will use the data from the previous steps with the DataSource component. Its API allows you to sort, filter, select, and group data. At its core, the DataSource has a store - an object that keeps data and provides an API to access and modify it. To configure the store, use the DataSource's store object. Specify its type as "array", pass the initial data array to the data field, and set the key field.

To group data, specify the data field to group by the DataSource's group property and set the SelectBox's grouped property to true.

index.js
  • $(function() {
  • const dataSource = new DevExpress.data.DataSource({
  • store: data,
  • type: "array",
  • key: "ID",
  • group: "Category"
  • });
  •  
  • $("#selectBox").dxSelectBox({
  • // ...
  • grouped: true
  • });
  • });

If your data is already grouped, ensure each group has the key and items fields as shown in the following article: Grouping in the Data Source. If the fields are named differently, implement the DataSource's map function to create key + items field mappings.

Run the code and ensure the UI component's data is grouped.

Customize the Drop-Down Menu

The SelectBox uses the Popup component as a drop-down menu. To customize the menu, specify Popup properties in the dropDownOptions object. For example, the following code sets the height of SelectBox's drop-down menu to 150 pixels:

index.js
  • $(function() {
  • $("#selectBox").dxSelectBox({
  • // ...
  • dropDownOptions: {
  • height: 150
  • }
  • });
  • });

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