如何启用/禁用Datatables复选框选定行时的按钮
问题描述:
当我的数据表中存在选定的复选框行时,如何启用/禁用我的按钮?如何启用/禁用Datatables复选框选定行时的按钮
var pctoreceive = $('#pcToReceive').DataTable({
'columnDefs': [{
'targets': 0,
'searchable': false,
'orderable': false,
'className': 'dt-body-center',
'render': function (data, type, full, meta) {
return '<input type="checkbox" name="id[]" value="'
+ $('<div/>').text(data).html() + '">';
}
}],
我缩短了我的代码。上面说我添加了一个新列checkbox
节目选择
在没有选择的行这两个按钮必须被禁止。否则启用
$('#rc-select-all').on('click', function() {
// Check/uncheck all checkboxes in the table
var rows = pctoreceive.rows({
'search': 'applied'
}).nodes();
$('input[type="checkbox"]', rows).prop('checked', this.checked);
});
// Handle click on checkbox to set state of "Select all" control
$('#pcToReceive tbody').on('change', 'input[type="checkbox"]', function() {
// If checkbox is not checked
if (!this.checked) {
var el = $('#rc-select-all').get(0);
// If "Select all" control is checked and has 'indeterminate' property
if (el && el.checked && ('indeterminate' in el)) {
// Set visual state of "Select all" control
// as 'indeterminate'
el.indeterminate = true;
}
}
});
答
您的问题与datatable无关。
只需设置一个计数器变量来存储检查了多少输入检查,然后如果> 0启用您的按钮。
见这个小例子(fiddle)
var counterChecked = 0;
$('body').on('change', 'input[type="checkbox"]', function() {
this.checked ? counterChecked++ : counterChecked--;
counterChecked > 0 ? $('#submitButton').prop("disabled", false): $('#submitButton').prop("disabled", true);
});
为什么它的身体吗?为什么不在特定的桌子上? – qwerzxcxyz
只是因为我没有把任何表放在我的例子,但在你的代码中,你应该使用特定的表格选择器的c :) – pumpkinzzz
我试了一下,它只适用于行..但不是在选择/取消选择复选框header( – qwerzxcxyz