edwardhotchkiss / mongoose-paginate

Mongoose.js (Node.js & MongoDB) Document Query Pagination
MIT License
985 stars 220 forks source link

Visual implementation with Mustache #133

Open benbagley opened 6 years ago

benbagley commented 6 years ago

Hi,

I'm currently working on getting this package working and I believe I have it implemented correctly.

I have the following:

route

router.get('/dashboard', ensureAuthenticated, (req, res) => {
  User.paginate({}, { page: 1, limit: 6 }, function(err, result) {
    result.limit - 6
    result.page - 1
  });

  User.find({}, function(err, users) {
    res.render('dashboard/index.hbs', {
      pageTitle: 'Dashboard',
      users: users
    });
  });
});

and I have implemented the package in the user.js and called UserSchema.plugin(mongoosePaginate);.

However I am unsure on how to get a visible implementation of this working so it is clickable through the pages.

I am using Bootstrap 3 and the mustache templating engine.

shr33narayan commented 6 years ago

I can give you a short idea of what I am using:

In My Route:

router.get('/users', (req, res)=>{
  User.paginate({}, {
    sort: {'_createdAt': -1},
    limit: req.query.l ? parseInt(req.query.l) : 12,
    page: req.query.page ? req.query.page : 1
    })
    .then((result)=>{
      //result is a returned array from the database pagination
      return res.render('admin/dashboard', {
        title: "Dashboard | Admin Panel",
        users: result.docs,
        total: result.total,
        currentPage: result.page,
        limit: result.limit
      });
    })
    .catch((err)=>{
      console.log(err);
      return res.send('Error Contacting the database or Some Trouble happened while exec Pagination Script');
    })
})

In My .pug file (using Bootstrap 4 theme):

         - var PossiblePages = Math.trunc(total/limit)+1;
        - var nextPage = currentPage
        nav(aria-label="...")
          ul.pagination.pagination-md.justify-content-center
            li.page-item(class= {disabled: currentPage==1})
              a.page-link(href="/admin/dashboard?page="+(currentPage-1) tabindex="-1") Previous
            - var p = 1;
            while p <= PossiblePages
              li.page-item(class= {active: currentPage == p} )
                a.page-link(href="/admin/dashboard?page="+p)=p
              - p++;
            li.page-item(class={disabled: currentPage == PossiblePages})
              a.page-link(href="/admin/dashboard?page="+ ++nextPage ) Next

As you can see, I am making use of req.query or ?p=2 (for ex) for getting mine work.