2015-09-04 53 views
1

我有一個java腳本來編寫,以檢查jquery數據表中選定行的數量。但計數不起作用。任何人都可以幫助確定我犯的錯誤。Javascript來統計jquery數據表中選定行的數量

我可能會很愚蠢請原諒和幫助我。

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

}); 
+0

發現你得到任何警告? –

+1

你的代碼不是很清楚。我認爲在計算行時不需要data()。還有一個類似的問題已經解答:http://stackoverflow.com/questions/1149958/jquery-count-number-of-rows-in-a-table – cezar

+1

'$('#jobsTable')。find('tr .selected')。'長度' –

回答

3

爲什麼不呢?

$('#jobsTable tbody').on('click', 'tr', function() { 
    $(this).toggleClass('selected'); 
    alert($('#jobsTable tbody tr.selected').length + ' row(s) selected'); 
}); 

它必須正常工作。如果不是 - 打電話給我。

+0

這不適合我。 – user3253099

+0

它對你有什麼好處? @ user3253099 – Legendary

+0

它顯示爲空。 – user3253099

1

我覺得

alert(table.rows('.selected').data().length +' row(s) selected'); 

應該

alert(table.rows('.selected').length +' row(s) selected'); 

那選擇將返回「選擇」

0

類的行數這裏有一個直接的,簡單的方法來做到這一點。使用DT的count()函數。

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

if (! table.data().count()) { 
    alert('Empty table'); 
} 

這來自於數據表網站本身上的例子,在https://datatables.net/reference/api/count()