I have a requirement where I need to update the background color of cells if the value of a column is different two adjacent rows. Also, I want to be able to do this for some columns only of the grid and not all the columns. (Refer Image)
What I did is:
If the values are different, I update the cellClass property of that particular column in the node.
this.gridApi.forEachNode(function (node) { if (node.rowIndex + 1 < currDetailGridData.length) { if (node.data['ColumnKey'] != currDetailGridData[node.rowIndex + 1]['ColumnKey']) { var nodeToHighlight = node.columnController.gridColumns.find(x => x.colId = 'ColumnKey'); nodeToHighlight.colDef.cellClass += 'bgColorRed'; } } });
However, this doesn't seem to be working. Need your help on identifying, what wrong am I doing here?
Might be you are executing it exactly after rowData
binding inside onGridReady
. If so, grid
need to finalize the data-rendering process, so you can use the hack for it wrapping method in setTimeout
, but I do not recommend to use this case.
setTimeout(() => {
this.gridApi.forEachNode(node =>{
...
})
}, 100);
Another way is to prepare columnDef
first, and then use setColumnDefs
API method to inform the grid
, to avoid a loop check.
And the last one, I recommend using cellClass
binding as function
in columnDef
cellClass: this.handleCellClass.bind(this)
...
handleCellClass(params){
if (params.node.rowIndex + 1 < this.rowData.length) {
if (params.node.data['...'] != this.rowData[params.node.rowIndex + 1]['...'])
return 'bgColorRed';
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.