2015-05-20 37 views
2

我正在爲我的應用程序使用數據表,我的需要是我想要數據表onload中的行的總數。如何獲得該。在這裏我的代碼我我張貼如何統計數據表中的整行數

$(document).ready(function() { 
$('#jobSearchResultTable').dataTable({ 
     responsive: true, 
     "scrollY": 500, 
     "scrollCollapse": true, 
     "jQueryUI":  true, 
     "aaSorting": [] 

    }); 
)}; 

如何獲得行的總數在數據表中的onload

+0

dataTable未定義顯示 – lucifer

回答

4
$(document).ready(function() { 

// Initialize your table 
var table = $('#jobSearchResultTable').dataTable(); 

// Get the total rows 
alert(table.fnGetData().length); 
}); 

來源:jquery datatables row count across pages

另一種方法:

table.fnSettings().fnRecordsTotal(); 

看看哪一個對你

來源:http://datatables.net/forums/discussion/2278/how-to-get-number-of-rows

+0

關於這個答案的事情是,我字面上,沒有使用過這個數據表。我只是搜索了一下,找到了答案。因此,OP是一個懶惰的傢伙。 – Ruchan

+0

不適用於我,表格是動態填充的。 –

1

根據你的描述我假定你有ID =「jobSearchResultTable」表。

試試這個可以爲你工作。

$(document).ready(function() { 
    $('#jobSearchResultTable').dataTable({ 
    responsive: true, 
    "scrollY": 500, 
    "scrollCollapse": true, 
    "jQueryUI":  true, 
    "aaSorting": [] 

    }); 
    var count=0; 
    $('#jobSearchResultTable tr').each(function(){ 
     count++; 
    }); 
    alert(count); 

)};

這將顯示錶中的行數。

+0

,但我想要總行數,即它顯示當前頁行數,即如果該表有50行,那麼我希望計數爲50,而不是每個10 – lucifer

+0

我認爲這是正確答案爲你(http://stackoverflow.com/questions/3238047/jquery-datatables-row-count-across-pages?lq=1) – wupendra

0

//獲取整個行
數 table.rows()。eq(0).length;

+0

這應該是一個評論,而不是答案。我知道你沒有評論特權,但你可以在一天內獲得足夠的聲望...... – Badacadabra

2

如果您使用新的DataTables(v1.10 +),則無法訪問某些傳統函數,如fnGetData()。

相反,試試這個:

var table = $('#table_id').DataTable(); 
var table_length = table.data().count(); 

如果您使用分頁(我是),並且需要在所有頁面的總數,試試這個:

var table = $('#table_id').DataTable({ 
    'paging': true 
}); 

var length = table.page.info().recordsTotal; 

來源:

https://datatables.net/reference/api/count()

https://datatables.net/reference/api/page.info()

+0

table.page.info()。recordsTotal;這完美地工作 –

+0

table.data()。count()給出數據表中的單元格總數(tds)。 table.data()。rows()。count()給你的行數。 – Srinivas