Javascript to count the number of selected rows in jquery data table - Stack Overflow

admin2025-04-18  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' );
    }

});

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' );
    }

});
Share edited Sep 4, 2015 at 11:39 Ɛɔıs3 7,89911 gold badges53 silver badges83 bronze badges asked Sep 4, 2015 at 11:33 user3253099user3253099 912 silver badges11 bronze badges 4
  • do you get any alert? – Guruprasad J Rao Commented Sep 4, 2015 at 11:36
  • 1 Your code isn't very clear. I think you don't need data() when counting the rows. There is also a similar question already answered: stackoverflow./questions/1149958/… – cezar Commented Sep 4, 2015 at 11:38
  • 1 $('#jobsTable').find('tr.selected').length – Amit Soni Commented Sep 4, 2015 at 11:38
  • @AmitSoni This works for me ! – user3253099 Commented Sep 4, 2015 at 12:48
Add a ment  | 

3 Answers 3

Reset to default 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.

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"

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 es from an example on the data tables website itself, found at https://datatables/reference/api/count()

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744931825a275203.html

最新回复(0)