JavaScript/jQuery SelectBox - 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 property. Note that you should implement the onCustomItemCreating handler to create a new data source entry.
HTML
JavaScript
- <div id="selectBoxContainer"></div>
- const 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'
- let nextId;
- selectBoxData.store().totalCount().done(count => {nextId = count + 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();
- }
- });
- });
See Also
Feel free to share topic-related thoughts here.
If you have technical questions, please create a support ticket in the DevExpress Support Center.
Thank you for the feedback!
If you have technical questions, please create a support ticket in the DevExpress Support Center.