JavaScript-about the way nodejs fuss paging, how to get the total number of pages to the page?

Source: Internet
Author: User
router.get("/:page",function(req,res){    if(req.params.page == 0){        res.send("404");    }    conn.query("select * from news_base",function(err,pdata){        conn.query("select * from news_base limit "+(req.params.page-1)*3+",3",function(err,data){            res.render("admin/list",{datas : pdata,pageDatas : data});        });    });});

SELECT * FROM news_base limit This SQL is based on the number of pages to obtain specific data.
But in the front page I want to make a page page "1.2.3.4.5 next page" effect, it is necessary to know the sum of data to calculate. But select * FROM news_base limit This SQL gets not the sum of the data.

So I had to write another SQL "select * from News_base" which would return an array. Then you can get the total value by length to calculate.

But two SQL feels like it's superfluous. Feel the code is bloated.
There is no better way to achieve the effect of page flipping.
The front desk is using the Ejs template engine.

Reply content:

router.get("/:page",function(req,res){    if(req.params.page == 0){        res.send("404");    }    conn.query("select * from news_base",function(err,pdata){        conn.query("select * from news_base limit "+(req.params.page-1)*3+",3",function(err,data){            res.render("admin/list",{datas : pdata,pageDatas : data});        });    });});

SELECT * FROM news_base limit This SQL is based on the number of pages to obtain specific data.
But in the front page I want to make a page page "1.2.3.4.5 next page" effect, it is necessary to know the sum of data to calculate. But select * FROM news_base limit This SQL gets not the sum of the data.

So I had to write another SQL "select * from News_base" which would return an array. Then you can get the total value by length to calculate.

But two SQL feels like it's superfluous. Feel the code is bloated.
There is no better way to achieve the effect of page flipping.
The front desk is using the Ejs template engine.

In SQL Select, you can use count and check the usage yourself.

The total is occupied by one SQL, and the total is obtained with SELECT COUNT (*) from News_base.

Indicates that the last two times the Select method is actually queried only once

SELECT SQL_CALC_FOUND_ROWS  * FROM apps limit 2,6;SELECT FOUND_ROWS();//在得到数据后,通过FOUND_ROWS()可以得到不带LIMIT的结果数:

But Sql_calc_found_rows is slower than count (*) in Speed ~

If there is no filter, the total number of lists can be saved separately and the memory is cached and then spit out with the list.

You can make two requests when you first enter the page: the total number of requests, one request limit line.
The total quantity is recorded in the page.
After the page, only a second request can be sent.

Only need the total number of cases also exist, should consider developing this interface separately.

If you do not consider the initial load of network overhead problems, you can directly all pull to the local, the client to do paging.

  • 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.