pise pise - 4 years ago 1294
Javascript Question

Select All rows in Jquery Datatables

I have a datatables in which I can select multiple row onclick but how can I select all the row on a click of button and at the same time all rows are highlighted with selection (Can you please give example of selection for current page and all the pages). I have written a code to get multiple selected value.

Checkbox will also do but then how to get selected value.

Below is code for single and multiple selection.

var oTable = $("#example").dataTable();

$('#example tbody').on('click', 'tr', function() {
$(this).toggleClass('selected');

});


Code to get selected value on button submit.

var row = oTable.rows('.selected').data();

var jsonArr = "[";

if(row != null && row.length){

for (var i = 0; i < row.length; i++) {
var row1 = row[i]; // this will give me one row details
// row1[0] will give me column details
jsonArr = jsonArr + "{\"ID\":" + row1[0] + "},";

}
jsonArr = jsonArr + "]";

Answer Source

What will probably help you is TableTools extension. There is an example with select_all and select_none buttons, and those work for all pages.

One default limitation is that select_all will ignore current filtering, but that is easy to solve using the code below. Providing "true" argument to fnSelectAll enables the filter-aware selection.

tableTools: {
    sRowSelect: 'multi',
    aButtons: [
        {
            sExtends: 'select_all',
            sButtonText: 'Select All',
            fnClick: function (nButton, oConfig, oFlash) {
                TableTools.fnGetInstance('example').fnSelectAll(true);
            }
        }
    ]
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download