问题描述
我有一个函数可以选择表 (selecionarTodos()
) 的所有页面上的所有复选框 (#tbTarefas
)。但是,我想使用相同的方法选择其他表中的所有复选框,这些表具有相同的结构,只是id
不同。
我想检查某个表是否在屏幕上呈现。类似的东西。
之前
function selecionarTodos(source) {
const tabela = $("#tbTarefas").DataTable();
let celulasCheckBox = tabela.column(0).nodes();
for (let i = 0; i < celulasCheckBox.length; i++) {
let checkBox = celulasCheckBox[i].querySelector('input[type="checkBox"]');
checkBox.checked = source.checked;
}
}
之后(初步思考)
function selecionarTodos(source) {
const tabelaTarefas = $("#tbTarefas").DataTable();
const tabelaAtendimento = $("#tbPendentes").DataTable();
if (tabelaTarefas) {
let CheckBoxTarefasTd = tabelaTarefas.column(0).nodes();
for (let i = 0; i < CheckBoxTarefasTd.length; i++) {
let checkBox = CheckBoxTarefasTd[i].querySelector('input[type="checkBox"]');
checkBox.checked = source.checked;
}
}
else if (tabelaAtendimento) {
let CheckBoxAtendimentoTd = tabelaAtendimento.column(0).nodes();
for (let i = 0; i < CheckBoxAtendimentoTd.length; i++) {
let checkBox = CheckBoxAtendimentoTd[i].querySelector('input[type="checkBox"]');
checkBox.checked = source.checked;
}
}
}
解决方法
Datatables 在初始化完成后有一个回调。
$('#tbTarefas').on( 'init.dt',function () {
}
或者在每次重绘时使用“draw.dt”