Coder Social home page Coder Social logo

mongoose-paginater's Introduction

Mongoose Paginater

A Mongoose paginatation plugin, forked from Paginate for mongoose by song940.

paginater means paginator.

Installation

$ npm install mongoose-paginater

Usage

var mongoose = require('mongoose');
require('mongoose-paginater');

var options = {
  perPage: 10,
  delta  : 3,
  page   : req.query.page
};

var query = MyModel.find({deleted: false}).sort('name', 1);

query.paginater(options, function(err, res) {
  console.log(res); // => res = {
    //  results: [Document, ...],       // mongoose results
    //  current: 5,                     // current page number
    //  last: 12,                       // last page number
    //  prev: 4,                        // prev number or null
    //  next: 6,                        // next number or null
    //  pages: [ 2, 3, 4, 5, 6, 7, 8 ], // page numbers
    //  count: 125                      // document count
    //  render: '<ul>...</ul>'          // rendered pagination
  //};
});

Pagination Render

There is a pagination template in the render, and you can set options via paginater's settings.

The defaults:

{
    firstText: 'First',
    prevText: '&laquo;',
    nextText: '&raquo;',
    lastText: 'Last',
    totalText: 'Page %d of %d',
    path: '',  // path/to
    query: {}, // the page query is `page=n`, you can add other queries `{query1: 'val', q2: 'v2' }`
               // the pagination href will be like: `path/to?query1=val&q2=v2&page=n`
    classNameSpace: '',
    className: {
        wrap: 'pagination',
        active: 'active',
        disable: 'disabled'
    }
}

In the example folder, the render is used like this:

var options = {
    perPage: 10,
    delta  : 3,
    page   : req.query.page,
    classNameSpace: 'am',
    query: {q1: 'v1', q2: 'v2'}
};

var query = User.find().sort({'id': 1});

query.paginater(options, function(err, result) {
    res.render('index', {
        title: 'User List',
        pager: result
    });
});

The HTML is for Amaze UI Pagination:

<ul class="am-pagination">
  <li><span>Page 1 of 11</span></li>
  <li class="am-disabled"><a href="?q1=v1&q2=v2&page=1">« Prev</a></li>
  <li class="am-active"><a href="?q1=v1&q2=v2&page=1">1</a></li>
  <li><a href="?q1=v1&q2=v2&page=2">2</a></li>
  <li><a href="?q1=v1&q2=v2&page=3">3</a></li>
  <li><a href="?q1=v1&q2=v2&page=4">4</a></li>
  <li><a href="?q1=v1&q2=v2&page=5">5</a></li>
  <li><a href="?q1=v1&q2=v2&page=6">6</a></li>
  <li><a href="?q1=v1&q2=v2&page=7">7</a></li>
  <li><a href="?q1=v1&q2=v2&page=2">Next »</a></li>
  <li><a href="?q1=v1&q2=v2&page=11">Last</a></li>
</ul>
  • Bootstrap: just use it with default options.
  • UIKit: set classNameSpace: 'uk'

You can also use rendered HTML to other frameworks, just set classes you want.

Run Example

  1. Clone this project;
  2. Install packages and run it;
  3. Visit localhost:3007.
git clone https://github.com/Minwe/mongoose-paginater.git
cd mongoose-paginater/example
npm install
npm start

NPM

License

The MIT License

mongoose-paginater's People

Contributors

angelxmoreno avatar cobola avatar minwe avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.