根据内容更改jquery数据表的单元格背景

发布于 2025-01-01 08:10:35 字数 1618 浏览 1 评论 0原文

我是数据表新手 - http://datatables.net/ - 。我很难找到一个示例,如何根据单元格的位置和内容更改单元格的背景颜色。

像这样的东西对我有用,除了所选行的突出显示现在是 背景颜色已更改的单元格中的“颜色过度”。如果我可以在 fnRowCallback 中获取行的类名,那么我就可以处理它。

$(document).ready(function() {

   // Add a click handler to the rows - this could be used as a callback 
   $("#example tbody").click(function(event) {

      $(oTable.fnSettings().aoData).each(function() {
         $(this.nTr).removeClass('row_selected');
      });
      (event.target.parentNode).addClass('row_selected');
   });


   oTable = $('#example').dataTable({

      "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {

         $(nRow).children().each(function(index, td) {

            if (index == 6) {

               if ($(td).html() === "pending") {
                  $(td).css("background-color", "#078DC6");
               } else if ($(td).html() === "rendering") {
                  $(td).css("background-color", "#FFDE00");
               } else if ($(td).html() === "success") {
                  $(td).css("background-color", "#06B33A");
               } else if ($(td).html() === "failure") {
                  $(td).css("background-color", "#FF3229");
               } else {
                  $(td).css("background-color", "#FF3229");
               }
            }
         });
         return nRow;
      },
      "bProcessing": true,
      "bServerSide": true,
      "sAjaxSource": "scripts/server_processing.php",
      "sPaginationType": "full_numbers",
   });
});

I am new to datatables - http://datatables.net/ - . I am in trouble to find an example how I could change the background color of a cell based on its position and content.

Something like this worked for me except that the highlighting of the selected row is now
'overcolored' in the cells which have changed background color. If I could get the class name of the row in the fnRowCallback then I could handle it.

