中特定单元格的背景

中特定单元格的背景

本文介绍了如何更改 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.

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

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 中特定单元格的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 00:11