简体   繁体   English

ag-grid呈现具有背景色的行

[英]ag-grid render row with background color

I am currently working with Angular and ag-grid to display a table that contains various data, a column within this data will contain the value "Fail" or "Pass". 我目前正在使用Angular和ag-grid来显示包含各种数据的表,该数据中的列将包含值“ Fail”或“ Pass”。

I am currently looking into the ability to render the background of a row as red when a fail is detected. 我目前正在研究在检测到故障时将行背景呈现为红色的功能。

The problem i am having is that i cannot find any examples of this code. 我遇到的问题是我找不到该代码的任何示例。

I have inferred that there are a number of things i can do, specifically use multiple callbacks in the api to perform functions like this. 我已经推断出我可以做很多事情,特别是在api中使用多个回调来执行这样的功能。 But as a newbie to the field of web development (at least the front end) i am finding the exact stuff elusive in how to use these call backs. 但是,作为Web开发领域(至少是前端)的新手,我发现在使用这些回调方面难以捉摸的确切内容。

ok so, I have the following known methods: 好的,我有以下已知方法:

1) 1)

gridOptions.getRowStyle(params) {
    if (params.data.myColumnToCheck === myValueToCheck) {
        return {'background-color': 'yellow'}
    }
    return null;
}

2) processRowPostCreate callback - no known code 2)processRowPostCreate回调-未知代码

3) This is currently what i have in place and was from my head - but it is ugly code and whilst working i am sure its more expensive and poor than the actual suggestions i have seen. 3)这是我目前所掌握的,是我的脑筋-但它是丑陋的代码,在工作时,我确信它比我所看到的实际建议更昂贵,更糟糕。

vm.toggleColour = function () {
    vm.state.colour = !vm.state.colour;
    if (vm.state.colour) {
        columnDefs = [
            { headerName: "S", field: "1", width: 210, sort: "asc", cellStyle: changeRowColor },
            { headerName: "TS", field: "2", width: 170, cellStyle: changeRowColor },
            { headerName: "TC", field: "3", width: 195, sort: "asc", cellStyle: changeRowColor },
            { headerName: "H", field: "4", width: 60, cellStyle: changeRowColor },
            { headerName: "TV", field: "5", width: 85, cellStyle: changeRowColor },
            { headerName: "Verdict", field: "6", width: 60, cellStyle: changeRowColor },
            { headerName: "Message", field: "Message", width: 85, cellStyle: changeRowColor },
            { headerName: "Timestamp", field: "Timestamp", width: 175, cellStyle: changeRowColor },
            { headerName: "Audit Num", field: "AuditNum", width: 95, cellStyle: changeRowColor },
            { headerName: "Fail Reasons", field: "Reasons", width: 1430, cellStyle: changeRowColor }
        ];
    }
    else {
        columnDefs = [
            { headerName: "S", field: "1", width: 210, sort: "asc" },
            { headerName: "TS", field: "2", width: 170 },
            { headerName: "TC", field: "3", width: 195, sort: "asc" },
            { headerName: "H", field: "4", width: 60 },
            { headerName: "TV", field: "5", width: 85 },
            { headerName: "Verdict", field: "6", width: 60 },
            { headerName: "Message", field: "Message", width: 85 },
            { headerName: "Timestamp", field: "Timestamp", width: 175 },
            { headerName: "Audit Num", field: "AuditNum", width: 95 },
            { headerName: "Fail Reasons", field: "Reasons", width: 1430 }
        ];
    }
    resizeColumns();
    vm.gridOptions.api.setColumnDefs(columnDefs);
};
function changeRowColor(params) {
    if (params.node.data.FinalVerdict === 'Fail') {
        return { 'background-color': '#red' };
    }
}

Does anyone have any ideas on how to do what i am wanting without there being a really horrible bit of code like i currently have? 在没有像我目前所拥有的那样可怕的代码的情况下,没有人对如何做我想要的事情有任何想法吗?

I should say that i would prefer to be able to inject a class to the row and let css style it if possible - any ideas on how to do this? 我应该说,我希望能够向该行注入一个类,并在可能的情况下让CSS设置样式-有关如何执行此操作的任何想法? Redrawing the grid is such a pain! 重画网格是如此痛苦!

Thanks Guys 多谢你们

Try the following. 尝试以下方法。

To hook up the getRowStyle callback to the function changeRowColor getRowStyle回调连接到功能changeRowColor
add the following the line to your gridOptions object. 将以下行添加到您的gridOptions对象。

getRowStyle:changeRowColor getRowStyle:changeRowColor

Then define the changeRowColor function as you require 然后根据需要定义changeRowColor函数
eg 例如

gridOptions={
  columnDefs: blah,
  rowData: blah,
  ...
  getRowStyle:changeRowColor
}

function changeRowColor(params) {
    if (params.data.FinalVerdict === 'Fail') {
        return { 'background-color': '#red' };
    }
}

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM