Javascript 如何计算数据表中的整个行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30346748/
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-23 05:02:14  来源:igfitidea点击:

How to count the entire number of rows in a datatable

javascriptjquerydatatable

提问by lucifer

I am using a datatable for my application. How do I get the total count of the rows in the datatable onload? My code:

我正在为我的应用程序使用数据表。如何获取数据表中的总行数onload?我的代码:

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

How to get the total count of rows in the datatable onload

如何获取数据表中的总行数 onload

回答by Ruchan

$(document).ready(function() {
 //Initialize your table
 var table = $('#jobSearchResultTable').dataTable();
 //Get the total rows
 alert(table.fnGetData().length);
});

Source: https://stackoverflow.com/questions/3238047/jquery-datatables-row-count-across-pages

来源:https: //stackoverflow.com/questions/3238047/jquery-datatables-row-count-across-pages

Another method:

另一种方法:

table.fnSettings().fnRecordsTotal();

see which one works for you

看看哪一个适合你

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

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

回答by jpaik

If you're using the new DataTables (v1.10+), you don't have access to some legacied functions such as fnGetData().

如果您使用的是新的 DataTables (v1.10+),则您无权访问某些遗留函数,例如 fnGetData()。

Instead, try this:

相反,试试这个:

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

If you're using paging (which I was), and need the total count across all pages, try this:

如果您正在使用分页(我曾经使用过),并且需要所有页面的总数,请尝试以下操作:

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

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

Sources:

资料来源:

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

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

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

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

回答by bharat

Get response from serverside processing following way it works for me:

按照对我有用的方式从服务器端处理获取响应:

// 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
    },
    ...
});

回答by Praveen Kumar Thalluri

$('#jobSearchResultTable').dataTable().rows().count()

回答by wupendra

According to your description I assume that you have table with id="jobSearchResultTable".

根据您的描述,我假设您有 id="jobSearchResultTable" 的表。

Try this it could work for you.

试试这个它可能对你有用。

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

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

)};

)};

This will show the no of rows in the table.

这将显示表中的行数。

回答by Abdul khader

Please check out the following code

请查看以下代码

var table = $('#example').DataTable(); 
alert(
    'Number of row entries: '+
    table
        .column( 0 )
        .data()
        .length
);

回答by Abdo Bahhous

//get number of entire rows
table.rows().eq(0).length;

//获取整行数
table.rows().eq(0).length;

回答by jayson.centeno

I just read the table instance in console and got this in version 1.10.8

我刚刚在控制台中读取了表实例,并在 1.10.8 版中得到了这个

var table = $("#mytable").DataTable({})
var total = table.settings()[0].json.recordsTotal

Hope this helps

希望这可以帮助

回答by Satish

I tried most of the solution from the answers but not worked for me.

我尝试了答案中的大部分解决方案,但对我不起作用。

Some work but when we search,it shows same number instead number of rows after search.

一些工作,但当我们搜索时,它显示相同的数字而不是搜索后的行数。

 var tbl=$('#tbl_name').DataTable();
 console.log(tbl['context'][0]['aiDisplay'].length);

Its worked for me even after search,shows current number of rows after search.

即使在搜索后它也对我有用,显示搜索后的当前行数。