1

I have a java script to written to check the number of selected rows in a jquery data table. But the count doesn't works. Can anyone help in identifying the bug i made.

I may be silly please excuse and help me out.

var table = $("#jobsTable").dataTable();

$('#jobsTable tbody').on( 'click', 'tr', function () {
    $(this).toggleClass('selected');
    alert( table.rows('.selected').data().length +' row(s) selected' );
});

$('#batchAction').click( function () {

    alert("button is clicked");
    alert( table.rows('.selected').data().length +' row(s) selected' );
    var selectedRows = table.rows('.selected').data().length ;

    if( selectedRows === 0){
        alert("Zero rows selected. Please select jobs to proceed with bulk Operation");
        alert( table.rows('.selected').data().length +' row(s) selected' );
    }

});
w3spi
  • 4,380
  • 9
  • 47
  • 80
user3253099
  • 91
  • 2
  • 11

3 Answers3

4

why just not?

$('#jobsTable tbody').on( 'click', 'tr', function () {
  $(this).toggleClass('selected');
  alert( $('#jobsTable tbody tr.selected').length + ' row(s) selected' );
});

It have to works right. If it is not - call me.

Legendary
  • 2,243
  • 16
  • 26
1

I think

alert( table.rows('.selected').data().length +' row(s) selected' );

should be

alert( table.rows('.selected').length +' row(s) selected' );

That selector would return the number of rows of class "selected"

Velimir Tchatchevsky
  • 2,812
  • 1
  • 16
  • 21
0

Here's a direct, simple way to do it. Use DT's count() function.

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

if ( ! table.data().count() ) {  
    alert( 'Empty table' );
}

This comes from an example on the data tables website itself, found at https://datatables.net/reference/api/count()

Steven Lacher
  • 273
  • 1
  • 2
  • 10