Pagination in nodejs with mysql

后端 未结 4 842
鱼传尺愫
鱼传尺愫 2021-02-08 10:48

In my project I need to query the db with pagination and provide user the functionality to query based on current search result. Something like limit, I am not able to find anyt

4条回答
  •  甜味超标
    2021-02-08 11:51

    I wrote a pagination class in order to use it on different pages, I used bootstrap to style the links, you can change it if you're not using bootstrap.

    Items route

    router.get('/items/:page',(req,res) => {
    const db = require('mysql'),
            Pagination = require('./pagination'),
    
            // Get current page from url (request parameter)
            page_id = parseInt(req.params.page),
            currentPage = page_id > 0 ? page_id : currentPage,
    
    //Change pageUri to your page url without the 'page' query string 
            pageUri = '/items/';
    
            /*Get total items*/
            db.query('SELECT COUNT(id) as totalCount FROM items',(err,result)=>{
    
                // Display 10 items per page
                const perPage = 10,
                    totalCount = result[0].totalCount;
    
                // Instantiate Pagination class
                const Paginate = new Pagination(totalCount,currentPage,pageUri,perPage);
    
    
                /*Query items*/
                db.query('SELECT * FROM items LIMIT '+Paginate.perPage+' OFFSET '+Paginate.start,(err,result)=>{
    
                    data = {
                        items : result,
                        pages : Paginate.links()
                    }
    
                    // Send data to view
                    res.render('items',data);
                });
            });
    
    });
    

    On items view, just print "pages" to generate pagination links

    {{ pages }}
    

    pagination.js >> Add this code to pagination.js and import it to any page you want to use pagination

    class Pagination{
    
    constructor(totalCount,currentPage,pageUri,perPage=2){
        this.perPage = perPage;
        this.totalCount =parseInt(totalCount);
        this.currentPage = parseInt(currentPage);
        this.previousPage = this.currentPage - 1;
        this.nextPage = this.currentPage + 1;
        this.pageCount = Math.ceil(this.totalCount / this.perPage);
        this.pageUri = pageUri;
        this.offset  = this.currentPage > 1 ? this.previousPage * this.perPage : 0;
        this.sidePages = 4;
        this.pages = false;
    }
    
    
    
    links(){
        this.pages='
      '; if(this.previousPage > 0) this.pages+='
    • Previous
    • '; /*Add back links*/ if(this.currentPage > 1){ for (var x = this.currentPage - this.sidePages; x < this.currentPage; x++) { if(x > 0) this.pages+='
    • '+x+'
    • '; } } /*Show current page*/ this.pages+='
    • '+this.currentPage+'
    • '; /*Add more links*/ for(x = this.nextPage; x <= this.pageCount; x++){ this.pages+='
    • '+x+'
    • '; if(x >= this.currentPage + this.sidePages) break; } /*Display next buttton navigation*/ if(this.currentPage + 1 <= this.pageCount) this.pages+='
    • Next
    • '; this.pages+='
    '; return this.pages; } } module.exports = Pagination;

提交回复
热议问题