Nodejs paging code sharing

Source: Internet
Author: User

Paging class, which I put in plugin/Paginate. js

Copy codeThe Code is as follows:
/**
* Paging plug-in class (the number of displays per page is missing, and listrows will be written tomorrow)
* @ Param page {Number} current page
* @ Param pagesize {Number} Number of records per page
* @ Param total {Number} total Number of 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;
If (this. 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;
}
}

/*
* Number of current start entries
*/
Paginate. prototype. first = function (){
Var first = (this. page-1) * this. pagesize;
If (first> this. total ){
Return (this. maxpage-1) * this. pagesize;
}
Return first;
}
/*
* Maximum number of entries on the current page
*/
Paginate. prototype. last = function (){
Var last = this. first () + this. pagesize;
If (last> this. total ){
Return this. 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 ){
Return false;
}
Return (parseInt (this. page) + 1 );
}
Module. exports = Paginate;

Example

Copy codeThe Code is as follows:
Var Paginate = require ("../plugin/Paginate ");
Var q = req. query. q;
Var paginate = new Paginate (q, 10,185 );
Var page = paginate. page; // current page number
Var first = paginate. first (); // The current first
Var last = paginate. last (); // current maximum number of entries
Var maxpage = paginate. maxpage; // the total number of pages.
Var pagesize = paginate. pagesize; // number of entries displayed 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 })

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.