Prinu Philip
Prinu Philip

Reputation: 189

How to get number of rows in datatable after ajax load

"ajax": {

        "url": "<%=request.getContextPath()%>/Home?value=getreqs5",
        "data": function ( json,callback) {
            var oTable = $('#fifth').dataTable();

            // Get the length
            var count=oTable.fnGetData().length;
         return json;
        }
      }

Ajax data is successfully loaded in datatable.But the number of rows showing zero. How can i get number of rows in datatable after ajax return data?

Upvotes: 5

Views: 15866

Answers (2)

Rmaxx
Rmaxx

Reputation: 1187

Just adding for all people looking for how to get to the api in other usecases:

var table = $("#myTable").dataTable();   
table.on('draw', function (data) {
    console.log(table.page.info().recordsDisplay);
});

Upvotes: 4

John
John

Reputation: 4991

If you are using datatables 1.10 you can get the total rows number with

"ajax": {
     "url" : "<%=request.getContextPath()%>/Home?value=getreqs5",
     "dataSrc": function(res){
         var count = res.data.length;
         alert(count);

         return res.data;
     }
}

With the dataSrc you can get the ajax success callback. In this callback you can count the objects number than your Ajax returns.

If you want to know how many rows you have in all your Datatable including all rows in all pages you can do this in your datatables parameter but out your ajax parameter :

"initComplete": function(settings, json){ 
     var info = this.api().page.info();
     alert('Total records', info.recordsTotal);
 }

Upvotes: 15

Related Questions