Bootstrap数据表:根据值更改单元格颜色

时间:2017-08-09 06:00:35

标签: jquery ajax datatables

我从服务器获取json数据并尝试显示数据表。

代码:

    var siteName = $("#stateType").val();

    $(".jqueryDataTable").DataTable({
        "sPaginationType" : "full_numbers",
        "bProcessing" : false,
        "bServerSide" : false,
        "bJQueryUI" : true,
        "bDestroy" : true,
        "sAjaxSource" : "searchState",
        "sServerMethod" : "POST",
        "fnServerParams" : function(aoData) {
            aoData.push({
                "name" : "stateType",
                "value" : siteName
            });
        },
        "aoColumns" : [ {
            "mData" : "stitcher"
        }, {
            "mData" : "state"
        }, {
            "mData" : "load"
        }, {
            "mData" : "sessionsActive"
        }, {
            "mData" : "sessionsPaused"
        }, {
            "mData" : "csmVersion"
        }, {
            "mData" : "serviceID"
        } ]
    });

我能够显示数据。但要求是我需要根据值改变状态单元格的颜色吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码。基于columns.createdCell

{
   data: 'serviceID',
   createdCell: function (td, cellData, rowData, row, col) {
                    var serviceID = cellData;
                    if (serviceID < 1) {
                        $(td).css('color', 'red')
                    }
                }
}