Row selection in Vue Grid component
22 Mar 202315 minutes to read
Select row at initial rendering
To select a row at initial rendering, set the selectedRowIndex
value.
<template>
<div id="app">
<ejs-grid :dataSource='data' :selectedRowIndex=1 height='315px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data
};
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Get selected row indexes
You can get the selected row indexes by using the getSelectedRowIndexes
method.
<template>
<div id="app">
<ejs-grid ref='grid' :dataSource='data' height='315px' :rowSelected='rowSelected'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data
};
},
methods: {
rowSelected: function(args) {
let selectedrowindex = this.$refs.grid.getSelectedRowIndexes(); // Get the selected row indexes.
alert(selectedrowindex); // To alert the selected row indexes.
let selectedrecords = this.$refs.grid.getSelectedRecords(); // Get the selected records.
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Simple multiple row selection
You can select multiple rows by clicking on rows one by one. This will not deselect the previously selected rows. To deselect the previously selected row, you can click on the selected row. You can enable this behavior by using selectionSettings.enableSimpleMultiRowSelection
property.
<template>
<div id="app">
<ejs-grid :dataSource='data' :selectionSettings='selectionOptions' height='315px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
selectionOptions: { type: 'Multiple', enableSimpleMultiRowSelection: true }
};
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Toggle selection
The Toggle selection allows to perform selection and unselection of the particular row or cell or column. To enable toggle selection, set enableToggle
property of the selectionSettings as true. If you click on the selected row or cell or column then it will be unselected and vice versa.
<template>
<div id="app">
<ejs-grid :dataSource='data' :selectionSettings='selectionOptions' height='315px'>
<e-columns>
<e-column type='checkbox' width='50'></e-column>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
selectionOptions: { enableToggle: true}
};
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
If multi selection is enabled, then first click on any selected row (without pressing Ctrl key), it will clear the multi selection and in second click on the same row, it will be unselected.
Clear selection programmatically
You can clear the Grid selection programmatically by using the clearSelection method.
In the demo below, we initially selected the third row using selectedRowIndex. You can clear this selection by calling the clearSelection method in the button click event.
<template>
<div id="app">
<ejs-button id='clear' cssClass='e-flat' @click.native='clear'>Clear Selection</ejs-button>
<ejs-grid ref='grid' :dataSource='data' :allowPaging='true' :allowSelection='true' :pageSettings='pageSettings' :selectedRowIndex='2' :selectionSettings='selectionOptions'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' :isPrimaryKey='true' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='ShipCountry' headerText='Ship Country' width=130></e-column>
<e-column field='Freight' headerText='Freight' format='C2' width=100></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Page, Selection } from "@syncfusion/ej2-vue-grids";
import { ButtonPlugin } from "@syncfusion/ej2-vue-buttons";
import { cdata } from './datasource.js';
Vue.use(GridPlugin);
Vue.use(ButtonPlugin);
export default {
data() {
return {
data: cdata,
selectionOptions: { type: 'Multiple' },
pageSettings: { pageSize: 5 }
};
},
methods: {
clear: function (args) {
var gridObj = this.$refs.grid.ej2Instances;
gridObj.clearSelection();
}
},
provide: {
grid: [Page, Selection]
}
}
</script>
<style>
@import "https://ej2.syncfusion.com/vue/documentation./node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Get selected records on various pages
Enabling the selectionSettings.persistSelection property will persist the selection in all Grid operations.
So the selection will be maintained on every page even after navigating to another page.
You can get the selected records using the getSelectedRecords method.
<template>
<div id="app">
<ejs-button id='select' cssClass='e-flat' @click.native='select'>Selected Records</ejs-button>
<ejs-grid ref='grid' :dataSource='data' :allowPaging='true' :pageSettings='pageSettings' :selectedRowIndex='2' :selectionSettings='selectionOptions'>
<e-columns>
<e-column type='checkbox' headerText='Check Box' width=50></e-column>
<e-column field='OrderID' headerText='Order ID' :isPrimaryKey='true' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='ShipCountry' headerText='Ship Country' width=130></e-column>
<e-column field='Freight' headerText='Freight' format='C2' width=100></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Page, Selection } from "@syncfusion/ej2-vue-grids";
import { ButtonPlugin, CheckBoxPlugin } from "@syncfusion/ej2-vue-buttons";
import { cdata } from './datasource.js';
Vue.use(GridPlugin);
Vue.use(ButtonPlugin, CheckBoxPlugin);
export default {
data() {
return {
data: cdata,
selectionOptions: { persistSelection: true },
pageSettings: { pageSize: 5 }
};
},
methods: {
select: function (args) {
var gridObj = this.$refs.grid.ej2Instances;
let selectedrecords = gridObj.getSelectedRecords(); // get the selected records.
let selectedRecordsCount = selectedrecords.length;
alert(selectedRecordsCount); // to alert the selected records count.
}
},
provide: {
grid: [Page, Selection]
}
}
</script>
<style>
@import "https://ej2.syncfusion.com/vue/documentation./node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
</style>
To persist the grid selection, it is necessary to define any one of the columns as a primary key using the columns.isPrimaryKey property.