Category / Section
How to change background color of row on checkbox column click in Javascript TreeGrid?
2 mins read
This article explains how to change background color of row on checkbox column click in JavaScript TreeGrid component.
The checkboxChange event triggers when the check box state change in checkbox column. You can use this event to change the background color and text color of the row based on e-check e-uncheck / e-stop checkbox states.
To change background color of the row, follow the given steps,
- Declare the function checkboxChange and write the below conditions.
- Select e-check class using queryselector and add class bgcolor class if the arg.checked value is true and remove this class when the value is false.
index.js
var treeGridObj = new ej.treegrid.TreeGrid({ checkboxChange(args) { if (args.checked) { setTimeout(() => { const checkedRows = this.element.querySelectorAll('.e-check'); Array.from(checkedRows).map((row) => { row.closest('tr').classList.add('bgcolor'); }); }, 0); } else { setTimeout(() => { const coloredRows = this.element.querySelectorAll('.bgcolor'); Array.from(coloredRows).map((row) => { if (row.querySelector('.e-uncheck') || row.querySelector('.e-stop')) { row.classList.remove('bgcolor'); } }); }, 0); } }, dataSource: window.sampleData, childMapping: 'subtasks', height: 350, allowSelection: true, enableCollapseAll: 'true', autoCheckHierarchy: 'true', selectionSettings: { persistSelection: true }, treeColumnIndex: 1, columns: [ { field: 'taskID', headerText: 'Task ID', isPrimaryKey: true, width: 70, textAlign: 'Right', }, { field: 'taskName', headerText: 'Task Name', showCheckbox: 'true', width: 200, textAlign: 'Left', }, { field: 'priority', headerText: 'Priority', width: 90 }, ], }); treeGridObj.appendTo('#TreeGrid');
index.html
<style> .bgcolor td { color: aliceblue !important; background-color: cadetblue !important; } </style>
Result
Refer to our documentation and online samples for more features. If you have any queries, please let us know in the comments below. You can also contact us through our Support forum or Support ticket. We are happy to assist you!