简体   繁体   English

Javascript计算jquery数据表中选定行的数量

[英]Javascript to count the number of selected rows in jquery data table

I have a java script to written to check the number of selected rows in a jquery data table. 我编写了一个Java脚本来检查jquery数据表中选定行的数量。 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' );
    }

});

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. 使用DT的count()函数。

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() 这来自数据表网站本身的示例,该示例位于https://datatables.net/reference/api/count()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM