top of page

Dynamic Pagination


<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); }); };

6 views0 comments

Recent Posts

See All

SQL UNION Operator UNION operator is used to combine the results of two or more SELECT statements Every SELECT statement within UNION must have the same number of columns The columns must also have si

JOIN clause is used to combine rows from two or more tables. INNER JOIN === selects records that have matching values in both tables SELECT Orders.OrderID, Customers.CustomersID, Orders.OrdersDate FRO

bottom of page