我正在为我的应用程序使用数据表。如何获取数据表中的总行数
onload
?我的代码:
$(document).ready( function() {
$('#jobSearchResultTable').dataTable({
responsive: true,
"scrollY": 500,
"scrollCollapse": true,
"jQueryUI": true,
"aaSorting": []
});
)};
如何获取数据表中的总行数
onload
如果您使用新的 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;
来源:
新版本更新
table.data().count()
参考:https://datatables.net/reference/api/count()
对于旧版本:
$(document).ready(function() {
//Initialize your table
var table = $('#jobSearchResultTable').dataTable();
//Get the total rows
alert(table.fnGetData().length);
});
来源:https://stackoverflow.com/questions/3238047/jquery-datatables-row-count-across-pages
另一种方法:
table.fnSettings().fnRecordsTotal();
看看哪一个适合您
来源:http://datatables.net/forums/discussion/2278/how-to-get-number-of-rows/
按照对我有用的方式从服务器端处理获取响应:
// tested with : DataTables 1.10.15
t1 = $('#item-list').DataTable({
"processing": true,
"serverSide": true,
"ajax":'url',
"drawCallback": function( settings, start, end, max, total, pre ) {
console.log(this.fnSettings().json); /* for json response you can use it also*/
alert(this.fnSettings().fnRecordsTotal()); // total number of rows
},
...
});
在 DataTables 库的官方 API 文档页面上显示,自 DataTables 版本 1.10.8 起,获取表中总行数的方法是
table.rows().count()
。
count()
API 页面的屏幕截图(请参阅第二个示例):
所以对于您的具体情况应该是这样的:
$('#jobSearchResultTable').dataTable().rows().count();
我尝试了答案中的大部分解决方案,但对我不起作用。
有些工作,但是当我们搜索时,它显示相同的数字,而不是搜索后的行数。
var tbl=$('#tbl_name').DataTable();
console.log(tbl['context'][0]['aiDisplay'].length);
即使在搜索后它也对我有用,显示搜索后的当前行数。
2021 年 10 月 Yajra 数据表
myTable.rows().count();
根据您的描述,我假设您有 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);
)};
这将显示表中的行数。
请查看以下代码
var table = $('#example').DataTable();
alert(
'Number of row entries: '+
table
.column( 0 )
.data()
.length
);
//获取整行数
table.rows().eq(0).length;
我刚刚在控制台中读取了表实例并在版本 1.10.8 中得到了这个
var table = $("#mytable").DataTable({})
var total = table.settings()[0].json.recordsTotal
希望这有帮助