Javascript jQuery DataTables - 访问所有行数据

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

jQuery DataTables - Access all rows data

javascriptjqueryhtmlajaxdatatables

提问by jmvtrinidad

I'm using jQuery DataTables and want to copy all rows (save in JavaScript array) upon clicking the header checkbox.

我正在使用 jQuery DataTables 并希望在单击标题复选框时复制所有行(保存在 JavaScript 数组中)。

First Page

第一页

I want to find where jQuery DataTables store the HTML for remaining page of rows, so I can navigate through JavaScript then check it there or set property checked to true.

我想找到 jQuery DataTables 为剩余行页面存储 HTML 的位置,以便我可以浏览 JavaScript,然后在那里检查它或将属性设置为 true。

Something like this one.

像这样的东西。

enter image description here

在此处输入图片说明

Other information:

其他信息:

  • I use data from an ajax source(serverside:false), all data is returned.
  • When I click page 1, all the rows remain Checked.
  • 我使用来自 ajax 源(serverside:false)的数据,返回所有数据。
  • 当我单击第 1 页时,所有行都保持选中状态。

采纳答案by jmvtrinidad

I find the generated element by jQuery DataTables using this code, and I can copy the whole trelement that hides when paging the DataTables.

我使用此代码通过 jQuery DataTables 找到生成的元素,并且我可以复制tr在分页 DataTables 时隐藏的整个元素。

$('#example').DataTable().rows().iterator('row', function(context, index){
    var node = $(this.row(index).node()); 
    //node.context is element of tr generated by jQuery DataTables.
});

回答by Gyrocode.com

SOLUTION

解决方案

There are many methodsthat could be used for that purpose. You can use rows().data()to get the data for the selected rows.

许多方法可用于该目的。您可以使用rows().data()来获取所选行的数据。

Example:

例子:

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

var data = table
    .rows()
    .data();

alert( 'The table has ' + data.length + ' records' );

DEMO

演示

See this jsFiddlefor code and demonstration.

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

回答by Vineela Thonupunuri

If you do this:

如果你这样做:

$('#table').DataTable().rows().data(); 

you get a lot of unnecessary data.

你会得到很多不必要的数据。

If you just want the table data you can do this:

如果你只想要表数据,你可以这样做:

$('#table').DataTable().rows().data().toArray();

回答by FOD

Using

使用

tableObject.rows().data() 

will return all the data from the table.

将返回表中的所有数据。