如何根据jQuery数据表中的列值设置表行的颜色 [英] How to set color for table rows based on column value in jQuery data table

查看:232
本文介绍了如何根据jQuery数据表中的列值设置表行的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery数据表。我有如下数据:

  Column1 Column2 Column3 
--- --------------------
AAA BBB CCC
AAA GGG YYY
BBB ooo LLL

现在在第一列的第一行中,我有相同的值 AAA 。我想申请一些颜色给那些
rows.And然后另一个颜色为第三行。如此这个我有30个记录。这可能做到这一点。如果可能我可以做到这一点。我正在使用jQuery数据表。提前谢谢

解决方案

使用 fnRowCallback (或更新的 rowCallback )来实现这个fnRowCallback:function(nRow,aData,iDisplayIndex,iDisplayIndexFull(),$ i $ s $ ){
switch(aData [0]){
case'AAAA':
$(nRow).css('color','red')
break;
case'BBBB':
$(nRow).css('color','green')
break;
case'CCCC':
$(nRow).css('color','blue')
break;
}
}
});

演示:小提琴


I am using jQuery datatables.I have the data like as follows

Column1 Column2 Column3
-----------------------
 AAA    BBB     CCC
 AAA    GGG     YYY
 BBB    ooo     LLL

Now in column1 for first 2 rows i have same value AAA.I want to apply some color to those rows.And then another color for third row.Like this i have 30 records.Is it possible to do this.If possible how i can do this.I am using jQuery data tables.Thanks in advance..

解决方案

Use the fnRowCallback (or newer rowCallback) to achieve this

$('#example').dataTable({
    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
        switch(aData[0]){
            case 'AAAA':
                $(nRow).css('color', 'red')
                break;
            case 'BBBB':
                $(nRow).css('color', 'green')
                break;
            case 'CCCC':
                $(nRow).css('color', 'blue')
                break;
        }
    }
});

Demo: Fiddle

这篇关于如何根据jQuery数据表中的列值设置表行的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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