javascript 数据表 - 如何更改动态更改的单元格的背景和文本颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32894735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:53:14  来源:igfitidea点击:

Datatables - How do I change background and text color of a cell changed dynamically?

javascriptjquerydatatablescellbackground-color

提问by rodzun

I use the following code to update a cell dynamically and works perfect, the only thing is how to change the color of the background and the text of that cell data. If it′s possible an example of how to change the entire row as well. Thanks in advance.

我使用以下代码动态更新单元格并完美运行,唯一的问题是如何更改背景颜色和该单元格数据的文本。如果可能的话,还有一个如何更改整行的示例。提前致谢。

$(document).ready(function (){
    var table = $('#example').DataTable();

    table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
        var data = this.data();       
        console.log(data);

        data[0] = '* ' + data[0];

        this.data(data);
    });
});

回答by Gyrocode.com

SOLUTION

解决方案

You can access the cell node by using cell().node()API method.

您可以使用cell().node()API 方法访问单元节点。

$(document).ready(function (){
    var table = $('#example').DataTable();

    table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
        var cell = table.cell({ row: rowIdx, column: 0 }).node();
        $(cell).addClass('warning');
    });
});

DEMO

演示

See this jsFiddlefor code and demonstration.

有关代码和演示,请参阅此 jsFiddle