Permalink
Browse files

Fix #299 - pager calculations.

  • Loading branch information...
1 parent 2ae0b4f commit f5f5b912a26c3a7a60d4cfc50b4317b5a4c09b9e @mleibman committed Apr 14, 2012
Showing with 10 additions and 13 deletions.
  1. +4 −8 controls/slick.pager.js
  2. +1 −1 examples/example4-model.html
  3. +5 −4 slick.dataview.js
@@ -14,18 +14,14 @@
function getNavState() {
var cannotLeaveEditMode = !Slick.GlobalEditorLock.commitCurrentEdit();
var pagingInfo = dataView.getPagingInfo();
- var lastPage = Math.ceil(pagingInfo.totalRows / pagingInfo.pageSize) - 1;
- if (lastPage < 0) {
- lastPage = 0;
- }
+ var lastPage = pagingInfo.totalPages - 1;
return {
canGotoFirst: !cannotLeaveEditMode && pagingInfo.pageSize != 0 && pagingInfo.pageNum > 0,
canGotoLast: !cannotLeaveEditMode && pagingInfo.pageSize != 0 && pagingInfo.pageNum != lastPage,
canGotoPrev: !cannotLeaveEditMode && pagingInfo.pageSize != 0 && pagingInfo.pageNum > 0,
canGotoNext: !cannotLeaveEditMode && pagingInfo.pageSize != 0 && pagingInfo.pageNum < lastPage,
- pagingInfo: pagingInfo,
- lastPage: lastPage
+ pagingInfo: pagingInfo
}
}
@@ -45,7 +41,7 @@
function gotoLast() {
var state = getNavState();
if (state.canGotoLast) {
- dataView.setPagingOptions({pageNum: state.lastPage});
+ dataView.setPagingOptions({pageNum: state.pagingInfo.totalPages - 1});
}
}
@@ -139,7 +135,7 @@
if (pagingInfo.pageSize == 0) {
$status.text("Showing all " + pagingInfo.totalRows + " rows");
} else {
- $status.text("Showing page " + (pagingInfo.pageNum + 1) + " of " + (state.lastPage + 1));
+ $status.text("Showing page " + (pagingInfo.pageNum + 1) + " of " + pagingInfo.totalPages);
}
}
@@ -272,7 +272,7 @@
});
dataView.onPagingInfoChanged.subscribe(function (e, pagingInfo) {
- var isLastPage = pagingInfo.pageSize * (pagingInfo.pageNum + 1) - 1 >= pagingInfo.totalRows;
+ var isLastPage = pagingInfo.pageNum == pagingInfo.totalPages - 1;
var enableAddRow = isLastPage || pagingInfo.pageSize == 0;
var options = grid.getOptions();
View
@@ -129,11 +129,11 @@
function setPagingOptions(args) {
if (args.pageSize != undefined) {
pagesize = args.pageSize;
- pagenum = Math.min(pagenum, Math.ceil(totalRows / pagesize));
+ pagenum = pagesize ? Math.min(pagenum, Math.max(0, Math.ceil(totalRows / pagesize) - 1)) : 0;
@dbradicich
dbradicich May 1, 2012

Curious why

Math.max(0,Math.ceil(totalRows / pagesize) - 1)

and not just

Math.floor(totalRows/pagesize) ?

}
if (args.pageNum != undefined) {
- pagenum = Math.min(args.pageNum, Math.ceil(totalRows / pagesize));
+ pagenum = Math.min(args.pageNum, Math.max(0, Math.ceil(totalRows / pagesize) - 1));
}
onPagingInfoChanged.notify(getPagingInfo(), null, self);
@@ -142,7 +142,8 @@
}
function getPagingInfo() {
- return {pageSize: pagesize, pageNum: pagenum, totalRows: totalRows};
+ var totalPages = pagesize ? Math.max(1, Math.ceil(totalRows / pagesize)) : 1;
+ return {pageSize: pagesize, pageNum: pagenum, totalRows: totalRows, totalPages: totalPages};
}
function sort(comparer, ascending) {
@@ -676,7 +677,7 @@
// if the current page is no longer valid, go to last page and recalc
// we suffer a performance penalty here, but the main loop (recalc) remains highly optimized
if (pagesize && totalRows < pagenum * pagesize) {
- pagenum = Math.floor(totalRows / pagesize);
+ pagenum = Math.max(0, Math.ceil(totalRows / pagesize) - 1);
diff = recalc(items, filter);
}

0 comments on commit f5f5b91

Please sign in to comment.