Search code examples
jquerydatatable

How to find a value in a jQuery Datatable


I am trying to find a value in the first column on a datatable, and colored the row when this value is found.

I tried this :

$.each(table.column(0), function(key, value){
  if(value==="value"){
    table.row(0).css('background-color', 'red');
  }
})

then this:

for(var i =0; i<table.row(0).length; i++){
  if(table.column(0).item(i).data() === "value"){
    table.row(i).css('background-color', 'red');   
  }
}

But nothing worked.


Solution

  • You can loop inside your datatable after bind using YourTable.rows().every(function(rowIdx, tableLoop, rowLoop){ and add desire class whatever needed.

    Example:

    var YourTable = $('#table').DataTable({}); // Bind your table first.
    
    YourTable .rows().every(function(rowIdx, tableLoop, rowLoop){
             var rowData = this.data();
             if(rowData.value  == "value"){
                $(this.node()).addClass('YourClass');
            }
    });
    

    On CSS:

    .YourClass{
      background-color: red
    }
    

    Note: Dont confuse with rowData.value, its field name just like first name, Email .........