Node JS Pagination using in Mean Stack ( Node js mongodb pagination )



Nodejs Pagination

Ejs file[Html page] :
Node js mongodb pagination

     <% if (pageCount > 1) { %>
      <ul class="pagination">
<% if (currentPage > 1) { %>
                                <li><a href="/userdetails?page=1">&laquo;</a></li>
                <% } %>
                <% var i = 1;
                if (currentPage > 5) {
                                i = +currentPage - 4;
                } %>
                <% if (i !== 1) { %>
                                <li class="disabled"><a href="#">...</a></li>
                <% } %>
<% for (var j = i; j<=pageCount; j++) { %>
                <% if (currentPage == j) { %>
                                <li class="active"><span><%= j %> <span class="sr-only">(current)</span></span></li>
                <% } else { %>
                                <li><a href="/userdetails?page=<%= j %>"><%= j %></a></li>
                <% } %>
                <% if (j == (+currentPage + 4)) { %>
                                <li class="disabled"><a href="#">...</a></li>
                <% break; } %>
                <% } %>
<% if (currentPage != pageCount) { %>
                                <li><a href="/userdetails?page=<%= pageCount %>">&raquo;</a></li>
                <% } %>
   </ul>  <% } %>


    Routes.js :
         

               var totalRec = 0,
                pageSize  = 5,
                pageCount = 0;
                var currentPage = 1;
                app.get('/userdetails',  function (req, res) {
                                Register.find({}).exec(function(err, Register) {
                                   if (err) throw err;
                                                totalRec      = Register.length;
                                                pageCount     =  Math.ceil(totalRec /  pageSize);
                                                if (typeof req.query.page !== 'undefined') {
                                                                currentPage = req.query.page;
                                                }
                                                var start = 0;
                                                if(currentPage >1){
                                                                start = (currentPage - 1) * pageSize;
                                                }
                                                var data = [];
                                                for(var i=start;i<start+pageSize;i++) {
                                                                if(i < totalRec){
                                                                                data.push(Register[i]);
                                                                } else {
                                                                                break;
                                                                }
                                                }
                            res.render('userdetails.ejs', { "Register": Register, data:data, pageSize: pageSize, pageCount: pageCount,currentPage: currentPage});
                                   });            
                    });
      

Share this

Related Posts

Previous
Next Post »

10 comments

comments
23 April 2018 at 04:18 delete

Thanks for sharing the information for more details Mean stack training in hyderabad

Reply
avatar
19 February 2019 at 03:45 delete

Hi I am so glad to be part of this great blog. Keep sharing such quality content. Thank you.
MEAN Stack Training
MEAN Stack Online Training

Reply
avatar
20 July 2019 at 00:44 delete This comment has been removed by the author.
avatar
26 July 2020 at 08:20 delete

Good post and informative. Thank you very much for sharing this good article, it was so good to read and useful to improve my knowledge as updated, keep blogging. Really the post is very unique.every concepts are captured nice.

oracle training in chennai

oracle training institute in chennai

oracle training in bangalore

oracle training in hyderabad

oracle training

oracle online training

hadoop training in chennai

hadoop training in bangalore


Reply
avatar

Pageviews from the past week