I am trying to write a function to check if any of the table rows are selected.
我正在尝试编写一个函数来检查是否选择了任何表行。
I need this function to be triggered at any <tr>
click.
我需要在任何点击时触发此功能。
Thank you.
Here is my code for selecting rows:
这是我选择行的代码:
$('#Request tbody').on('click', 'tr', function () {
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
} else {
oTable.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
});
4 个解决方案
#1
As of Datatables 1.10, there is an any() method that can be used:
从Datatables 1.10开始,有一个可以使用的any()方法:
var table = $('#example').DataTable();
if ( table.rows( '.selected' ).any() )
// Your code here
#2
You have answer in your question already. The code below would let you determine if there are any selected rows.
你已经回答了你的问题。下面的代码可以让您确定是否有任何选定的行。
var $rows = oTable.$('tr.selected');
// If some rows are selected
if($rows.length){
// Otherwise, if no rows are selected
} else {
}
I assume that somewhere in your code you have var oTable = $('#Request').DataTable()
. Otherwise you can use $('#Request').DataTable()
.
我假设代码中的某处有var oTable = $('#Request')。DataTable()。否则你可以使用$('#Request')。DataTable()。
#3
Using the API.
使用API。
var oTable = $("#yourtable").DataTable();
anyRowSelected = oTable.rows({selected : true}).indexes().length === 0 ? false : true;//false - nothing selected. true - 1 or more are selected.
#4
var table = $('#foo-table').DataTable();
var selectedRows = table.rows({ selected: true });
This is proper way to get the selected rows as of DataTables 1.10.8.
这是从DataTables 1.10.8获取所选行的正确方法。
#1
As of Datatables 1.10, there is an any() method that can be used:
从Datatables 1.10开始,有一个可以使用的any()方法:
var table = $('#example').DataTable();
if ( table.rows( '.selected' ).any() )
// Your code here
#2
You have answer in your question already. The code below would let you determine if there are any selected rows.
你已经回答了你的问题。下面的代码可以让您确定是否有任何选定的行。
var $rows = oTable.$('tr.selected');
// If some rows are selected
if($rows.length){
// Otherwise, if no rows are selected
} else {
}
I assume that somewhere in your code you have var oTable = $('#Request').DataTable()
. Otherwise you can use $('#Request').DataTable()
.
我假设代码中的某处有var oTable = $('#Request')。DataTable()。否则你可以使用$('#Request')。DataTable()。
#3
Using the API.
使用API。
var oTable = $("#yourtable").DataTable();
anyRowSelected = oTable.rows({selected : true}).indexes().length === 0 ? false : true;//false - nothing selected. true - 1 or more are selected.
#4
var table = $('#foo-table').DataTable();
var selectedRows = table.rows({ selected: true });
This is proper way to get the selected rows as of DataTables 1.10.8.
这是从DataTables 1.10.8获取所选行的正确方法。