如何自定义角度ui网格行背景颜色?

时间:2017-10-20 15:20:02

标签: javascript css angularjs angular-ui-grid

我正在尝试根据条件更改角度ui网格行的背景颜色,因此如果riskValue为1则尝试将其设置为红色,如果值为2行,则背景颜色应为黄色。任何简单的方法来完成这项任务?

config.js

"columnDefs": [{
        "name": "Ticket Number",
        "field": "ticketNum",
        "width": 120
    },
    {
        "name": "Asset Id ",
        "field": "assetID",
        "width": 100
    },
    {
        "name": "Severity",
        "field": "severity",
        "width": 100
    },
    {
        "name": "Risk Index",
        "field": "riskIndex",
        "width": 100
    },
    {
        "name": "Risk Val",
        "field": "riskValue",
        "cellTemplate": "<div ng-if=\"row.entity.Comments_Col1 != 'none'\" title={{row.entity.riskValue}} \" ><div style='white-space:nowrap;border-bottom: 1px solid red !important; height:100%;width:100%;'>{{row.entity.riskValue}}</div></div>",
        "sort": {
            "direction": "aesc",
            "priority": 0
        },
        "width": 100,
        "visible": false
    }
],

1 个答案:

答案 0 :(得分:1)

您可以使用gridOptions.cellClass

cellClass: function(grid, row, col, rowRenderIndex, colRenderIndex) {
    if (row.entity.riskValue === 1) 
        return 'red';
    if (row.entity.riskValue === 2)
        return 'yellow';
}

其中redyellow是css类。

此方法需要将函数添加到所有columnDef,但只允许选择一些应更改背景颜色的列。

示例:http://plnkr.co/edit/Xie68a3sT9CXTdCuI3tq?p=preview