首页 > 解决方案 > 如何选择数据表 1.9 中当前页面中显示的行?

问题描述

我正在使用 datatable 1.9 和 tabletool.js 2.1.5。我总共有 23 条记录,每页 10 条记录。我想选择当前显示的页面记录。

我使用了 TableTools.js 按钮 (select_all)。但这会选择数据表的所有行。我试过了

$('#file-records> tbody > tr').each(function() {                                
   TableTools.fnGetInstance('file-records').fnSelect($(this));                                              
});

上面的代码给了我一个错误 Uncaught TypeError: Cannot read property 'nTr' of undefined

标签: jquerydatatableslegacy

解决方案


我已经实现了以下

$('#file-records> tbody > tr').each(function() {
    $(this).addClass("active");
});

$(this).addClass("active"); 添加我在 css 文件中编写的背景颜色:#017ebc。

所以当你想获得选定的行值时,你可以拥有

$('#file-records> tbody > tr').each(function() {
     if($(this).hasClass('active')){
     console.log($(this)[0]);
   }
});

$(this)[0] 是我的行属性,我们可以从中获取实际的行内容


推荐阅读