3

I am using jQuery DataTables 1.10.3 to create a table with JSON data fetched from a web service. The data is linked to other elements on the page of my application such that when a user selects data points in one element, the DataTable should update and 'select' the complimentary rows (simply by adding the active class to the rows <tr> tag). Given the documentation, it would appear that this could be easily accomplished with something like this:

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

But this does not work, since .rows() returns an array of row indexes instead of row objects. Instead, I have implemented this:

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 through DataTable rows that I am missing? Or is it possible to make a row selection based on data values? Are the documents incorrect about .rows() returning API objects?

1 Answer 1

1

Well, I found an answer to my second question:

Or is it possible to make a 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 run almost instantly, so it handles my use case, but I'd still like to know what the best way is to iterate through rows in a more general way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.