首页 > 解决方案 > 如何为 ag-grid 中的特定行着色?

问题描述

我有一个 ag 网格,我只想为一些行号着色。例如,处理后我发现我只需要为第 1、4 和 5 行着色。

我尝试了 ag-grid 的 getRowStyle 函数但徒劳无功

gridOptions.getRowStyle = function(params) {
    if (params.node.rowIndex % 2 === 0) {
    return { background: 'red' }
    }
}

标签: angularag-grid-angular

解决方案


这是直接来自文档的一些代码

如果您只需要为某些行着色。您需要知道要为哪些行着色。我假设您对此有一个状态。也许一个变量叫做indices?

indices: Array<number> = [1,4,5]; // color these rows

gridOptions.getRowStyle = (params) => { // should use params, not indices in the first braces. Binds the component to this. Can use indices in the function now
    if (this.indices.includes(params.node.rowIndex)) {
        return { background: 'red' }
    }
}

如果您需要更多帮助,则需要更具体。我不知道什么不起作用。

这是一个带有示例的 Plunkr。我不确定您是如何在项目中设置所有内容的,但您可能会获得一些关于如何修改代码以适应我从 ag-grid 文档调整的示例的想法。我希望这个例子有帮助


推荐阅读