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:
- <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:
- <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>
If you have technical questions, please create a support ticket in the DevExpress Support Center.