3

我知道我可以使用函数 fnGetData() 但这只会让我获得我所在页面的数据。我想获取所有数据,即使是当前未显示的页面。我的目的是获取所有纬度和经度字段,以便我可以使用 Google Maps v3 将其显示在地图上。

我有这段代码,但同样,它只为我获取当前页面的数据。

var table = $('#example1 table').dataTable();
            var data = table.fnGetData();   
            console.log(data);
4

2 回答 2

1

当启用服务器端处理 ( 'serverSide': true) 时,实际上 1.9.x 函数fnGetData()和 1.10.x 函数data()都只返回当前页面的数据。

此行为可以通过查看服务器端处理示例并在控制台$('#example').dataTable().fnGetData()$('#example').DataTable().data().

为了检索所有数据,您需要发出一个单独的 AJAX 请求来模仿 DataTables 的行为。

由于您的问题已被标记datatables-1.10,我假设您使用的是 DataTables 1.10.x。您可以使用ajax.params(),它获取 DataTables 在最后一个 Ajax 请求中提交给服务器的数据。

function processAllRecords(){
   var req = $('#example1 table').DataTable().ajax.params();

   // Reset request parameters to retrieve all records
   req.start = 0;
   req.length = -1;
   req.search.value = "";

   // Request data
   $.ajax({
      'url': 'script.php',
      'data': req,
      'dataType': 'json'
   })
   .done(function(json){
      // json.data is array of data source objects (array/object),
      // one for each row
      console.log(json);
   });
}
于 2015-05-14T11:58:30.210 回答
-1
use the fnSettings().fnRecordsTotal() of the datatable. 
example : http://www.datatables.net/forums/discussion/2401/record-count-with-server-side-processing

我以前用过它,我想我可以给你一个片段:

var table = $('#table ').DataTable(
{
                        "dom" : 'rtp', // Initiate drag column
                        "fnDraw" : false,
                        "serverSide": true,
                        "ajax" : ... ...
                            }
                        }),
                      "fnDrawCallback" : drawCallBack,
});

unction drawCallBack() {
    console.log(this.fnSettings().fnRecordsTotal());
}
于 2015-02-05T09:21:15.213 回答