Pagination with Javascript and MongoDB
Create Pagination with Node.js, MongoDB, Express and EJS Step by Step from Scratch
Suppose we have a collection contains many of data (products for instance) and we need to view all on website. We can just output products on page but this looks not so clean. In such cases we need a pagination.
In this article I’ll tell about how to make pagination using Node.js, MongoDB, Express.js, EJS and Bootstrap from scratch. We will create new database, declare data collection, fill this collection with data and output contents to page with pagination.
This article is generally for beginners who interested in web development so I will try to make all explanations very detailed. But maybe this article will be useful for an experienced reader.
After reading this article, you will be able to create pagination from scratch, not like this:
it’s a bad practice.
You will be able to create pagination from scratch like this:
For reading this article you need to have installed Node.js and MongoDB. You can download and install it from official websites:
Node.js - https://nodejs.org/en/
Get started by creating a new folder for this project, and name it anything you like. Then, inside that folder, create additional folders and files to match the following structure:
and install necessary dependencies to work:
Create a Server and Connect to MongoDB
Open
server.js
file and create simple express.js server:
In this file we create connection to MongoDB, enable
body-parser
middleware for parse incoming request bodies and use ejs
template engine.Declare Collection and Fill DataBase
Before create routes to filling our database we need declare model for products collection. Products will have a name, category name, price and cover image.
Open
models/product.js
and paste following code:Later In this article I will provide the code for quickly filling the database with fake data using faker.js
Now we can create a
POST
route and new page with HTML form to fill collection with products.
Open
routes/main.js
file and paste this code:
In this code we created
GET
route to show add-products
page with HTML form and POST
route that we will use to add new products to MongoDB. Also in this case as product cover image I use fake image from faker.js library.About how to upload files using Node.js and Multer you can read in my article: Upload files to server using Node.js and Multer package.
We need to use this routes in our server. To do that add following code to
server.js
file:
Let’s create
add-product
page with form to send a POST
request to our POST
route.
Open
views/main/add-product.ejs
file and paste following HTML:
Now you can run your MongoDB, then run server with
node server.js
command and go to http://localhost:8080/add-product
page in your browser and add new items to MongoDB.
For convenient viewing and editing MongoDB contents on your desktop I recommend to use MongoDB Compass.
Create new product:
See in MongoDB Compass:
Quick Filling DataBase Using Faker.js
Open
routes/main.js
file and paste following code:
Each time you will go to
http://localhost:8080/generate-fake-data
to your database will insert 90 new items. After filling the database, you will be redirected to add-product
page.Output Products to Page with Pagination
First we need to create new route to render
products
page and output some necessary data to create pagination. Open routes/main.js
file and paste following code:
Explanation:
perPage
variable contains max number of items on each page, page
variable contains current page number.
First we are finding all documents in
Products
collection:
for each page we need to skip
((perPage * page) - perPage)
values (on the first page the value of the skip should be 0):
output only
perPage
items (9 in this case):
count all items in collection with
count()
(we will use this value to calculate the number of pages):
then render
main/products
page and send necessary data:
Create pagination page:
Open
views/main/products.ejs
and paste following EJS:
Explanation:
We will show pagination only if number of pages more than 0:
In this part of code:
we are check number of current page and if this value less than 5 we are output pagination links from 1 to
current_page + 4
:
and if this value more than 5 we will start output pagination link from
current_page - 4
to current_page + 4
, thus previous links on current_step - 5
will be hide.
Gif Demo:
Additional condition
&& i <= pages
in for
loop we are using for not to display the number of links more than the number of pages.
Here:
we check the number from which we begin the output and if this number not qual to
1
(read above) we are prepend ...
(to show that we have previous links).
The same way we are using at the end:
to append
...
to show that we have links ahead.
Finally at start and the end of pagination we toggle active of
Fist
and Last
links:
At the end let’s add some contents to
views/index.ejs
file:
and add new route to render this page to
routes/main.js
file:
Now you can run MongoDB with
mongod
command, node server.js
command, go to http://localhost:8080/generate-fake-data
to insert data to MongoDB and use pagination on http://localhost:8080/products/1
page.
Comments
Post a Comment