DevExtreme Vue - Local Array

To bind a UI component to a local array, pass this array to the UI component's dataSource property. We recommend that you also set the keyExpr property OR the valueExpr and displayExpr properties, depending on your UI component:

App.vue
data.js
  • <template>
  • <div>
  • <DxDataGrid
  • :data-source="employees"
  • key-expr="ID"
  • />
  • <DxSelectBox
  • :data-source="employees"
  • value-expr="ID"
  • :display-expr="getDisplayExpr"
  • />
  • </div>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxDataGrid from 'devextreme-vue/data-grid';
  • import DxSelectBox from 'devextreme-vue/select-box';
  •  
  • import service from './data.js';
  •  
  • export default {
  • components: {
  • DxDataGrid,
  • DxSelectBox
  • },
  • data() {
  • const employees = service.getEmployees();
  • return {
  • employees
  • }
  • },
  • methods: {
  • getDisplayExpr(item) {
  • return item && item.FirstName + ' ' + item.LastName;
  • }
  • }
  • }
  • </script>
  • const employees = [
  • { ID: 1, FirstName: 'Sandra', LastName: 'Johnson' },
  • { ID: 2, FirstName: 'James', LastName: 'Scott' },
  • { ID: 3, FirstName: 'Nancy', LastName: 'Smith' }
  • ];
  •  
  • export default {
  • getEmployees() {
  • return employees;
  • }
  • }

If you plan to update the data or need to handle data-related events, wrap the array in an ArrayStore. You can use the store's key property instead of the UI component's keyExpr or valueExpr. You can further wrap the ArrayStore in a DataSource if you need to filter, sort, group, and otherwise shape the data.

The following example declares an ArrayStore, wraps it in a DataSource, and binds the DataGrid and SelectBox UI components to this DataSource:

App.vue
  • <template>
  • <div>
  • <DxDataGrid
  • :data-source="employeesDataSource"
  • />
  • <DxSelectBox
  • :data-source="employeesDataSource"
  • :display-expr="getDisplayExpr"
  • />
  • </div>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxDataGrid from 'devextreme-vue/data-grid';
  • import DxSelectBox from 'devextreme-vue/select-box';
  • import ArrayStore from 'devextreme/data/array_store';
  • import DataSource from 'devextreme/data/data_source';
  •  
  • import service from './data.js';
  •  
  • const employeesStore = new ArrayStore({
  • data: service.getEmployees(),
  • key: 'ID',
  • onLoaded: function() {
  • // ...
  • }
  • });
  •  
  • const employeesDataSource = new DataSource({
  • store: employeesStore,
  • sort: 'LastName'
  • });
  •  
  • export default {
  • components: {
  • DxDataGrid
  • },
  • data() {
  • return {
  • employeesDataSource
  • }
  • },
  • methods: {
  • getDisplayExpr(item) {
  • return item && item.FirstName + ' ' + item.LastName;
  • }
  • }
  • }
  • </script>
NOTE
If you pass a JavaScript array to a UI component's dataSource property, the UI component automatically places it in an ArrayStore that is wrapped in a DataSource. You can then call the getDataSource() method to get this DataSource.