简体   繁体   English

如何更改jQuery jTable特定行中的背景颜色?

[英]How can I change the background color in a specific row of jQuery jTable?

rowInserted: function (event, data) {
   if (data.record) {
       if (condition1 == condition2) {
          $('#div1').find(".jtable tbody tr").css("background", "#F5ECCE");
       }
   }
}

the above code could be changing all row color, can i specify row number? 上面的代码可能会改变所有行颜色,我可以指定行号吗?

Use :eq() selector like, 使用:eq()选择器,如,

rowInserted: function (event, data) {
   if (data.record) {
       if (condition1 == condition2) {
          $('#div1').find(".jtable tbody tr:eq(1)").css("background", "#F5ECCE");
          // changing first row background color
       }
   }
}

Updated you can set index dynamically like 更新后你可以动态设置索引

$('#div1').find(".jtable tbody tr:eq("+index+")").css("background", "#F5ECCE");

Rohan Kumar and raevilman's answer works fine. Rohan Kumar和raevilman的回答很好。 But the code can be shortened and runs faster by using data.row 但是使用data.row可以缩短代码并加快运行速度

rowInserted: function (event, data) {
   if (data.record) {
       if (condition1 == condition2) {
          data.row.css("background", "#F5ECCE");
       }
   }
}

The example below will change the 4th rows background color 以下示例将更改第4行背景颜色

var $rows = $('#div1').find(".jtable tbody tr");
var ROWNUMBER = 3;
$($rows[ROWNUMBER]).css("background", "#F5ECCE");

Alternatively you can use the pseudo class selector :eq() to select the number you want only, this also uses 0 based indexing. 或者,您可以使用伪类选择器:eq()来选择您想要的数字,这也使用基于0的索引。

var ROWNUMBER = 3;
var $row = $('#div1').find(".jtable tbody tr:eq(" + ROWNUMBER + ")");    
$row.css("background", "#F5ECCE");

Try this. 尝试这个。

var rowNumber = 1;
$('#div1').find(".jtable tbody tr").eq(rowNumber).css("background", "#F5ECCE");

To get row id dynamically 动态获取行ID
Use like following 使用如下

rowInserted : function(event, data) 
        {
           var index = data['row'][0]['rowIndex'];
           $('#npoDiv').find(".jtable tbody tr:eq("+index+")").css("background", "green");
        }

Building up on raevilman 's answer which was not working for me due to a little detail: with rowIndex I get indices starting at 1 , but the CSS path below has the first row indexed at 0 , hence the -1 when i assign the value to the variable index . 建立raevilman的答案,由于一些细节,这对我来说不起作用:使用rowIndex我得到的索引从1开始,但是下面的CSS路径的第一行索引为0 ,因此当我赋值时为-1到变量索引

        rowInserted: function(event, data) {
            if (data.record.value>100){
                var index = data['row'][0]['rowIndex']-1;
                console.log('decorating row with index: '+index);
                $('#div1').find(".jtable tbody tr:eq("+index+")").css({"background":"red"});

            }
        }

This correctly highlights in red the rows whose "value" field is greater than 100. 这正确地以红色突出显示“value”字段大于100的行。

要在加载表后执行此操作,并且如果您知道该行的键,则可以使用以下命令:

    $("tr[data-record-key=" + rowKey + "]").addClass(rowClass); 

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

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