Hi guys,
Does anyone know how to implement an event to be triggered when you click on the checkbox "select all" in the header of the DataTable to select all records?
These is the code I've implemented so far. For some reason the "$('#myTable').on('click', '.toggle-all', function (e)" is not working properly.
let table = $("#myTable").DataTable({
...
...
...
}).on('select.dt deselect.dt', function (e) {
let numberOfRecords = table.rows().nodes().length;
let selectedRows = table.rows(".selected").nodes().length;
if (numberOfRecords === selectedRows)
$(".toggle-all").closest("tr").addClass("selected");
else
$(".toggle-all").closest("tr").removeClass("selected");
table.button(0).enable(selectedRows > 0);
table.button(1).enable(selectedRows === 1);
table.button(2).enable(selectedRows === 1);
});
$('#myTable').on('click', '.toggle-all', function (e) {
$(this).closest("tr").toggleClass("selected");
if ($(this).closest("tr").hasClass("selected"))
table.rows().select();
else
table.rows().deselect();
});
I believe it would be more reliable to use an event directly from the dataTable API.
Any help appreciated!
Thank you very much in advance.
Regards,
Alex