问题描述
我正在使用 jquery dataTables 在我的网站上生成分页表.我需要运行一个从特定列中获取所有数据的进程.类似的东西:
I am using jquery dataTables to generate the paginated table on my site. I need to run a process that grabs all of the data out of a particular column. Something like :
$('.testLink').click(function(){
var cells = new Array();
$('#myTable tr td').each(function(){
cells.push($(this).html());
});
console.log(cells);
});
该示例包含所有内容,但我只需要一列 tds 中的信息.我想我可以通过向该行中的所有 tds 添加一个类来做到这一点,但我相信有更好的方法.这是一个额外的问题..
That example grabs everything but I would need just the information from one column of tds. I guess I could do that by adding a class to all of the tds in that row but I am sure there is a better way. That is a bonus question..
但我真正想知道的是如何让它与数据表一起工作?由于脚本隐藏了大部分表格以进行分页,因此该函数仅抓取可见的单元格.我玩过 fnGetData
但我没有得到它.有什么想法吗?
but what I really want to know is how to get this to work with datatables? Because the script hides most of the table to put in pagination this function only grabs the cells that are visible. I played around with fnGetData
but I am not getting it. Any ideas?
推荐答案
要访问所有行,您可以:
To access all the rows, you can do:
var rows = $("#myTable").dataTable().fnGetNodes();
在你的情况下,这应该有效:
In your case, this should work:
$('.testLink').click(function(){
var cells = [];
var rows = $("#myTable").dataTable().fnGetNodes();
for(var i=0;i<rows.length;i++)
{
// Get HTML of 3rd column (for example)
cells.push($(rows[i]).find("td:eq(2)").html());
}
console.log(cells);
});
这篇关于循环遍历 DataTables 表以获取所有单元格内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!