Check if any row is not selected in jQuery DataTables

I am trying to write a function to check if any of the table rows are selected.

I need this function to run on any click <tr>

.

Thank.

Here is my code for selecting rows:

$('#Request tbody').on('click', 'tr', function () {
     if ($(this).hasClass('selected')) {
          $(this).removeClass('selected');
     } else {
          oTable.$('tr.selected').removeClass('selected');
          $(this).addClass('selected');
     }
});

      

+4


source to share


4 answers


As of Datatables 1.10, there is an any () method that can be used:



var table = $('#example').DataTable();

if ( table.rows( '.selected' ).any() )
    // Your code here

      

+6


source


You already have the answer to your question. The code below will allow you to determine if there are any selected rows.

var $rows = oTable.$('tr.selected');

// If some rows are selected
if($rows.length){

// Otherwise, if no rows are selected
} else {

}

      



I am assuming there is somewhere in your code var oTable = $('#Request').DataTable()

. Otherwise, you can use $('#Request').DataTable()

.

+1


source


Using the API.

var oTable = $("#yourtable").DataTable();

anyRowSelected = oTable.rows({selected : true}).indexes().length === 0 ? false : true;//false - nothing selected. true - 1 or more are selected.

      

0


source


var table = $('#foo-table').DataTable();
var selectedRows = table.rows({ selected: true });

      

This is the correct way to get the selected rows in DataTables 1.10.8.

0


source







All Articles