You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ag-grid:无需Cell Class Rules,能否用类rowClassRules方式标记修改单元格

How to Highlight Modified Cells Without Per-Column cellClassRules in AG Grid?

Great question! When you're dealing with hundreds of columns, setting up cellClassRules for each individual column isn't just tedious—it's totally unmanageable. The good news is AG Grid has a clean, scalable solution that works similarly to rowClassRules but targets specific cells.

The Solution: Use defaultColDef with a cellClass Callback

You can define a global cell class rule in the defaultColDef configuration, which applies to every column in your grid automatically. This callback checks if the current column's field exists in the row's modifiedKeys array, and applies a custom style class if it does.

Here's how to implement it:

1. Configure the Grid Options

const gridOptions = {
  rowData: [
    { "uniqueIdentifier": "12345", "identifier": "UJHU", "latitude": 33.68131385650486, "longitude": -83.36814721580595, "cycle": "1", "speedLimit": "255", "customerCode": "Standard", "altitude" : 12345, "modifiedKeys": [ "speedLimit", "altitude" ] },
    { "uniqueIdentifier": "13434", "identifier": "UJHU", "latitude": 93.68131385650486, "longitude": -33.36814721580595, "cycle": "2", "speedLimit": "255", "customerCode": "Standard", "altitude" : 12345, "modifiedKeys": [ "cycle", "latitude" ] }
  ],
  columnDefs: [
    // Your hundreds of column definitions go here—no need to add cellClassRules to any!
    { field: "uniqueIdentifier" },
    { field: "identifier" },
    { field: "latitude" },
    { field: "longitude" },
    { field: "cycle" },
    { field: "speedLimit" },
    { field: "customerCode" },
    { field: "altitude" }
  ],
  defaultColDef: {
    // This callback runs for every cell in the grid
    cellClass: (params) => {
      // Guard against undefined data or modifiedKeys to avoid runtime errors
      if (!params.data || !params.data.modifiedKeys) return '';
      
      // Check if the current column's field is in the modifiedKeys array
      return params.data.modifiedKeys.includes(params.colDef.field) 
        ? 'modified-cell' 
        : '';
    }
  }
};

2. Add CSS for Highlighting

Define a CSS class to style modified cells (customize the colors to fit your app's design):

.modified-cell {
  background-color: #fff3cd; /* Soft yellow to flag changes */
  font-weight: 500; /* Optional: Bold text for better visibility */
}

Why This Works

  • Scalability: The defaultColDef applies this rule to every column automatically—no need to update hundreds of column definitions.
  • Flexibility: You can easily extend the callback to return different classes for different modification types (e.g., distinguish between new vs. updated values).
  • Performance: AG Grid runs this callback efficiently during rendering, so it won't slow down even with large datasets or many columns.

Quick Notes

  • Make sure the field value in your columnDefs exactly matches the strings in the modifiedKeys array (it's case-sensitive!).
  • The guard clauses in the callback prevent errors if a row has no modifiedKeys or if params.data is undefined (like during grid initialization).

内容的提问来源于stack exchange,提问作者Jackson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 17:37:43