Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid zero max index and fall back to last page #2043

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions examples/resources/knockout.simpleGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,21 @@
this.columns = configuration.columns || getColumnsForScaffolding(ko.utils.unwrapObservable(this.data));

this.itemsOnCurrentPage = ko.computed(function () {
if (this.data().length === 0) {
return [];
}
var startIndex = this.pageSize * this.currentPageIndex();
if (startIndex !== 0 && startIndex >= ko.unwrap(this.data).length) {
this.currentPageIndex(Math.ceil(ko.unwrap(this.data).length / this.pageSize) - 1);
startIndex = this.pageSize * this.currentPageIndex();
}
return ko.utils.unwrapObservable(this.data).slice(startIndex, startIndex + this.pageSize);
}, this);

this.maxPageIndex = ko.computed(function () {
if (this.data().length === 0) {
return 1;
}
return Math.ceil(ko.utils.unwrapObservable(this.data).length / this.pageSize);
}, this);
}
Expand Down Expand Up @@ -66,8 +76,8 @@
<div class=\"ko-grid-pageLinks\">\
<span>Page:</span>\
{{each(i) ko.utils.range(1, maxPageIndex)}}\
<a href=\"#\" data-bind=\"click: function() { currentPageIndex(i) }, css: { selected: i == currentPageIndex() }\">\
${ i + 1 }\
<a href=\"#\" data-bind=\"click: function() { currentPageIndex(i - 1) }, css: { selected: i == currentPageIndex() + 1 }\">\
${ i }\
</a>\
{{/each}}\
</div>");
Expand Down Expand Up @@ -95,4 +105,4 @@
ko.renderTemplate(pageLinksTemplateName, viewModel, { templateEngine: templateEngine }, pageLinksContainer, "replaceNode");
}
};
})();
})();