简体   繁体   中英

Jquery Datatable change row color based on value

I have this jquery datatable below. If id is 50415, I want row color to change to red. but it fails to do despite adding the css. Tried many of the soloutions on stackoverflow. Do advise, thanks so much.

{
  "mDataProp": null,
  render: function (data, type, row, i) {
    var reconJrnlId = (data.jrnlItemId).toString();                 
    if (reconJrnlId === '50415') {
      console.log('change to red please');
      $(row).css("background-color","red");
    }                   
    return '-';
  }

I'm not too sure how your data is structured. But you can get the key of the data and check if the key value is your id. So then you can add the CSS this way

var id = data[3];
   if(id == 66){
   console.log(data);
   $(row).addClass('blue');   //add class to row
   }

See fiddle here https://jsfiddle.net/d9m6bzyg/1/ You will see row number 2 in blue ie Ashton Cox

This example was take from here http://live.datatables.net/cezewiqe/1/edit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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