[英]Allow selecting of row once for each datatable shown
我正在使用datatables.js
https://www.datatables.net/並遇到一個問題,我希望用戶為顯示的覆蓋率表選擇一行。
我已經嘗試了好幾種方法,但是無法正常工作,我的腳本檢查了所有數據表,而不是您要選擇的數據表。我理解這是因為在我的代碼中,我通過使用var table = $('table').DataTable();
但是我不知道如何指定它,因此它檢查是否在數據表的行之一上設置了selected
類。
var table = $('table').DataTable();
$('table tbody').on( 'click', 'tr', function () {
if ( $(this).hasClass('selected') ) {
$(this).removeClass('selected');
}
else {
table.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
} );
在on()
事件中,獲取行的表,而不使用函數外部定義的表。
碼
$('table tbody').on( 'click', 'tr', function () {
var table = $(this).closest("table");
if ( $(this).hasClass('selected') ) {
$(this).removeClass('selected');
}
else {
table.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
} );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.