我有一个数据表,我试图在其中获取所有选中的行。此表具有行分组并使用来自 gyrocode 的复选框插件.我试过 api 上列出的代码,但我没有运气。无论选择什么,我只返回第一条记录。我使用的代码如下所示:
var tbl;
$(document).ready(function (){
tbl = $('#example').DataTable({
columnDefs: [{
targets: 0,
data: 2,
'checkboxes': {
'selectRow': true
}
},
{ "visible": false, "targets": 1 }],
select: {
style: 'multi'
},
order: [[1, 'asc']],
iDisplayLength: 10,
drawCallback: function () {
var api = this.api();
var rows = api.rows({ page: 'current' }).nodes();
var last = null;
api.column(1, { page: 'current' }).data().each(function (group, i) {
if (last !== group) {
$(rows).eq(i).before(
'<tr class="group"><td colspan="6">' + group + '</td></tr>'
);
last = group;
}
});
}
});
});
function getSelected(){
alert(tbl.columns().checkboxes.selected().length);
}
我的 jfiddle 中有代码这里。我不确定它们是否是复选框和行分组之间的干扰?请让我知道哪里出错了。
注:checkbox是基于gyrocode的插件,datatables是1.10.12版本
最佳答案
我来不及回答这个问题了。但我的回答可以帮助社区中的其他人。
//datatable has to be initialized to a variable
var myTable = $('#calltable').dataTable();
//checkboxes should have a general class to traverse
var rowcollection = myTable.$(".call-checkbox:checked", {"page": "all"});
//Now loop through all the selected checkboxes to perform desired actions
rowcollection.each(function(index,elem){
//You have access to the current iterating row
var checkbox_value = $(elem).val();
//Do something with 'checkbox_value'
});
希望对您有所帮助。
关于javascript - jquery Datatables 复选框获取所有选中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42658148/