简体   繁体   English

根据数据值将类添加到单元格

[英]Add a class to a cell depending on data value

I have a datatable 我有一个数据表

<table data-bind="dataTable: { 
        data: items, 
        options: {
            bPaginate: false,
            aaSorting: [[0, 'desc']],
            aoColumns: [ 
                { sClass: 'date', mDataProp: 'date' }, 
                { mDataProp: 'time' }, 
                { sClass: 'name', mDataProp: 'name' }, 
                { sClass: 'thought', mDataProp: 'thought' } 
            ] 
        } 
    }">

there is also another value in the items that I don't display (thought type). 在我不显示的项目中也有另一个值(思想类型)。 I want to change the class of the cell 'thought' depending on the value of 'thought type'. 我想根据“思想类型”的值更改“思想”单元格的类别。

So if thought type is new idea I want the cell that displays the value of 'thought' to be yellow. 因此,如果思想类型是新思想,我希望显示“思想”值的单元格为黄色。

Is this possible with datatables? 数据表可能吗?

Add a function 添加功能

"fnRender": function(obj) {
                    var sReturn = obj.aData[ obj.iDataColumn ];
                    if ( sReturn == "is wat you needed" ) {
                        sReturn = "add style to your element";
                    }
                    return sReturn;
                }

Go through the example shown in below link 浏览下面链接中显示的示例

http://datatables.net/examples/data_sources/js_array.html http://datatables.net/examples/data_sources/js_array.html

You can see that the A alphabet is bold compared to others..Hope this solves your problem 您会发现A字母比其他字母大胆..希望这可以解决您的问题

Take a look at fnRowCallback in the API . 看一下API中的fnRowCallback For any given row, this can respond to that row just after drawing it and adjust the row as needed based on the data of that row. 对于任何给定的行,它可以在绘制完该行之后对其做出响应,并根据该行的数据根据​​需要调整该行。 For example, something like this might work: 例如,类似这样的方法可能会起作用:

'fnRowCallback' : function(row, data) {
    if (data[0] === 'someValue') {
        $('td:eq(0)', row).addClass('someClass');
    }
}

This Solution help fix my problem might help u. 此解决方案有助于解决我的问题,可能对您有所帮助。 for more info check this link: columns.createdCell 有关更多信息,请检查此链接: columns.createdCell

Using createdCell manipulate the DOM within columnDefs options. 使用createdCell在columnDefs选项中操作DOM。
For example, 例如,

"columnDefs": [ 
{
   "targets": [1] // first CELL That will be checked,
   "createdCell": function (td, cellData, rowData, row, col) {
             if (cellData < 1) {
                 $(td).addClass('someClass');
              }
    }
 },{
   "targets": [2] //second CELL That will be checked,
   "createdCell": function (td, cellData, rowData, row, col) {
             if (cellData < 1) {
                 $(td).addClass('someClass');
              }
    }
 }  ],

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

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