Vue SelectBox - Custom Sources

Access to a custom data source is configured using the CustomStore component. DevExtreme provides ASP.NET and PHP extensions that help configure it and implement server-side data processing. You can also use the third-party extension for MongoDB. If these extensions are not suitable for your data source, follow the instructions below to configure the CustomStore manually.

The CustomSource's configuration differs depending on whether data is processed on the client or server. In the former case, switch the CustomStore to the raw mode and load all data from the server using the load function as shown in the following example:

jQuery
JavaScript
$(function() {
    $("#selectBoxContainer").dxSelectBox({
        dataSource: new DevExpress.data.DataSource({
            store: new DevExpress.data.CustomStore({
                key: "ID",
                loadMode: "raw",   
                load: function () {
                    return $.getJSON('https://mydomain.com/MyDataService');
                }
            })
        })
    });
});
Angular
TypeScript
HTML
import { ..., Inject } from "@angular/core";
import { HttpClient, HttpClientModule } from "@angular/common/http";
import { DxSelectBoxModule } from "devextreme-angular";
import DataSource from "devextreme/data/data_source";
import CustomStore from "devextreme/data/custom_store";
import "rxjs/add/operator/toPromise";
// ...
export class AppComponent {
    selectBoxDataSource: any = {};
    constructor(@Inject(HttpClient) httpClient: HttpClient) {
        this.selectBoxDataSource = new DataSource({
            store: new CustomStore({
                key: "ID",
                loadMode: "raw",   
                load: () => {
                    return httpClient.get('http://mydomain.com/MyDataService')
                        .toPromise();
                }
            })
        });
    }
}
@NgModule({
    imports: [
        // ...
        DxSelectBoxModule,
        HttpClientModule
    ],
    // ...
})
<dx-select-box ...
    [dataSource]="selectBoxDataSource">
</dx-select-box>
Vue
<template>
    <DxSelectBox ... 
        :data-source="dataSource" />
</template>
<script>
import { DxSelectBox } from "devextreme-vue/select-box";
import CustomStore from "devextreme/data/custom_store";
import DataSource from "devextreme/data/data_source";
import 'whatwg-fetch';
// ...
function handleErrors(response) {
    if (!response.ok)
        throw Error(response.statusText);
    return response;
}
const selectBoxDataSource = new DataSource({
    store: new CustomStore({
        key: "ID",
        loadMode: "raw",   
        load: () => {
            return fetch("https://mydomain.com/MyDataService")
                    .then(handleErrors);
        }
    })
});
export default {
    // ...
    data() {
        return {
            dataSource: selectBoxDataSource
        };
    },
    components: {
        // ...
        DxSelectBox
    }
}
</script>
React
import React from "react";
import SelectBox from "devextreme-react/select-box";
import CustomStore from "devextreme/data/custom_store";
import DataSource from "devextreme/data/data_source";
import 'whatwg-fetch';
// ...
function handleErrors(response) {
    if (!response.ok)
        throw Error(response.statusText);
    return response;
}
const selectBoxDataSource = new DataSource({
    store: new CustomStore({
        key: "ID",
        loadMode: "raw",   
        load: () => {
            return fetch("https://mydomain.com/MyDataService")
                    .then(handleErrors);
        }
    })
});
class App extends React.Component {
    render() {
        return (
            <SelectBox ...
                dataSource={selectBoxDataSource}>
            </SelectBox>
        );
    }
}
export default App;
NOTE
We recommend not using this mode with large amounts of data because all data is loaded at once.

In the latter case, use the CustomStore's load function to send data processing settings to the server. These settings are passed as a parameter to the load function and depend on the operations (paging, filtering, sorting, etc.) that you have enabled in the DataSource. The following settings are relevant for the SelectBox:

After receiving these settings, the server should apply them to data and send back an object with the following structure:

{
    data: [{
        key: "Group 1",
        items: [ ... ] // result data objects
    },
    ...
    ]
}

If the group setting is absent, the object structure is different:

{
    data: [ ... ] // result data objects
}

If you specify the SelectBox's value beforehand, the CustomStore should implement the byKey operation. If the SelectBox allows a user to add custom items, implement the insert method. Below is a generalized CustomStore configuration for the SelectBox UI component.

