如何更改 ag-grid 单元格的颜色以动态更改数据

新手上路,请多包涵

我有一个从动态变化的数据加载的表。它每 5 秒刷新一次。我正在使用这个例子使用 ag-grid: https ://www.ag-grid.com/javascript-grid-sorting/index.php

是否可以更改值发生变化的单元格的颜色,例如假设单元格值为 100 并且它变为(小于此值,即 <100),因此使单元格变为红色,id 变大,变为绿色。我正在尝试使用此示例来做到这一点: https ://www.ag-grid.com/javascript-grid-cell-styling/index.php

但我不明白该怎么做。

更新:我这样做,但它没有改变颜色:

 var columnDefs = [
    {headerName: "Arr Px Slippage", field: "total_wt_arr_slp", width: 100, newValueHandler: compareValues},
    {headerName: "IVWAP Slippage", field: "total_wt_ivwap_slp", width: 100}

];

function compareValues(params) {
    if (params.oldValue > params.newValue){
    return {color: 'green', backgroundColor: 'black'};
    console.log(params.newValue);

    }
    if (params.oldValue < params.newValue){
    return {color: 'red', backgroundColor: 'black'};
    }
}

原文由 shek 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
1 个回答

这是代码片段,您可以在其中更改 ag 网格单元格文本的颜色和背景颜色。

  var colDef = {
            name: 'Dynamic Styles',
            field' 'dummyfield',
            cellStyle: function(params) {
                if (params.node.value=='Police') {
        //Here you can check the value and based on that you can change the color
                    //mark police cells as red
                    return {color: 'red', backgroundColor: 'green'};
                } else {
                    return null;
                }
            }
        }

原文由 Rajanikanta Pradhan 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题