superRaytin / paginationjs

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

getTotalNumber (and getTotalPage) methods return wrong results when data is loaded asynchronously #43

Closed manish-in-java closed 6 years ago

manish-in-java commented 7 years ago

I have the following code:

$(selector).pagination({
    formatNavigator      : function() {
        return "showing page <%= currentPage %> of <%= totalPage %> for <%= totalNumber %> records";
    }
    , showNavigator      : true
    , totalNumberLocator : function(response) { return response.total; }
});

Here, response is a JSON object returned from the server and it correctly contains a total field. When total is greater than zero, the navigator correctly shows something like showing page 9 of 1039 for 10396 records.

However, when total is zero, the navigator incorrectly shows showing page 1 of 1 for 1 records, which is incorrect, because neither is there any record nor is the first page being displayed.

I have traced the problem to the getTotalNumber method, which is coded as:

return this.model.totalNumber || attributes.totalNumber || 1;

Naturally, when both this.model.totalNumber and attributes.totalNumber are undefined, or set to zero, this method returns 1. This is why the navigator incorrectly refers to 1 record and the first page.

I think that the getTotalNumber method should return zero when the total number of records is undefined or unknown.

superRaytin commented 6 years ago

Thanks for the feedback, i will fix it and bump a new version later.