cellstyle的AG网格动态颜色值

AG grid dynamic color value for cellstyle

Ag grid column definition


 columnDefs = [
                    { headerName: "Status", field: "statusCode", cellStyle: {'background-color': 'green'}}, }

                ]

它不是硬编码颜色,而是从服务器获取。

columnDefs = [
            {  headerName: "Status", field: "statusCode", cellStyle: {'background-color': ['colorVal']}}, }

        ]

colorVal 将具有类似红色或绿色的值,但上述语法不起作用。

这是我设置到 ag-grid 的 rowData 的 json。

 {"statusCode":101,"colorVal":green}

我可以设置使用还是使用this.gridOptions。 ?

试试这个

let data= {"statusCode":101,"colorVal":green}

columnDefs = [
        {  headerName: "Status", field: "statusCode", cellStyle: {'background-color': data.colorVal}}, }
    ]

这里 let 是一个局部变量,你必须在你的方法范围内使用它

PS:- 评论太长所以发布这个作为答案。

@Basavaraj 感谢提示。

columnDefs = [
                        { headerName: "Status", field: "statusCode",cellStyle: this.cellStyling},

                ]

调用此方法动态显示样式

cellStyling(params:any){     
       return {'background-color': params.data.colourCode};
  }