不使用 fnGetNodes() 计算所有行

Does not count all rows with fnGetNodes()

我有以下table初始化代码

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。

谢谢。

我对数据tables 的了解不是很多,但在阅读 API 并找到 this SO Post and some info from here 后,我得出的结论是,根据您使用的设置 fnGetNodes() 行为不同。

由于 table 正在服务器端处理,.fnGetNodes() 将只会获得 当前生成的元素 。 Datatables 似乎没有通过服务器端处理获取所有行的直接方法,因为服务器 returns 只有 当前请求的行


如果您只想对它们进行计数,则返回的 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;
        }
     },
     /* ... */
});
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());
}

检查 fiddle : http://jsfiddle.net/ns7mt3La/

如果要计算所有行数,请使用以下内容:

  1. this.fnSettings().fnRecordsTotal() 在你的回调方法中。
  2. fnGetNodes() 将给出 tbody 标签内的所有行,但是如果你想使用 fnGetNodes() 进行计数,那么可以这样使用:

    this.fnGetNodes().length  //will count all rows