jquery datatables row count across pages

前端 未结 5 2030
礼貌的吻别
礼貌的吻别 2021-02-18 13:50

I\'m using the jQuery DataTables plug-in for my HTML table.

Is there a way to get the row count of the number of rows in my table across pages.

For example, if I

相关标签:
5条回答
  • 2021-02-18 14:20

    In the JSON response of your datatable ajax call, we get 'iTotalDisplayRecords' as one of the property of that response JSON object. You can directly use the value of 'iTotalDisplayRecords' as number of records in your Data Table.

    0 讨论(0)
  • 2021-02-18 14:22

    It looks like DataTables is removing the rows that aren't on the current page from the DOM, so you aren't going to be able to count them with a jQuery selector. You'll have to use the DataTables API, specifically the fnGetData function:

    $(document).ready(function() {
    
        // Initialize your table
        var oTable = $('#myTable').dataTable();
    
        // Get the length
        alert(oTable.fnGetData().length);
    } );
    
    0 讨论(0)
  • 2021-02-18 14:28

    When using server-side paging you can access the total record count like so...

    dt.fnSettings()._iRecordsTotal;
    

    If you're using server side paging, you could hook into the fnDrawCallback and update your html whenever the table is drawn...

    $('#Table').dataTable({
        "fnDrawCallback": function (oSettings) {
            alert(oSettings._iRecordsTotal);
         }
    });
    
    0 讨论(0)
  • 2021-02-18 14:34

    SOLUTION

    For DataTables 1.10

    Use page.info() to retrieve information about the table as shown below.

    Property recordsTotal will hold total number of records in the table.

    var table = $('#example').DataTable({
        "initComplete": function(settings, json){ 
            var info = this.api().page.info();
            console.log('Total records', info.recordsTotal);
            console.log('Displayed records', info.recordsDisplay);
        }
    });
    

    DEMO

    See this jsFiddle for code and demonstration.

    NOTES

    This solution will work for both client-side and server-side processing modes as long as you use page.info() once data has been retrieved, for example in initComplete callback function.

    0 讨论(0)
  • 2021-02-18 14:37

    If you are using the Scroller extension, and use search to filter down the rows to a subset, you can get the length of that subset like this

    $('#foo').dataTable().fnSettings().aiDisplay.length;
    
    0 讨论(0)
提交回复
热议问题