简体   繁体   English

如何更改JQuery.DataTable中特定单元格的背景?

[英]How do you change background of specific cell in a JQuery.DataTable?

I'm trying to change the styling of some specific cells in a DattaTable, but I'm not sure how to do that. 我正在尝试更改DattaTable中某些特定单元格的样式,但是我不确定如何执行此操作。 I know how to change for an entire column, but that's not what i need. 我知道如何更改整个专栏,但这不是我所需要的。 So let's say that my data is like: 假设我的数据如下:

{
name: "user1",
gender : "f",
age: 54
},{
name: "user1",
gender : "m",
age: 33
}

In my table, I have 在我的桌子上

columns: [{ 
data: "name"
},{
data: "age"
}]

And I want to highlight, lets say, the age cells in blue case gender = "m" or red case gender = "f". 我想强调一下,蓝色格性别=“ m”或红色格性别=“ f”的年龄单元格。

Any suggestion on how I can accomplish that? 关于我如何做到这一点的任何建议?

Thank you! 谢谢!

You would use the column configuration's createdCell function . 您将使用列配置的createdCell函数

Basically, you need to add a column definition for the gender column that includes a callback that will be called when the cell is populated. 基本上,您需要为“性别”列添加一个列定义,其中包括一个将在填充单元格时调用的回调。 Try adding this object to your columns array: 尝试将此对象添加到您的columns数组:

    {
      data: "gender",
      createdCell: function(td, cellData, rowData, row, col){
        var color = (cellData === 'm') ? 'blue' : 'red';
        $(td).css('color', color);
      }
    }

Here is a working example. 这是一个工作示例。

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

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