Check box selection in Vue Treegrid component
29 Dec 202514 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.checkboxOnlyproperty to true.
Selection can be persisted on all the operations
usingselectionSettings.persistSelectionproperty.
For persisting selection on the TreeGrid, any one of the column should be defined as a primary key
usingcolumns.isPrimaryKeyproperty.
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 thecheckboxMode. In this mode, user can select multiple rows by clicking rows one by one. -
ResetOnRowClick: InResetOnRowClickmode, 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.
Conditional row selection
The TreeGrid supports conditional row selection through the isRowSelectable callback. This allows selection to be controlled by custom business logic, ensuring that only rows meeting specific conditions can be selected. The callback receives each row’s data and returns “true” to allow selection or “false” to prevent it.
Local data: The callback runs once when the TreeGrid initializes and evaluates all records because the full dataset is already available on the client.
Remote data: The callback runs only for the rows displayed on the current page when the TreeGrid first loads. It runs again whenever the TreeGrid fetches new data such as during paging, filtering, or sorting to re-evaluate the newly visible rows.
In the following sample, selection is disabled for rows where the “Progress” column has the value “Completed”.
<template>
<div id="app">
<ejs-treegrid
:dataSource="data"
idMapping="TaskID"
parentIdMapping="ParentID"
:treeColumnIndex="1"
:enableVirtualization="true"
:selectionSettings="selectionOptions"
:isRowSelectable="isRowSelectable"
:height="600"
>
<e-columns>
<e-column type="checkbox" width= '90'></e-column>
<e-column field='Task' headerText='Task' width='300'></e-column>
<e-column field='TaskID' isPrimaryKey="true" visible="false"></e-column>
<e-column field='AssignedTo' headerText='Assigned To' width= '140'></e-column>
<e-column field='StartDate' headerText='Start' format='yMd' width= '180'></e-column>
<e-column field='DueDate' headerText='Due' format='yMd' width= '180'></e-column>
<e-column field='Priority' headerText='Priority' width= '90'></e-column>
<e-column field='Progress' headerText='Progress' width= '110'></e-column>
</e-columns>
</ejs-treegrid>
</div>
</template>
<script setup>
import { ref, provide } from 'vue';
import {
TreeGridComponent,
VirtualScroll,
} from '@syncfusion/ej2-vue-treegrid';
import { taskData } from './data-source';
import { TreeGridComponent as EjsTreegrid, ColumnDirective as EColumn, ColumnsDirective as EColumns,
} from "@syncfusion/ej2-vue-treegrid";
const data = ref(taskData);
const selectionOptions = ref({
persistSelection: true,
});
const isRowSelectable = (data, columns) => {
return data.Progress !== 'Completed';
};
provide('treegrid', [VirtualScroll]);
</script><template>
<div id="app">
<ejs-treegrid :dataSource='data' idMapping='TaskID' parentIdMapping='ParentID' :treeColumnIndex='1'
:enableVirtualization="true" :selectionSettings='selectionOptions' :isRowSelectable="isRowSelectable"
:height="600">
<e-columns>
<e-column type="checkbox" width= '90'></e-column>
<e-column field='Task' headerText='Task' width='300'></e-column>
<e-column field='TaskID' isPrimaryKey="true" visible="false"></e-column>
<e-column field='AssignedTo' headerText='Assigned To' width= '140'></e-column>
<e-column field='StartDate' headerText='Start' format='yMd' width= '180'></e-column>
<e-column field='DueDate' headerText='Due' format='yMd' width= '180'></e-column>
<e-column field='Priority' headerText='Priority' width= '90'></e-column>
<e-column field='Progress' headerText='Progress' width= '110'></e-column>
</e-columns>
</ejs-treegrid>
</div>
</template>
<script>
import { TreeGridComponent, VirtualScroll, ColumnDirective, ColumnsDirective } from "@syncfusion/ej2-vue-treegrid";
import { taskData } from "./datasource.js";
export default {
name: "App",
components: {
"ejs-treegrid": TreeGridComponent,
"e-columns": ColumnsDirective,
"e-column": ColumnDirective,
},
data() {
return {
data: taskData,
selectionOptions: { persistSelection: true }
};
},
provide: {
treegrid: [VirtualScroll]
},
methods: {
isRowSelectable(data, columns) {
return data.Progress !== 'Completed';
}
}
}
</script>