Nodejs Pagination Class

Source: Internet
Author: User

Adapted from http://my.oschina.net/wanglihui/blog/269907, thanks to the original author, I carried out a two-time package based on

Pagination class, I put in plugin/paginate.js

/** * Page plug-in class (missing the number of displays per page, ListRows write tomorrow) *@paramPage {Number} current pages *@parampagesize {Number} records per page *@paramTotal {Number} records *@constructor*/function paginate (page, pagesize, total) {if (!page | | Page <1) {page =1; }if (!pagesize | | pagesize<1) {pagesize =20; }if (!total | | | Total <0) {total =0; }This.pagesize = pagesize;This.total = total;Ifthis.total%This.pagesize = = =0) {This.maxpage = parseint (this.total/This.pagesize); }else{This.maxpage = parseint (This.total/This.pagesize) +1; }if (page>This.maxpage) {This.page =This.maxpage; }else{this.page = page; }}/** the number of bars currently starting */paginate.prototype.first = function () {var first = (This.page-1) *This.pagesize;if (first>This.total) {Return (This.maxpage-1) *This.pagesize; }return first; }/** the maximum number of bars on the current page */paginate.prototype.last = function () {var last =This.first () +This.pagesize;if (last>This.total) {ReturnThis.total; }return last;}/** * Previous Page *@returns{Number} */paginate.prototype.prev = function () {if (this.page <= 1) {return FALSE;} return this.page-1;} /** * Next page *   @returns  {*} */paginate.prototype.next = function () {if (this.page >= this.maxpage) { Span class= "Hljs-keyword" >return FALSE;} return (parseint (this.page) +1);} Module.exports = paginate;              

Examples of Use

var paginate =Require".. /plugin/paginate ");var q = req.query.q;var paginate =New Paginate (q,10,185); var page = paginate.page; //Current page var first = Paginate.first ();  The current first var last = Paginate.last ();  Current maximum number of bars var maxpage = paginate.maxpage;  Total Pages var pagesize = paginate.pagesize;  Display per page var total = paginate.total;  Total number of records var prev = Paginate.prev ();  Previous var next = Paginate.next ();  Next Res.json ({page:page, first:first,last:last,maxpage:maxpage,pagesize:pagesize, Total:total,prev:prev,next: Next})                

Nodejs Pagination Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.