Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

#286: Resolving pagination issue #287

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
35 changes: 23 additions & 12 deletions app/templates/routes/views/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,32 @@ exports = module.exports = function (req, res) {
// Load the posts
view.on('init', function (next) {

var q = keystone.list('Post').paginate({
page: req.query.page || 1,
perPage: 10,
maxPages: 10,
filters: {
state: 'published',
},
})
.sort('-publishedDate')
.populate('author categories');

var q;

if (locals.data.category) {
q.where('categories').in([locals.data.category]);
q = keystone.list('Post').paginate({
page: req.query.page || 1,
perPage: 10,
maxPages: 10,
filters: {
state: 'published',
categories: locals.data.category,
},
});
} else {
q = keystone.list('Post').paginate({
page: req.query.page || 1,
perPage: 10,
maxPages: 10,
filters: {
state: 'published',
},
})
}

q.sort('-publishedDate')
.populate('author categories');

q.exec(function (err, results) {
locals.data.posts = results;
next(err);
Expand Down
16 changes: 8 additions & 8 deletions app/templates/templates/default-hbs/views/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ module.exports = function () {

// might be a ghost helper
// used for pagination urls on blog
_helpers.pageUrl = function (pageNumber, options) {
return '/blog?page=' + pageNumber;
_helpers.pageUrl = function (pageNumber, category, options) {
return '/blog' + (category ? '/' + category : '') +'?page=' + pageNumber;
};

// create the category url for a blog-category page
Expand Down Expand Up @@ -227,7 +227,7 @@ module.exports = function () {
return options.inverse(this);
};

_helpers.paginationNavigation = function (pages, currentPage, totalPages, options) {
_helpers.paginationNavigation = function (pages, currentPage, totalPages, category, options) {
var html = '';

// pages should be an array ex. [1,2,3,4,5,6,7,8,9,10, '....']
Expand All @@ -247,7 +247,7 @@ module.exports = function () {
}

// get the pageUrl using the integer value
var pageUrl = _helpers.pageUrl(page);
var pageUrl = _helpers.pageUrl(page, category);
// wrapup the html
html += '<li' + liClass + '>' + linkTemplate({ url: pageUrl, text: pageText }) + '</li>\n';
});
Expand All @@ -256,20 +256,20 @@ module.exports = function () {

// special helper to ensure that we always have a valid page url set even if
// the link is disabled, will default to page 1
_helpers.paginationPreviousUrl = function (previousPage, totalPages) {
_helpers.paginationPreviousUrl = function (previousPage, category) {
if (previousPage === false) {
previousPage = 1;
}
return _helpers.pageUrl(previousPage);
return _helpers.pageUrl(previousPage, category);
};

// special helper to ensure that we always have a valid next page url set
// even if the link is disabled, will default to totalPages
_helpers.paginationNextUrl = function (nextPage, totalPages) {
_helpers.paginationNextUrl = function (nextPage, totalPages, category) {
if (nextPage === false) {
nextPage = totalPages;
}
return _helpers.pageUrl(nextPage);
return _helpers.pageUrl(nextPage, category);
};


Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<ul class="pagination">
<li {{#unless data.posts.previous}}class="disabled"{{/unless}}>
<a href="{{paginationPreviousUrl data.posts.previous}}">
<a href="{{paginationPreviousUrl data.posts.previous filters.category}}">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
</li>
{{{paginationNavigation data.posts.pages data.posts.currentPage data.posts.totalPages}}}
{{{paginationNavigation data.posts.pages data.posts.currentPage data.posts.totalPages filters.category}}}
<li {{#unless data.posts.next}}class="disabled"{{/unless}}>
<a href="{{paginationNextUrl data.posts.next data.posts.totalPages}}">
<a href="{{paginationNextUrl data.posts.next data.posts.totalPages filters.category}}">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</li>
Expand Down