Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server side pagination using DataTables plugin

The server is returning 15 records per page and the total records are over 2000. I'd like to display first 15 records and then on every click of the 'Next' button , display the remaining all records, (15 per page). For this do we do a server side pagination or client side???

Here is my table and the attributes I'm using for pagination in DataTables:

  var tableData = self.accountCollection.getData();

        var tableColumns = this.accountCollection.getColumns();
        var totalRecs = this.accountCollection.length;

        //create the UI grid containing the list of items

        this.resultsTable = tableEl.dataTable( {
            "bServerSide": true,
            "sEcho": 3,
            "iTotalRecords": totalRecs,
            "iTotalDisplayRecords": 15,
            "aaData": tableData,
            "aoColumns": tableColumns,
            "aaSorting": [[1,'asc']],
           });



getData: function () {

        var returnData = [];
        $.each(this.models, function (idx, accountModel) {
            returnData.push(accountModel.attributes);
        });
        return returnData;
    },

The returnData will return me an Object that has fields I will be populating I a table.

Object returned (roughly):

Object
 accountName: "No Company"
 address1: "1234 asdf"
  address2: ""
  billingAcctId: null
  billingSystem: null
  city: "mountain view"
  comments: null
   country: "USA"

The getData() function will be then called to return the data from the database using:

var tableData = this.accountCollection.getData()

So basically tableData will have the necessary fields and values to display in table. Now I may have more than 1000 records returned from the server. Hence I needed pagination.

The one in fiddle is what I tried and does not have any impact on the paginatin.

I think I have the basic pagination that comes with the DataTables, but now I need to have a server side, to get only 15 records to display at a time, and on click of 'next' and 'prev' button i should be able to make ajax calls to get the remaining records 15 per page.

I hope this helps you understand better. Please let me know if you need more details.

How can I achieve pagination using DataTables?

Thanks

like image 278
user1234 Avatar asked Sep 19 '14 00:09

user1234


1 Answers

enter image description here

Pagination works total displayed record you need to perform following minimum changes.

"iTotalDisplayRecord" will be total filtered records

like image 132
Pranav Labhe Avatar answered Sep 27 '22 21:09

Pranav Labhe