Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82266

dataTable trigger event for select all rows

$
0
0

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


Viewing all articles
Browse latest Browse all 82266

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>