循环通过DataTables表获取所有单元格的内容 [英] Loop through DataTables table to get all cells content

查看:479
本文介绍了循环通过DataTables表获取所有单元格的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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表获取所有单元格的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