$(document).ready(function() {

   // Add a click handler to the rows - this could be used as a callback 
   $("#example tbody").click(function(event) {

      $(oTable.fnSettings().aoData).each(function() {
         $(this.nTr).removeClass('row_selected');
      });
      (event.target.parentNode).addClass('row_selected');
   });


   oTable = $('#example').dataTable({

      "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {

         $(nRow).children().each(function(index, td) {

            if (index == 6) {

               if ($(td).html() === "pending") {
                  $(td).css("background-color", "#078DC6");
               } else if ($(td).html() === "rendering") {
                  $(td).css("background-color", "#FFDE00");
               } else if ($(td).html() === "success") {
                  $(td).css("background-color", "#06B33A");
               } else if ($(td).html() === "failure") {
                  $(td).css("background-color", "#FF3229");
               } else {
                  $(td).css("background-color", "#FF3229");
               }
            }
         });
         return nRow;
      },
      "bProcessing": true,
      "bServerSide": true,
      "sAjaxSource": "scripts/server_processing.php",
      "sPaginationType": "full_numbers",
   });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一片旧的回忆 2025-01-08 08:10:35

我知道这个问题已经得到解答,但我想我会分享如何能够通过以下方式将各种“条件格式”应用到各种单元格:

首先,我在数据表初始化期间向每列添加了一个类:

"aoColumns": [{
                "mDataProp": "serial",
                "sClass": "serial"
            }, {
                "mDataProp": "us",
                "sClass": "us"
            }...],

然后,我创建了一个每次重绘都会调用的 addFormat() 函数(我必须这样做,因为我有一个实时更新的实时表):

"fnDrawCallback": function(oSettings) { addFormat(); },

在 addFormat 函数中,我基本上通过 CSS 类定义了所有格式规则。我使用 jQuery :contains 扩展 来应用正则表达式等非常具体的规则。这些规则的选择器将是 td 以及我在数据表初始化期间分配给列的任何类:

 $("td.us:containsRegex(/^[xtfv]$/)").addClass("s4 queue");

这对我来说非常有效。

I know the question was already answered, but I thought I would share how I was able to apply "conditional formatting" of sorts to various cells, in the following way:

First, I added a class to each column during my datatables initialization:

"aoColumns": [{
                "mDataProp": "serial",
                "sClass": "serial"
            }, {
                "mDataProp": "us",
                "sClass": "us"
            }...],

Then, I created an addFormat() function that was called each redraw (I had to do it this way because I have a live table that updates in realtime):

"fnDrawCallback": function(oSettings) { addFormat(); },

In the addFormat function, I essentially defined all of my formatting rules, via CSS classes. I used a jQuery :contains extension in order to apply very specific rules with regular expressions and such. My selectors for these rules would be td and whatever class I had assigned to a column during datatables initialization:

 $("td.us:containsRegex(/^[xtfv]$/)").addClass("s4 queue");

That worked very well for me.

时光暖心i 2025-01-08 08:10:35

您可以根据单元格值动态更改单元格的背景颜色

{"mData": "index", "sClass": "center" ,"fnRender": function(obj) {
                                              var index  = obj.aData.index;
                                              var isActive  = obj.aData.isActivated;
                                              if(isActive == true){
                                                  obj.oSettings.aoColumns[obj.iDataColumn].sClass = "greenBackground"
                                              }else{
                                                  obj.oSettings.aoColumns[obj.iDataColumn].sClass = "yellowBackground"
                                              }
                                              return index;
                                          } },

You can change background color of cell dynamically respective of cell value by

{"mData": "index", "sClass": "center" ,"fnRender": function(obj) {
                                              var index  = obj.aData.index;
                                              var isActive  = obj.aData.isActivated;
                                              if(isActive == true){
                                                  obj.oSettings.aoColumns[obj.iDataColumn].sClass = "greenBackground"
                                              }else{
                                                  obj.oSettings.aoColumns[obj.iDataColumn].sClass = "yellowBackground"
                                              }
                                              return index;
                                          } },
归途 2025-01-08 08:10:35
$('table:last tr:nth-child(2) td:nth-child(2)').
  css("background-color", "#cccccc");
$('table:last tr:nth-child(2) td:nth-child(2)').
  css("background-color", "#cccccc");
以可爱出名 2025-01-08 08:10:35

我还没有测试过这个,但是这样的东西应该会给你一个想法:

// Change i-th row j-th column
var rows = document.getElementById("myTable").getElementsByTagName('tr');
var columns = rows[i].getElementsByTagName('td');
jQuery( columns[j] ).css("background-color", "#cccccc");

I haven't tested this but something like this should give you an idea:

// Change i-th row j-th column
var rows = document.getElementById("myTable").getElementsByTagName('tr');
var columns = rows[i].getElementsByTagName('td');
jQuery( columns[j] ).css("background-color", "#cccccc");
ゞ花落谁相伴 2025-01-08 08:10:35

以下是如何使用 DataTables 1.10+ 语法通过 createdCell 有条件地设置单元格样式

"columnDefs": [
{
        "targets": [6],
        "createdCell": function(td, cellData, rowData, row, col) {
            var color;
            switch(cellData) {
            case "pending":
                color = '#078DC6';
                break;
            case "success":
                color = '#FFDE00';
                break;
            case "failure":
                color = '#06B33A';
                break;
            default:
                color = '#FF3229';
                break;
            }
            $(td).css('background',color);
        }
    }
],

Here's how to conditionally style the cells via createdCell, using DataTables 1.10+ syntax.

"columnDefs": [
{
        "targets": [6],
        "createdCell": function(td, cellData, rowData, row, col) {
            var color;
            switch(cellData) {
            case "pending":
                color = '#078DC6';
                break;
            case "success":
                color = '#FFDE00';
                break;
            case "failure":
                color = '#06B33A';
                break;
            default:
                color = '#FF3229';
                break;
            }
            $(td).css('background',color);
        }
    }
],
甜尕妞 2025-01-08 08:10:35

这对我有用。 data[3] 表示我们正在查看的行的第 4 列(请记住,javascript 中的数组是从零开始的,即它们从零开始编号)。但是当我们想要更改单元格背景时,我们使用 .eq(3) 语法。

"createdRow": function ( row, data, index ) {
    if ( data[3] == "pending" ) {
        $('td', row).eq(3).css('background-color', '#078DC6');
    }
    if ( data[3] == "rendering") {
        $('td', row).eq(3).css('background-color', '#FFDE00');
    }

}

This worked for me. data[3] means column 4 of the row we're looking at (remember, arrays in javascript are zero-based, that is they start numbering at zero). But when we want to change the cell background, we use the .eq(3) syntax.

"createdRow": function ( row, data, index ) {
    if ( data[3] == "pending" ) {
        $('td', row).eq(3).css('background-color', '#078DC6');
    }
    if ( data[3] == "rendering") {
        $('td', row).eq(3).css('background-color', '#FFDE00');
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文