jQuery 循环遍历 DataTables 表以获取所有单元格内容

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

Loop through DataTables table to get all cells content

jquerydatatables

提问by Zac

I am using jquery dataTablesto 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 :

我正在使用 jquery dataTables在我的网站上生成分页表。我需要运行一个从特定列中获取所有数据的进程。就像是 :

$('.testLink').click(function(){
            var cells = new Array();
            $('#myTable tr td').each(function(){
                cells.push($(this).html());
            });
            console.log(cells);
        });

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..

该示例包含所有内容,但我只需要一列 tds 中的信息。我想我可以通过向该行中的所有 tds 添加一个类来做到这一点,但我相信有更好的方法。这是一个奖励问题..

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 fnGetDatabut I am not getting it. Any ideas?

但我真正想知道的是如何让它与数据表一起工作?由于脚本隐藏了大部分表格以进行分页,因此该函数仅抓取可见的单元格。我玩过,fnGetData但我不明白。有任何想法吗?

回答by Diego

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);
    });

回答by charlietfl

Here's a method using fnGetData()

这是使用 fnGetData() 的方法

First get the data from plugin which will be all rows visible or not. Loop over each row data array, and push index=1( second cell) into new array

首先从插件中获取所有行是否可见的数据。循环遍历每一行数据数组,并将 index=1(第二个单元格)推入新数组

     oTable = $('#example').dataTable();

      var secondCellArray=[];
      $.each( oTable.fnGetData(), function(i, row){
          secondCellArray.push( row[1]);
    })

     console.log( secondCellArray)

EDit : working demo...look in console after render

编辑:工作演示......渲染后在控制台中查看

http://live.datatables.net/apixiv/edit#javascript,html

http://live.datatables.net/apixiv/edit#javascript,html

回答by Blazemonger

jQuery.mapcombined with fnGetData()makes for a compact solution. The following function will return a one-dimensional array containing all the values from obj_dtable's nth column:

jQuery.map结合起来fnGetData()形成一个紧凑的解决方案。以下函数将返回一个一维数组,其中包含obj_dtable的第nth 列中的所有值:

function getDataTableColumn(obj_dtable,n) {
    return $.map(obj_dtable.fnGetData(), function(val) {
        return val[n];
    });
};

回答by Ohgodwhy

You'll want to use the "EQ" selector. It starts at the index of "0", so if you have..

您需要使用“EQ”选择器。它从“0”的索引开始,所以如果你有..

<tr>
  <td>0</td>
  <td>1</td>
  <td>2</td>
  <td>3</td>
</tr>

Then by using

然后通过使用

 $("td").eq(3); // last one
 $("td").eq(2); //returns "2"

Make sense?

有道理?

http://api.jquery.com/eq-selector/

http://api.jquery.com/eq-selector/