Checkbox Selection in ASP.NET CORE Tree Grid Component

29 Dec 20256 minutes to read

Checkbox selection provides an option to select multiple treegrid records with help of checkbox in each row.

To render the checkbox in each treegrid row, you need to use checkbox column with type as checkbox using the column type property of e-treegrid-column tag helper.

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" allowPaging="true" allowSelection="true" childMapping="Children" treeColumnIndex="2">
    <e-treegrid-selectionsettings type="Multiple"></e-treegrid-selectionsettings>
    <e-treegrid-columns>
        <e-treegrid-column type="checkbox" width="50"></e-treegrid-column>
        <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="100"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="190"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText=" Start Date" textAlign="Right" format="yMd" type="date" width="120"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="110"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

NOTE

By default, selection is allowed by clicking a treegrid row or checkbox in that row. To allow selection only through checkbox, you can set the

checkboxOnly property to true.

Selection can be persisted in all the operations using the persistSelection property.

For persisting selection on the treegrid, any one of the columns should be defined as a primary key using the isPrimaryKey property of e-treegrid-column tag helper .

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 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.
<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" allowPaging="true" allowSelection="true" childMapping="Children" treeColumnIndex="1">
    <e-treegrid-selectionsettings checkboxMode="ResetOnRowClick" type="Multiple"></e-treegrid-selectionsettings>
    <e-treegrid-columns>
        <e-treegrid-column type="checkbox" width="50"></e-treegrid-column>
        <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="100"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="190"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText=" Start Date" textAlign="Right" format="yMd" type="date" width="120"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="110"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

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”.

<ejs-treegrid id="TreeGrid" dataSource="@ViewBag.datasource" enableVirtualization="true" allowSelection="true" parentIdMapping="ParentID" idMapping="TaskID" treeColumnIndex="1" isRowSelectable="isRowSelectable">
    <e-treegrid-selectionsettings pesistSelection="true"></e-treegrid-selectionsettings>
    <e-treegrid-columns>
        <e-treegrid-column type="checkbox" width="50"></e-treegrid-column>
        <e-treegrid-column field="Task" headerText="Task" width="300"></e-treegrid-column>
        <e-treegrid-column field="TaskID" visible="false" isPrimaryKey="true"></e-treegrid-column>
        <e-treegrid-column field="AssignedTo" headerText="Assigned To" width="140"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText="Start" format="yMd" type="date" width="180"></e-treegrid-column>
        <e-treegrid-column field="DueDate" headerText="Due" format="yMd" type="date" width="180"></e-treegrid-column>
        <e-treegrid-column field="Priority" headerText="Priority" width="90"></e-treegrid-column>
        <e-treegrid-column field="Progress" headerText="Status" width="90"></e-treegrid-column>   
    </e-treegrid-columns>
</ejs-treegrid>
<script>
    function isRowSelectable(data,columns) {
        return data.Progress !== 'Completed';
    }
</script>
public IActionResult Index()
{
    var tree = TreeData.GetTaskData();
    ViewBag.datasource = tree;
    return View();
}

NOTE

You can refer to our ASP.NET Core Tree Grid feature tour page for its groundbreaking feature representations. You can also explore our ASP.NET Core Tree Grid example ASP.NET Core Tree Grid example to knows how to present and manipulate data.