superRaytin / paginationjs

A jQuery plugin to provide simple yet fully customisable pagination.
http://pagination.js.org
MIT License
912 stars 659 forks source link

Total page is always 1 page #126

Closed edwardanthony closed 1 year ago

edwardanthony commented 3 years ago

I tried to use a custom function for the data source. But the number of pages is always only one page.

$('#paginator').pagination({
    dataSource: function(done) {
        $.get(url, {
            pageNumber: this.pageNumber,
            pageSize: this.pageSize,
        }).done(function(response) {
            this.totalNumber = response.total;
            done(response.data);
        });
    },
    pageSize: 9,
    callback: function (data) {
        reloadList(data);
    }
});

I logged the pagination data, it shows that the totalNumber is always the same as the pageSize which causing it to only have only one page.

I tried to set the totalNumber variable manually. But it didn't work.

Sindrenj commented 3 years ago

I had the same problem and found out from a different issue that you could do this:

If the jsonresult is like this:

{
"totalCount" : 27,
 "data" : ["data", "data" ..]
}

Then do this:

 totalNumberLocator: function (response) {
   return response.totalCount;
},

Full example:

 $('#logs').pagination({
          dataSource: url,
          locator: 'userLogs',
          alias: 'page',
          pageSize: 10,
          ajax: {
             beforeSend: function() {
                dataContainer.html('Loading logs..');
             }
          },
          totalNumberLocator: function (response) {
            return response.totalCount;
          },
          callback: function(data, pagination) {

            // template method of yourself
            var html = template(data);

            dataContainer.html(html);
          }
      });

Hope this helps!