0

我有以下表格初始化代码

var myTable = $jq11('#myTable').dataTable({
    "ajax": someUrl,
    "aoColumnDefs": [
        { "bSortable": false, "aTargets": [0, 6, 7] }
    ],
    "columns": [
        { 
            ...
        },
        ...
    ],
   // "deferRender": true,
    "dom": 'l<"#removeButtonDiv.removeButton">rtip',
    "filter": false,
    "initComplete": function(settings, json) {
        $('#removeButtonDiv').html('<input id="removeButton" type="button" value="Remove"  style="float:right; height: 25px;" disabled />');
    },
    "lengthMenu": [ [20, 40, 60, 80, 100], [20, 40, 60, 80, 100] ],
    "language": {
        "emptyTable": "No data to list",
        "infoFiltered": " "
    },
    "order": [[4, "desc"]],
    "processing": true,
    "drawCallback": function( settings ) {
        $.each(selected, function(index, value){
            $('#'+value).attr("checked", "checked");
        });
    },
    "serverSide": true
    //,"sPaginationType": "input"
});

alert($(myTable.fnGetNodes()).length);

总是显示 20 这是我的页面大小。因为我有 5 页的完整记录。不是应该显示 100。如果我缺少有关此 api 的任何内容,请告诉我。

谢谢。

4

3 回答 3

1

我对数据表的了解不是很大,但是在阅读了 API 并找到了这个 SO Post和一些来自这里的信息后,我得出的结论是,根据您使用的设置,fnGetNodes()行为会有所不同。

由于表正在服务器端处理,.fnGetNodes()因此只会获取当前生成的元素。Datatables 似乎没有直接的方法来获取服务器端处理的所有行,因为服务器只返回当前请求的行


如果您只想计算它们,那么返回的 ajax 响应应该包含总行数https://datatables.net/manual/server-side#Example-data

var myTable = $jq11('#myTable').dataTable({
    "ajax": {
       "url": someUrl,
       "dataSrc": function ( json ) {
          console.log('Total : ' + json.recordsTotal.length);
          return json;
        }
     },
     /* ... */
});
于 2015-02-04T15:58:04.757 回答
0
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,
});

function drawCallBack() {
    console.log(this.fnSettings().fnRecordsTotal());
}

检查小提琴:http: //jsfiddle.net/ns7mt3La/

于 2015-02-05T02:19:15.073 回答
0

如果要计算所有行,请使用以下命令:

  1. this.fnSettings().fnRecordsTotal()在您的回调方法中。
  2. fnGetNodes()将给出标签内的所有行,tbody但如果你想计算使用,fnGetNodes()那么使用如下:

    this.fnGetNodes().length  //will count all rows
    
于 2016-03-16T04:18:22.267 回答