简体   繁体   English

迭代并“选择”jQuery DataTables行

[英]Iterating through and 'selecting' jQuery DataTables rows

I am using jQuery DataTables 1.10.3 to create a table with JSON data fetched from a web service. 我正在使用jQuery DataTables 1.10.3创建一个表,其中包含从Web服务获取的JSON数据。 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). 数据链接到我的应用程序页面上的其他元素,这样当用户在一个元素中选择数据点时,DataTable应该更新并“选择”补充行(只需将active类添加到行<tr>标记中)。 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. 但这不起作用,因为.rows()返回行索引数组而不是行对象。 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). 考虑到表中记录的数量相对较少(3000行约3秒),这种方法很有效,但速度极慢。 Is there a better way to iterate through DataTable rows that I am missing? 有没有更好的方法来迭代我缺少的DataTable行? Or is it possible to make a row selection based on data values? 或者是否可以根据数据值进行行选择? Are the documents incorrect about .rows() returning API objects? 有关.rows()返回API对象的文档不正确吗?

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. 这几乎是立即运行,所以它处理我的用例,但我仍然想知道以更一般的方式迭代行的最佳方法是什么。

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

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