JQuery DataTables Iterating and Selecting Rows

I am using jQuery DataTables 1.10.3 to create a table with JSON data retrieved from a web service. The data is linked to other elements on the page of my application, so when the user selects data points in one element, the DataTable must update and "select" free rows (just adding the class active

to the rows <tr>

). Given the documentation , it would seem that this could be easily accomplished with the following:

var table = $("#my-table").DataTable();
table.rows().each(function(r){
    if (r.data().category == 'XYZ'){
        r.node().to$().addClass("active");
    }
});

      

But this doesn't work as it .rows()

returns an array of string indices instead of string objects. I implemented this instead:

var table = $("#my-table").DataTable();
table.rows().indexes().each(function(i){
    var r = table.row(i);
    if (r.data().category == 'XYZ'){
        r.node().to$().addClass("active");
    }
});

      

This works, but is incredibly slow considering the relatively small number of records in the table (~ 3 seconds for 3000 rows). Is there a better way to iterate over the DataTable rows that I am missing? Or is it possible to make row selection based on data values? Is it wrong for docs to .rows()

return API objects?

+3


source to share


1 answer


Well, I found the answer to the second question:

Or is it possible to make row selection based on data values?



var table = $("#my-table").DataTable();
table.rows(function(idx, data, node){
    return data.category == 'XYZ' ? true: false;
}).nodes().to$().addClass("active");

      

This is almost instantaneous, so it handles my use case, but I would still like to know how best to iterate over strings more generally.

+1


source







All Articles