Check box selection in Vue Treegrid component
11 Jun 20249 minutes to read
Checkbox Selection provides an option to select multiple TreeGrid records with help of checkbox in each row.
To render checkbox in each treegrid row, you need to use checkbox column with type as CheckBox
using
column type
property.
<template>
<div id="app">
<ejs-treegrid ref='treegrid' :dataSource='data' childMapping='subtasks' :treeColumnIndex='2'>
<e-columns>
<e-column type='checkbox' width='50'></e-column>
<e-column field='taskID' headerText='Task ID' width='90' textAlign='Right'></e-column>
<e-column field='taskName' headerText='Task Name' width='160'></e-column>
<e-column field='startDate' headerText='Start Date' width='90' format="yMd" textAlign='Right'></e-column>
<e-column field='duration' headerText='Duration' width='80' textAlign='Right'></e-column>
</e-columns>
</ejs-treegrid>
</div>
</template>
<script setup>
import { provide } from "vue";
import { TreeGridComponent as EjsTreegrid, Page, ColumnDirective as EColumn, ColumnsDirective as EColumns } from "@syncfusion/ej2-vue-treegrid";
import { sampleData } from "./datasource.js";
const data = sampleData;
provide('treegrid', [ Page ]);
</script>
<template>
<div id="app">
<ejs-treegrid ref='treegrid' :dataSource='data' childMapping='subtasks' :treeColumnIndex='2'>
<e-columns>
<e-column type='checkbox' width='50'></e-column>
<e-column field='taskID' headerText='Task ID' width='90' textAlign='Right'></e-column>
<e-column field='taskName' headerText='Task Name' width='160'></e-column>
<e-column field='startDate' headerText='Start Date' width='90' format="yMd" textAlign='Right'></e-column>
<e-column field='duration' headerText='Duration' width='80' textAlign='Right'></e-column>
</e-columns>
</ejs-treegrid>
</div>
</template>
<script>
import { TreeGridComponent, Page, ColumnDirective, ColumnsDirective } from "@syncfusion/ej2-vue-treegrid";
import { sampleData } from "./datasource.js";
export default {
name: "App",
components: {
"ejs-treegrid":TreeGridComponent,
"e-columns":ColumnsDirective,
"e-column":ColumnDirective,
},
data () {
return {
data: sampleData,
};
},
provide: {
treegrid: [ Page ]
}
}
</script>
By default selection is allowed by clicking a treegrid row or checkbox in that row. To allow Selection only through checkbox, you can set
selectionSettings.checkboxOnly
property to true.
Selection can be persisted on all the operations
usingselectionSettings.persistSelection
property.
For persisting selection on the TreeGrid, any one of the column should be defined as a primary key
usingcolumns.isPrimaryKey
property.
Checkbox selection mode
In checkbox selection, selection can also be done by clicking on rows. This selection provides two types of Checkbox Selection mode which can be set by using the following API,
selectionSettings.checkboxMode
. The modes are;
-
Default
: This is the default value of the checkboxMode. In this mode, user can select multiple rows by clicking rows one by one. -
ResetOnRowClick
: In ResetOnRowClick mode, when user clicks on a row it will reset previously selected row. Also you can perform multiple-selection in this mode by press and hold CTRL key and click the desired rows. To select range of rows, press and hold the SHIFT key and click the rows.
<template>
<div id="app">
<ejs-treegrid ref='treegrid' :dataSource='data' childMapping='subtasks' :treeColumnIndex='2'
:selectionSettings='selectionOptions'>
<e-columns>
<e-column type='checkbox' width='50'></e-column>
<e-column field='taskID' headerText='Task ID' width='90' textAlign='Right'></e-column>
<e-column field='taskName' headerText='Task Name' width='160'></e-column>
<e-column field='startDate' headerText='Start Date' width='90' format="yMd" textAlign='Right'></e-column>
<e-column field='duration' headerText='Duration' width='80' textAlign='Right'></e-column>
</e-columns>
</ejs-treegrid>
</div>
</template>
<script setup>
import { provide } from "vue";
import { TreeGridComponent as EjsTreegrid, Page, ColumnDirective as EColumn, ColumnsDirective as EColumns } from "@syncfusion/ej2-vue-treegrid";
import { sampleData } from "./datasource.js";
const data = sampleData;
const selectionOptions = { checkboxMode: 'ResetOnRowClick' };
provide('treegrid', [ Page ]);
</script>
<template>
<div id="app">
<ejs-treegrid ref='treegrid' :dataSource='data' childMapping='subtasks' :treeColumnIndex='2'
:selectionSettings='selectionOptions'>
<e-columns>
<e-column type='checkbox' width='50'></e-column>
<e-column field='taskID' headerText='Task ID' width='90' textAlign='Right'></e-column>
<e-column field='taskName' headerText='Task Name' width='160'></e-column>
<e-column field='startDate' headerText='Start Date' width='90' format="yMd" textAlign='Right'></e-column>
<e-column field='duration' headerText='Duration' width='80' textAlign='Right'></e-column>
</e-columns>
</ejs-treegrid>
</div>
</template>
<script>
import { TreeGridComponent, Page, ColumnDirective, ColumnsDirective } from "@syncfusion/ej2-vue-treegrid";
import { sampleData } from "./datasource.js";
export default {
name: "App",
components: {
"ejs-treegrid":TreeGridComponent,
"e-columns":ColumnsDirective,
"e-column":ColumnDirective,
},
data () {
return {
data: sampleData,
selectionOptions: { checkboxMode: 'ResetOnRowClick'}
};
},
provide: {
treegrid: [ Page ]
}
}
</script>
Checkbox Selection feature is intended for row selection only; it is not compatible with cell selection mode.