简体   繁体   English

jQuery DataTables单元样式

[英]jQuery DataTables cell styling

I'm using jQuery DataTables, and I am able to look at the row data and colorize the row. 我正在使用jQuery DataTables,并且能够查看行数据并为行着色。

Here is the code that creates the datatable: 这是创建数据表的代码:

 var $dataTable = $('#example1').DataTable({
   "data": data,
   "dataType": "json",
   "iDisplayLength": 25,
   "order": [[6, "desc"]],
   "scrollY": 550,
   "scrollX": true,
   "bDestroy": true,
   "stateSave": true,
   // here is the part that styles the row
   "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull)
   {
     if (aData[12] == "Y"){$('td', nRow).css('background-color', '#EE6363');}
     if (aData[9] == "Y"){$('td', nRow).css('font-weight', 'bold');}
   }
 });

So if you'll notice in the code above, the 1st if statement checks if the row data of the 12th column, which is called REJECTED, is "Y". 因此,如果您在上面的代码中注意到,则第一个if语句检查第12列的行数据(称为REJECTED)是否为“ Y”。 If so, make the entire row red. 如果是这样,请将整个行设为红色。

The 2nd if statement checks if the row data of the 9th column, which is called URGENT, is "Y". 第二个if语句检查第9列的行数据(称为URGENT)是否为“ Y”。 If so, make the text for that entire row bold. 如果是这样,请将整个行的文本设为粗体。

What I would like to do is make the cell for that column red instead of the whole row, primarily for the 1st row. 我想做的是将该列的单元格设为红色,而不是整个行,主要是第一行。

How can I alter my code above to achieve this? 如何更改上面的代码以实现此目的?

I can't test it.... 我无法测试...

But I would try something like this : 但是我会尝试这样的事情:

if (aData[12] == "Y"){$('td', nRow)[12].css('background-color', '#EE6363');}

Or this tricky thing: 还是这个棘手的事情:

if (aData[12] == "Y"){$('td', nRow).parent().children()[12].css('background-color', '#EE6363');}

If zero-based: 如果从零开始:

if (aData[12] == "Y"){$('td', nRow)[11].css('background-color', '#EE6363');}

Or: 要么:

if (aData[12] == "Y"){$('td', nRow).parent().children()[11].css('background-color', '#EE6363');}

Here is the code that will color the target cell: 这是将为目标单元着色的代码:

 if (aData[12] == "Y"){$('td', nRow).eq(1).css('background-color', '#BF5FFF');}

By adding eq(1), I can target and color the 2nd cell in the datatable. 通过添加eq(1),我可以定位数据表中的第二个单元并为其着色。 This is how I got it to work. 这就是我如何使其工作。

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

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