如何根据列值将样式应用于 Jquery 数据表列

How to apply style to the Jquery datatable column depending on the column value

http://jsfiddle.net/ktdj3u9r/5/

我正在使用 Jquery DataTable 以表格格式显示数据。

我的需求是,如果数量字段大于100000,我想用绿色显示

这是我的代码

var dataSet = 
    [
    [
        "1441.75",
        "238469"
    ],
    [
        "1614.45",
        "327663"

    ],
    [
        "834.15",
        "1583726"
    ],
    [
        "2261.85",
        "1062354"
    ],
    [
        "444.10",
        "99399"

    ]
];

var array_names = ["A", "B", "C", "D", "E"];

for(var key in dataSet) {
    if(dataSet.hasOwnProperty(key)) {
        //dataSet[key].unshift(array_names[key]);
        dataSet[key].splice(0,0,array_names[key]);
    }
} 

  $(function()
  {

        $('#allwl').dataTable( {
                  "iDisplayLength": -1,
        "data": dataSet,
        "columns": [
            { "title": "Name" },
            { "title": "Price" },
    {
   "title": "Quantity" ,
     mRender: function(data, type, row){
        var quantity = row[2] ;
        return quantity;
     }
    }
         ]
    } );   

  })

你能告诉我怎么做吗??

为此使用 fnRowCallback

"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
         if(aData[2] > 100000){
             $('td:eq(2)', nRow).addClass("td-green");
         }
  }

这会将 class td-green 添加到任何 > 100000

的值

已更新 fiddle:https://jsfiddle.net/markps/ktdj3u9r/6/

在您的数量声明中使用 createdCell 回调:

...
"columns": [
     { "title": "Name" },
     { "title": "Price" },
     { "title": "Quantity" ,
        mRender: function(data, type, row){
           var quantity = row[2] ;
           return quantity;
        },
        createdCell: function (td, cellData, rowData, row, col) {
           if (cellData>100000) $(td).css('color', 'green');
        }
     }
]
...

分叉 fiddle -> http://jsfiddle.net/5fbo72rm/