Select All (via Checkbox)

Select All (via Checkbox)

Jay77Jay77 Posts: 4Questions: 3Answers: 0

Hi,

I'm using a theme that suggests I can add a "select/deselect all" checkbox to the top-left corner of my datatables.

http://keenthemes.com/preview/metronic/theme/admin_1/table_datatables_managed.html

This checkbox selects/deselects only those rows that are visible.

How would I do this? I can't find anything in the documentation but I'm sure it must be there somewhere.

Many thanks

Answers

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    edited November 2018

    If you would like individual checkboxes to do selections that would be an option. There should be a button that does this.

    I have just implemented such a button myself that does the selection of all rows. The solution requires the Select extension. https://datatables.net/extensions/select/ and the Buttons extension: https://datatables.net/extensions/buttons/
    I don't use those checkboxes because they are redundant using the Select Extension. If you would like to use checkboxes you would have to assign the code of the button to that checkbox.

    This is the custom button definition to select all rows: ("selectAllLabel" contains different texts depending on user language)

    //custom button to select all rows of a data table
    $.fn.dataTable.ext.buttons.selectAll = {
        text: selectAllLabel,
        className: "selectAllButton",
        action: function ( e, dt, button, config ) {
            dt.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
                dt.row(this).select();
            });
        }
    };
    

    This is how you use it in your data table definition. Very simple just add the button to your table buttons:

    buttons: [        
                {extend: "editInfomaSettings", editor: infomaContractEditor}, 
                    "selectAll",
                    "colvis"
    ],
    

    This is how it works: (The checkboxes aren't meant to select records but serve a different purpose; you notice the selection by the different color of the selected records)

    a) button "Alles auswählen = select all" not clicked:

    b) button "Alles auswählen = select all" clicked:

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    I took a quick look but didn't find the code that used for the select all checkbox. Can you post this?

    Are you using server side processing? If so that could be why on the displayed page is checked.

    Are you using the Select extension? If so then you would use rows().select() to select the rows.

    Kevin

This discussion has been closed.