<section class="pagination"> <% if ( currentPage !==1 && previousPage !==1) { %> <a href="?page=1">1</a> <% } %> <% if(hasPreviousPage){ %> <a href="?page=<%= previousPage %>"> <%= previousPage %> </a> <% } %> <a href="?page=<%= currentPage %>" class="active"> <%= currentPage %> </a> <% if(hasNextPage){ %> <a href="?page=<%= nextPage %>"> <%= nextPage %> </a> <% } %> <% if(lastPage !==currentPage && nextPage !==lastPage) { %> <a href="?page=<%= lastPage %>"> <%= lastPage %> </a> <% } %> </section>
This is the controller part
const ITEMS_PER_PAGE = 1;
exports.getIndex = (req, res, next) => { const page = +req.query.page || 1; let totalItems; Product.find() .countDocuments() .then(numProducts => { totalItems = numProducts; return Product.find() .skip((page - 1) * ITEMS_PER_PAGE) .limit(ITEMS_PER_PAGE) }) .then(products => { res.render('shop/index', { prods: products, pageTitle: 'Shop', path: '/', currentPage: page, hasNextPage: ITEMS_PER_PAGE * page < totalItems, hasPreviousPage: page > 1, nextPage: page + 1, previousPage: page - 1, lastPage: Math.ceil(totalItems / ITEMS_PER_PAGE) }); }) .catch(err => { console.log(err); }); };
Comments