All docs
V19.1
24.2
The page you are viewing does not exist in version 24.2.
24.1
The page you are viewing does not exist in version 24.1.
23.2
The page you are viewing does not exist in version 23.2.
23.1
The page you are viewing does not exist in version 23.1.
22.2
The page you are viewing does not exist in version 22.2.
22.1
The page you are viewing does not exist in version 22.1.
21.2
The page you are viewing does not exist in version 21.2.
21.1
The page you are viewing does not exist in version 21.1.
20.2
The page you are viewing does not exist in version 20.2.
20.1
The page you are viewing does not exist in version 20.1.
19.2
19.1
18.2
18.1
17.2
A newer version of this page is available. Switch to the current version.

DevExtreme jQuery/JS - Create a User-Defined Item

A user can select existing values and add new values to the SelectBox. To enable this feature, assign true to the acceptCustomValue option. Note that you should implement the onCustomItemCreating handler to create a new data source entry.

jQuery
HTML
JavaScript
<div id="selectBoxContainer"></div>
var selectBoxData = new DevExpress.data.DataSource({
    store: [
        { id: 1, firstName: "Andrew" },
        { id: 2, firstName: "Nancy" },
        { id: 3, firstName: "Steven" }
    ]
});

$(function() {
    $("#selectBoxContainer").dxSelectBox({
        dataSource: selectBoxData,
        valueExpr: 'id',
        displayExpr: 'firstName',
        acceptCustomValue: true,
        onCustomItemCreating: function(e) {
            // Generates a new 'id'
            var nextId = Math.max.apply(Math, selectBoxData.items().map(function(c) { return c.id; })) + 1;
            // Creates a new entry
            e.customItem = { id: nextId, firstName: e.text };
            // Adds the entry to the data source
            selectBoxData.store().insert(e.customItem);
            // Reloads the data source
            selectBoxData.reload();
        }
    });
});
Angular
TypeScript
HTML
import DataSource from "devextreme/data/data_source";
import { DxSelectBoxModule } from "devextreme-angular";
// ...
export class AppComponent {
    selectBoxData = new DataSource({
        store: [
            { id: 1, firstName: "Andrew" },
            { id: 2, firstName: "Nancy" },
            { id: 3, firstName: "Steven" }
        ]
    });
    onCustomItemCreating (e) {
        // Generates a new 'id'
        let nextId = Math.max.apply(Math, this.selectBoxData.items().map(function(c) { return c.id; })) + 1;
        // Creates a new entry
        e.customItem = { id: nextId, firstName: e.text };
        // Adds the entry to the data source
        this.selectBoxData.store().insert(e.customItem);
        // Reloads the data source
        this.selectBoxData.reload();
    }
}
@NgModule({
     imports: [
         // ...
         DxSelectBoxModule
     ],
     // ...
 })
<dx-select-box
    [dataSource]="selectBoxData"
    valueExpr="id"
    displayExpr="firstName"
    [acceptCustomValue]="true"
    (onCustomItemCreating)="onCustomItemCreating($event)">
</dx-select-box>

View Demo

See Also