如何更改JQuery.DataTable中特定单元格的背景? [英] How do you change background of specific cell in a JQuery.DataTable?

查看:544
本文介绍了如何更改JQuery.DataTable中特定单元格的背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改DattaTable中某些特定单元格的样式,但是我不确定如何执行此操作.我知道如何更改整个专栏,但这不是我所需要的. 假设我的数据如下:

I'm trying to change the styling of some specific cells in a DattaTable, but I'm not sure how to do that. 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
}

在我的桌子上有

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

我想强调一下,蓝色格性别="m"或红色格性别="f"的年龄单元格.

And I want to highlight, lets say, the age cells in blue case gender = "m" or red case gender = "f".

关于我该如何实现的任何建议?

Any suggestion on how I can accomplish that?

谢谢!

推荐答案

您将使用列配置的createdCell函数.

You would use the column configuration's createdCell function.

基本上,您需要为性别"列添加一个列定义,其中包括一个在填充单元格时将被调用的回调.尝试将此对象添加到您的columns数组:

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:

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

这是一个有效的示例.

这篇关于如何更改JQuery.DataTable中特定单元格的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