Vue DataGrid - Selection

NOTE
You must specify the DataGrid's keyExpr or the Store's key property to ensure that selection works properly.
See Also

User Interaction

The DataGrid UI component supports single and multiple row selection. Use the selection.mode property to change the current mode.

App.vue
  • <template>
  • <DxDataGrid ... >
  • <DxSelection mode="single" /> <!-- "multiple" | "none" -->
  • </DxDataGrid>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxDataGrid, {
  • DxSelection
  • } from 'devextreme-vue/data-grid';
  •  
  • export default {
  • components: {
  • DxDataGrid,
  • DxSelection
  • }
  • }
  • </script>

In the single mode, only one row can be selected at a time, while in the multiple mode, several rows can be selected with check boxes that appear in the selection column.

DevExtreme HTML5 JavaScript jQuery Angular Knockout UI component DataGrid Sorting

The check box in the column's header selects all rows or only the currently rendered ones, depending on the selectAllMode. Note that clicking this check box selects/deselects only those rows that meet filtering conditions if a filter is applied.

App.vue
  • <template>
  • <DxDataGrid ... >
  • <DxSelection
  • mode="single"
  • select-all-mode="page" /> <!-- or "allPages" -->
  • </DxDataGrid>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxDataGrid, {
  • DxSelection
  • } from 'devextreme-vue/data-grid';
  •  
  • export default {
  • components: {
  • DxDataGrid,
  • DxSelection
  • }
  • }
  • </script>

You can prevent users from selecting all rows by setting the selection.allowSelectAll property to false.

App.vue
  • <template>
  • <DxDataGrid ... >
  • <DxSelection
  • mode="multiple"
  • :allow-select-all="false"
  • />
  • </DxDataGrid>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxDataGrid, {
  • DxSelection
  • } from 'devextreme-vue/data-grid';
  •  
  • export default {
  • components: {
  • DxDataGrid,
  • DxSelection
  • }
  • }
  • </script>

The showCheckBoxesMode property specifies when the UI component renders check boxes in the selection column. For example, the following code tells the UI component to never render them, though a user can still select rows using keyboard shortcuts:

App.vue
  • <template>
  • <DxDataGrid ... >
  • <DxSelection
  • mode="multiple"
  • :show-check-boxes-mode="none" /> <!-- or "onClick" | "onLongTap" | "always" -->
  • </DxDataGrid>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxDataGrid, {
  • DxSelection
  • } from 'devextreme-vue/data-grid';
  •  
  • export default {
  • components: {
  • DxDataGrid,
  • DxSelection
  • }
  • }
  • </script>

Single Selection Demo Multiple Selection Demo

See Also

API

Initial and Runtime Selection

Use the selectedRowKeys property to select rows initially. Note that to access a row by its key, you should specify the DataGrid's keyExpr or the Store's key property.

App.vue
  • <template>
  • <DxDataGrid ...
  • :data-source="dataGridDataSource"
  • :selected-row-keys="[1, 5, 18]">
  • </DxDataGrid>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxDataGrid from 'devextreme-vue/data-grid';
  • import DataSource from 'devextreme/data/data_source';
  • import 'devextreme/data/array_store';
  • // or
  • // import 'devextreme/data/odata/store';
  • // import 'devextreme/data/custom_store';
  •  
  • const dataGridDataSource = new DataSource({
  • store: {
  • // ...
  • key: 'id'
  • }
  • });
  •  
  • export default {
  • components: {
  • DxDataGrid
  • },
  • data() {
  • return {
  • dataGridDataSource
  • }
  • }
  • }
  • </script>

The DataGrid provides two methods that select rows at runtime: selectRows(keys, preserve) and selectRowsByIndexes(indexes). They both clear the previous selection by default, although with the selectRows(keys, preserve) method you can keep it if you pass true as the preserve parameter. Before selecting a row, you can call the isRowSelected(key) method to check if this row is not already selected.

App.vue
  • <template>
  • <DxDataGrid ...
  • :selected-row-keys.sync="selectedRowKeys"
  • @content-ready="selectFirstRow">
  • </DxDataGrid>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxDataGrid from 'devextreme-vue/data-grid';
  •  
  • export default {
  • components: {
  • DxDataGrid
  • },
  • data() {
  • return {
  • selectedRowKeys: []
  • }
  • },
  • methods: {
  • selectFirstRow(e) {
  • const rowKey = e.component.getKeyByRowIndex(0);
  • this.selectedRowKeys.push(rowKey);
  • }
  • }
  • }
  • </script>

To select all rows at once, call the selectAll() method.

