Javascript jquery Datatables 复选框获取所有选中的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42658148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery Datatables checkbox get all checked rows
提问by usr4896260
I have a datatable in which in which I'm trying to get all the checked rows. This table has row grouping and uses a checkbox plugin from gyrocode. I've tried the code listed on the api, but I had no luck. I only get the first record returned, regardless of what is selected. The code I used for the is shown below:
我有一个数据表,我试图在其中获取所有选中的行。该表具有行分组并使用来自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);
}
I have the code in my jfiddlehere. I'm not sure if their is interence between the checkbox and the row grouping? Please let me know where I am going wrong.
我的jfiddle 中有代码。我不确定它们是否是复选框和行分组之间的交互?请让我知道我哪里出错了。
Note: The checkbox is based on the plugin by gyrocode The datatables is version 1.10.12
注意:复选框基于 gyrocode 的插件数据表版本为 1.10.12
回答by Sujen K.
I did a quick check and Eric Guan is correct. I'll just post a code snippet:
我做了一个快速检查,Eric Guan 是正确的。我只会发布一个代码片段:
function getSelected() {
var selectedIds = tbl.columns().checkboxes.selected()[0];
console.log(selectedIds)
selectedIds.forEach(function(selectedId) {
alert(selectedId);
});
}
See: https://jsfiddle.net/nwmmbLso/3/
见:https: //jsfiddle.net/nwmmbLso/3/
I just noticed you have duplicatie Student Id's which might also cause unexpected behavior from the library you are using. The code provided above should work if the Student Id's are unique.
我刚刚注意到您有重复的学生 ID,这也可能导致您正在使用的库出现意外行为。如果学生 ID 是唯一的,则上面提供的代码应该有效。
回答by Ansari Rizwan
Working and tested.
工作和测试。
var id = "";
var oTable = $(".table").dataTable();
$(".check_quality:checked", oTable.fnGetNodes()).each(function() {
if (id != "") {
id = id + "," + $(this).data('id');
} else {
id = $(this).data('id');
}
});
回答by BEingprabhU
I'm too late to answer this question. But my answer can help others in the community.
我来不及回答这个问题。但我的回答可以帮助社区中的其他人。
//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'
});
I hope that helps.
我希望这有帮助。
回答by Priya Chetwani
Simple answer - use either table.rows( '.selected' ) or table.rows( {selected:true} )
简单的答案 - 使用 table.rows( '.selected' ) 或 table.rows( {selected:true} )
var count = $('#datatable').DataTable().rows( '.selected' ).count();
var checked_rows = $('#datatable').DataTable().rows( '.selected' ).data();
for(var i=0; i<checked_rows.length; i++)
{
console.log( checked_rows[i] );
}
Document Link: https://datatables.net/reference/api/count()

