检查jQuery DataTables中是否选择了任何行

das*_*vsk 4 jquery datatables jquery-datatables

我正在尝试编写一个函数来检查是否选择了任何表行.

我需要在任何<tr>点击时触发此功能.

谢谢.

这是我选择行的代码:

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

cor*_*ico 7

从Datatables 1.10开始,有一个可以使用的any()方法:

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

if ( table.rows( '.selected' ).any() )
    // Your code here
Run Code Online (Sandbox Code Playgroud)


Gyr*_*com 1

你的问题已经有了答案。下面的代码可以让您确定是否有任何选定的行。

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

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

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

}
Run Code Online (Sandbox Code Playgroud)

我假设你的代码中的某个地方有var oTable = $('#Request').DataTable(). 否则你可以使用$('#Request').DataTable().