从Datatables检索可见数据 [英] Retrieving visible data from Datatables

查看:176
本文介绍了从Datatables检索可见数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Datatables显示具有不同数量信息行的三列。



表被设置为使得当列标题被单击时参照该列排序。我试图使用fnGetData获取现在排序的可见数据。但是,我的尝试只是返回表初始化的原始数据。



这是一个大致如下的代码:



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

console.log(secondCellArray)
});


解决方案

fnGetData 方法是正确的(对于Datatables版本< = 1.10)提取数据的方式:



http://legacy.datatables.net/api#fnGetData



根据该功能的文档:


根据提供的参数,获取整个表,单个行或单个
单元格的数据。 p>

以下是该文档中的示例:

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

oTable $('tr')。click(function(){
var data = oTable.fnGetData(this);
// ...数组/数据行对象
});






注意:我原来的答案使用 fnGetData 没有参数(对我来说很久以前有效),文档建议应该仍然有效(整个表的数据),但是由于答案已经下降-voted和文档没有具体提供其使用的示例,我将避免推荐使用它。






当然,旧的Datatables是可怕的,所以很有可能(除非你有很多代码围绕旧的数据文件),最好的办法是升级到最新版本(或者完全升级到另一个表格/网格库) )。


I am currently using Datatables to display three columns with a varying number of rows of information.

The table is setup so that when a column header is clicked the table is sorted in reference to that column. I am trying to use fnGetData to get the visible data that is now sorted. However, my attempt simply returns the original data the table was initialized with.

This is a roughly what the code looks like:

$("#example").click(function() {
           oTable = $('#' + tableName).dataTable();

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

             console.log( secondCellArray)
});

解决方案

The fnGetData method is the correct (for Datatables version <= 1.10) way to extract data:

http://legacy.datatables.net/api#fnGetData

Per that documentation that function will:

Get the data for the whole table, an individual row or an individual cell based on the provided parameters.

Here's an example from that documentation:

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

oTable.$('tr').click( function () {
   var data = oTable.fnGetData( this );
   // ... do something with the array / object of data for the row
});


NOTE: My original answer mentioned using fnGetData without an argument (which worked for me a long time ago), and the docs suggest that should still work ("the data for the whole table"), but since that answer got down-voted and documentation doesn't specifically provide an example of its usage, I'm going to avoid recommending using it that way.


Of course, the old Datatables is kind of terrible, so most likely (unless you have a lot of code built around the old Datables) your best bet is to upgrade to the newest version (or to another table/grid library entirely).

这篇关于从Datatables检索可见数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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