jQuery jquery数据表跨页面的行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3238047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:05:36  来源:igfitidea点击:

jquery datatables row count across pages

jquerydatatables

提问by Pritish

I'm using the jQuery DataTables plug-in for my HTML table.

我正在为我的 HTML 表格使用 jQuery DataTables 插件。

Is there a way to get the row count of the number of rows in my table across pages.

有没有办法跨页获取表格中行数的行数。

For example, if I have 70rows in my table, and let's say 50of them get displayed on the 1st page, and 20on the 2nd page. Is there a way to get the count of 70?

例如,如果70我的表中有行,假设50它们显示在第一页和20第二页上。有没有办法得到计数70

I've tried all the suggestions included in this post: jQuery: count number of rows in a table

我已经尝试了这篇文章中的所有建议: jQuery: count number of rows in a table

This includes:

这包括:

var rowCount = $('#myTable tr').length;
var rowCount = $('#myTable tr').size();
var rowCount = $('#myTable >tbody >tr').length;
var rowCount = $("#myTable").attr('rows').length;

var rowCount = $('#myTable tr').length;
var rowCount = $('#myTable tr').size();
var rowCount = $('#myTable >tbody >tr').length;
var rowCount = $("#myTable").attr('rows').length;

But all the above suggestions seem to return the number of rows on the existing page (in this case, 50and not 70).

但是上述所有建议似乎都返回现有页面上的行数(在这种情况下,50而不是70)。

回答by Matt Peterson

It looks like DataTables is removing the rows that aren't on the current page from the DOM, so you aren't going to be able to count them with a jQuery selector. You'll have to use the DataTables API, specifically the fnGetDatafunction:

看起来 DataTables 正在从 DOM 中删除不在当前页面上的行,因此您将无法使用 jQuery 选择器对它们进行计数。您必须使用DataTables API,特别是fnGetData函数:

$(document).ready(function() {

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

    // Get the length
    alert(oTable.fnGetData().length);
} );

回答by Gyrocode.com

SOLUTION

解决方案

For DataTables 1.10

对于数据表 1.10

Use page.info()to retrieve information about the table as shown below.

使用page.info()如下图所示检索有关表的信息。

Property recordsTotalwill hold total number of records in the table.

属性recordsTotal将保存表中的记录总数。

var table = $('#example').DataTable({
    "initComplete": function(settings, json){ 
        var info = this.api().page.info();
        console.log('Total records', info.recordsTotal);
        console.log('Displayed records', info.recordsDisplay);
    }
});

DEMO

演示

See this jsFiddlefor code and demonstration.

有关代码和演示,请参阅此 jsFiddle

NOTES

笔记

This solution will work for both client-side and server-side processing modes as long as you use page.info()once data has been retrieved, for example in initCompletecallback function.

只要您page.info()在检索到数据后使用此解决方案,例如在initComplete回调函数中,它就适用于客户端和服务器端处理模式。

回答by dotjoe

When using server-side paging you can access the total record count like so...

使用服务器端分页时,您可以像这样访问总记录数......

dt.fnSettings()._iRecordsTotal;

If you're using server side paging, you could hook into the fnDrawCallback and update your html whenever the table is drawn...

如果您使用服务器端分页,则可以挂入 fnDrawCallback 并在绘制表格时更新您的 html...

$('#Table').dataTable({
    "fnDrawCallback": function (oSettings) {
        alert(oSettings._iRecordsTotal);
     }
});

回答by radbyx

If you are using the Scroller extension, and use search to filter down the rows to a subset, you can get the length of that subset like this

如果您使用的是 Scroller 扩展,并使用搜索将行过滤为子集,则可以像这样获得该子集的长度

$('#foo').dataTable().fnSettings().aiDisplay.length;

回答by Navnath Kumbhar

In the JSON response of your datatable ajax call, we get 'iTotalDisplayRecords' as one of the property of that response JSON object. You can directly use the value of 'iTotalDisplayRecords' as number of records in your Data Table.

在数据表 ajax 调用的 JSON 响应中,我们将“iTotalDisplayRecords”作为该响应 JSON 对象的属性之一。您可以直接使用“iTotalDisplayRecords”的值作为数据表中的记录数。