jQuery
JavaScript
$(function() {
    $("#selectBoxContainer").dxSelectBox({
        dataSource: new DevExpress.data.DataSource({
            key: "ID",
            load: function(loadOptions) {
                const d = $.Deferred(),
                    params = {};
                [
                    "skip",     
                    "take",  
                    "sort", 
                    "filter", 
                    "searchExpr",
                    "searchOperation",
                    "searchValue",
                    "group", 
                ].forEach(function(i) {
                    if(i in loadOptions && isNotEmpty(loadOptions[i])) 
                        params[i] = JSON.stringify(loadOptions[i]);
                });
                $.getJSON("http://mydomain.com/MyDataService", params)
                    .done(function(result) {
                        // Here, you can perform operations unsupported by the server
                        d.resolve(result.data);
                    });
                return d.promise();
            },
            byKey: function(key) {
                const d = new $.Deferred();
                $.get('https://mydomain.com/MyDataService?id=' + key)
                    .done(function(result) {
                        d.resolve(result);
                    });
                return d.promise();
            },
            insert: function(values) {
                return $.ajax({
                    url: "http://mydomain.com/MyDataService/",
                    method: "POST",
                    data: values
                })
            }
        })
    });
});
function isNotEmpty(value) {
    return value !== undefined && value !== null && value !== "";
}
Angular
TypeScript
HTML
import { ..., Inject } from "@angular/core";
import { HttpClient, HttpClientModule, HttpParams } from "@angular/common/http";
import { DxSelectBoxModule } from "devextreme-angular";
import DataSource from "devextreme/data/data_source";
import CustomStore from "devextreme/data/custom_store";
import "rxjs/add/operator/toPromise";
// ...
export class AppComponent {
    selectBoxData: any = {};
    constructor(@Inject(HttpClient) httpClient: HttpClient) {
        function isNotEmpty(value: any): boolean {
            return value !== undefined && value !== null && value !== "";
        }
        this.selectBoxData = new DataSource({
            store: new CustomStore({
                key: "ID",
                load: (loadOptions) => {
                    let params: HttpParams = new HttpParams();
                    [
                        "skip",     
                        "take",  
                        "sort", 
                        "filter", 
                        "searchExpr",
                        "searchOperation",
                        "searchValue",
                        "group", 
                    ].forEach(function(i) {
                        if(i in loadOptions && isNotEmpty(loadOptions[i])) 
                            params = params.set(i, JSON.stringify(loadOptions[i]));
                    });
                    return httpClient.get("http://mydomain.com/MyDataService", { params: params })
                        .toPromise()
                        .then(result => {
                            // Here, you can perform operations unsupported by the server
                            return result.data;
                        });
                },
                byKey: function(key) {
                    return httpClient.get('https://mydomain.com/MyDataService?id=' + key)
                        .toPromise();
                },
                insert: function(values) {
                    return httpClient.post('http://mydomain.com/MyDataService', values)
                        .toPromise();
                }
            })
        });
    }
}
@NgModule({
     imports: [
         // ...
         DxSelectBoxModule,
         HttpClientModule 
     ],
     // ...
 })
<dx-select-box
    [dataSource]="selectBoxData">
</dx-select-box>
Vue
<template>
    <DxSelectBox ... 
        :data-source="dataSource" />
</template>
<script>
import DxSelectBox from "devextreme-vue/select-box";
import CustomStore from "devextreme/data/custom_store";
import 'whatwg-fetch';
// ...
function isNotEmpty(value) {
    return value !== undefined && value !== null && value !== "";
}
function handleErrors(response) {
    if (!response.ok)
        throw Error(response.statusText);
    return response;
}
const selectBoxDataSource = {
    store: new CustomStore({
        key: "ID",
        load: (loadOptions) => {
            let params = "?";
            [
               "skip",     
                "take",  
                "sort", 
                "filter", 
                "searchExpr",
                "searchOperation",
                "searchValue",
                "group"
            ].forEach(function(i) {
                if(i in loadOptions && isNotEmpty(loadOptions[i])) 
                    params += `${i}=${JSON.stringify(loadOptions[i])}&`;
            });
            params = params.slice(0, -1);
            return fetch(`https://mydomain.com/MyDataService${params}`)
                .then(handleErrors)
                .then(response => response.json())
                .then((result) => {
                    return result.data;
                });
        },
        byKey: (key) => {
            return fetch(`https://mydomain.com/MyDataService?id=${key}`)
                    .then(handleErrors);
        },
        insert: (values) => {
            return fetch("https://mydomain.com/MyDataService", {
                method: "POST",
                body: JSON.stringify(values),
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(handleErrors);
        }
    })
}
export default {
    // ...
    data() {
        return {
            dataSource: selectBoxDataSource
        };
    },
    components: {
        // ...
        DxSelectBox
    }
}
</script>
React
import React from "react";
import SelectBox from "devextreme-react/select-box";
import CustomStore from "devextreme/data/custom_store";
import 'whatwg-fetch';
// ...
function isNotEmpty(value) {
    return value !== undefined && value !== null && value !== "";
}
function handleErrors(response) {
    if (!response.ok) 
        throw Error(response.statusText);
    return response;
}
const selectBoxDataSource = {
    store: new CustomStore({
        key: "ID",
        load: (loadOptions) => {
            let params = "?";
            [
               "skip",     
                "take",  
                "sort", 
                "filter", 
                "searchExpr",
                "searchOperation",
                "searchValue",
                "group"
            ].forEach(function(i) {
                if(i in loadOptions && isNotEmpty(loadOptions[i])) 
                    params += `${i}=${JSON.stringify(loadOptions[i])}&`;
            });
            params = params.slice(0, -1);
            return fetch(`https://mydomain.com/MyDataService${params}`)
                .then(handleErrors)
                .then(response => response.json())
                .then((result) => {
                    return result.data;
                });
        },
        byKey: (key) => {
            return fetch(`https://mydomain.com/MyDataService?id=${key}`)
                    .then(handleErrors);
        },
        insert: (values) => {
            return fetch("https://mydomain.com/MyDataService", {
                method: "POST",
                body: JSON.stringify(values),
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(handleErrors);
        }
    })
}
class App extends React.Component {
    render() {
        return (
            <SelectBox ...
                dataSource={selectBoxDataSource}>
            </SelectBox>
        );
    }
}
export default App;
See Also