App.vue
  • <template>
  • <DxDataGrid ...
  • :ref="dataGridRefKey">
  • </DxDataGrid>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxDataGrid from 'devextreme-vue/data-grid';
  •  
  • const dataGridRefKey = 'my-data-grid';
  •  
  • export default {
  • components: {
  • DxDataGrid
  • },
  • data() {
  • return {
  • dataGridRefKey
  • }
  • },
  • methods: {
  • selectAllRows() {
  • this.dataGrid.selectAll();
  • }
  • },
  • computed: {
  • dataGrid: function() {
  • return this.$refs[dataGridRefKey].instance;
  • }
  • }
  • }
  • </script>

View Demo

Call the getSelectedRowKeys() or getSelectedRowsData() method to get the selected row's keys or data.

App.vue
  • <template>
  • <DxDataGrid ...
  • :ref="dataGridRefKey">
  • </DxDataGrid>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxDataGrid from 'devextreme-vue/data-grid';
  •  
  • const dataGridRefKey = 'my-data-grid';
  •  
  • export default {
  • components: {
  • DxDataGrid
  • },
  • data() {
  • return {
  • dataGridRefKey
  • }
  • },
  • methods: {
  • getSelectedRowKeys() {
  • return this.dataGrid.getSelectedRowKeys();
  • },
  • getSelectedRowsData() {
  • return this.dataGrid.getSelectedRowsData();
  • }
  • },
  • computed: {
  • dataGrid: function() {
  • return this.$refs[dataGridRefKey].instance;
  • }
  • }
  • }
  • </script>
See Also

Clear Selection Settings

Call the deselectRows(keys) method to clear the selection of specific rows.

App.vue
  • <template>
  • <DxDataGrid ...
  • :selected-row-keys.sync="selectedRowKeys">
  • </DxDataGrid>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxDataGrid from 'devextreme-vue/data-grid';
  •  
  • export default {
  • components: {
  • DxDataGrid
  • },
  • data() {
  • return {
  • selectedRowKeys: []
  • }
  • },
  • methods: {
  • deselectRows(keys) {
  • let selectedRowKeys = this.selectedRowKeys;
  • keys.forEach(function(item) {
  • const index = selectedRowKeys.indexOf(item);
  • if (index !== -1) {
  • selectedRowKeys.splice(index, 1);
  • }
  • });
  • }
  • }
  • }
  • </script>

Call the clearSelection() method to clear selection of all rows. If you apply a filter and want to keep the selection of invisible rows that do not meet the filtering conditions, use the deselectAll() method. Also call this method to clear selection depending on the selectAllMode.

App.vue
  • <template>
  • <DxDataGrid ...
  • ref="dataGrid"
  • :selected-row-keys.sync="selectedRowKeys">
  • </DxDataGrid>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxDataGrid from 'devextreme-vue/data-grid';
  •  
  • export default {
  • components: {
  • DxDataGrid
  • },
  • data() {
  • return {
  • selectedRowKeys: []
  • }
  • },
  • methods: {
  • deselectAllRows() {
  • this.selectedRowKeys.length = 0;
  • },
  • deselectVisibleRows() {
  • this.$refs['dataGrid'].instance.deselectAll();
  • }
  • }
  • }
  • </script>

View Demo

See Also

Events

The DataGrid UI component raises the selectionChanged event when a row is selected, or the selection is cleared. If the function that handles this event is going to remain unchanged, assign it to the onSelectionChanged property when you configure the UI component. Note that information on selected and deselected rows is passed to the handler only when selection is not deferred.

App.vue
  • <template>
  • <DxDataGrid ...
  • @selection-changed="onSelectionChanged">
  • </DxDataGrid>
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxDataGrid from 'devextreme-vue/data-grid';
  •  
  • export default {
  • components: {
  • DxDataGrid
  • },
  • methods: {
  • onSelectionChanged(e) {
  • const currentSelectedRowKeys = e.currentSelectedRowKeys;
  • const currentDeselectedRowKeys = e.currentDeselectedRowKeys;
  • const allSelectedRowKeys = e.selectedRowKeys;
  • const allSelectedRowsData = e.selectedRowsData;
  • // ...
  • }
  • }
  • }
  • </script>

If you are going to change the event handler at runtime, or if you need to attach several handlers to the event, subscribe to it using the on(eventName, eventHandler) method. This approach is more typical of jQuery.

JavaScript
  • var selectionChangedEventHandler1 = function(e) {
  • // First handler of the "selectionChanged" event
  • };
  •  
  • var selectionChangedEventHandler2 = function(e) {
  • // Second handler of the "selectionChanged" event
  • };
  •  
  • $("#dataGridContainer").dxDataGrid("instance")
  • .on("selectionChanged", selectionChangedEventHandler1)
  • .on("selectionChanged", selectionChangedEventHandler2);
See Also