Reputation: 91
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' );
}
});
Upvotes: 1
Views: 6514
Reputation: 273
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()
Upvotes: 0
Reputation: 2825
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"
Upvotes: 1
Reputation: 2242
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.
Upvotes: 4