[Nodejs] Nodejs Development Personal Blog (vi) data paging

Source: Internet
Author: User

Controller route Definition

Home route: http://localhost:8888/

Home Page Paging: HTTP://LOCALHOST:8888/INDEX/2

/** * Home Controller*/varRouter=Express. Router ();/*number of bars per page*/varPagesize=4;/*Home Page*/router.Get('/', Function (req,res,next) {varCid=0; F.model ("article"). Assignindexdata (CID,1, pagesize,res);});/*Home page Pagination*/router.Get('/index/:p Age', Function (req,res,next) {varCurrentpage=parseint (req.params. Page); varCid=0; F.model ("article"). Assignindexdata (cid,currentpage,pagesize,res);});

Category List Page routing: http://localhost:8888/category/classification ID/Paging

/* category page */ router.get ('/category/:cid/:p age ',function(req,res,next) {    var cid=  req.params.cid;     var currentpage=parseint (req.params.page);    F.model ("article"). Assignindexdata (cid,currentpage,pagesize,res);});
Model Data Section

Controller calls the Assignindexdata () method of the article model, Parameters: Category ID, current page, number of bars per page, Response object

Call the Getalllist () method of the category model to get the category list, Parameters: callback function

Call the GetCount () method of the article model to get the total number of bars, Parameters: Category ID, callback function

Call the Getarticlepager () method of the article model to get the data list of the article object, Parameters: Category ID, current page, number of bars per page, callback function

On the previous page, the next page to 1 and +1, and to judge, the previous page should be greater than 0, the next page should be less than the total number of pages (total/number of bars per page up rounding)

assigning data to a template

/** * Article model file*/Module.exports={    /*get number of bars*/GetCount:function(categoryid,callback) {varCondition= ""; if(categoryid!=0) {Condition= "Where category_id=" +categoryId; }            varSql= "SELECT COUNT (*) num from article" +condition;    Db.query (Sql,callback); },    /*Get Paging Data*/Getarticlepager:function(categoryid,currentpage,pagesize,callback) {if(currentpage<=0| |! CurrentPage) currentpage=1; varstart= (currentPage-1) *pageSize; varEnd=pageSize; varCondition= ""; if(categoryid!=0) {Condition= "Where category_id=" +categoryId; }        varSql= "SELECT * from article" +condition+ "ORDER by time desc limit" +start+ "," +end;    Db.query (Sql,callback); },    /*Archive*/getarchives:function(callback) {Db.query ("Select time from article ORDER by time Desc", callback); },    /*Assigning home page data*/Assignindexdata:function(cid,currentpage,pagesize,res) {varCategorymodel=f.model ("category"); varArticlemodel= This; //categorical DataCategorymodel.getalllist (function(err,categorylist) {//number of article linesArticlemodel.getcount (CID,function(err,nums) {//article paginationArticlemodel.getarticlepager (Cid,currentpage,pagesize,function(err,articlelist) {varNextpage= (currentpage+1) >=math.ceil (nums[0].num/pagesize)? Math.ceil (nums[0].num/pagesize): currentpage+1; varPrepage= (currentPage-1) <=0? 1:currentpage-1; //ArchiveArticlemodel.getarchives (function(err,allarticletime) {varNewarticletime=[];  for(vari=0;i<allarticletime.length;i++) {Newarticletime.push (F.phpdate ("Y year M Month", Allarticletime[i].time)); }                        /*Assigning Data*/                        varData={categorylist:categorylist, articlelist:articlelist, Cid:cid, Nextpage:nextpage==0? 1: NextPage, Prepage:prepage, Allarticletime:newarticletime,                                                Currentpage:currentpage}; /*Render Template*/Res.render ("Home/index", data);                            });            });        });    }); }};
Template section
          <nav>            <ulclass= "Pager">              <Li><aclass= "Btn <%if (currentpage==prepage) {%>disabled<%}%>"href= "/<%if (cid!=0) {%>category/<%=cid%>/<%}else{%>index/<%}%><%=prepage%>">Previous page</a></Li>              <Li><aclass= "Btn <%if (currentpage==nextpage) {%>disabled<%}%>"href= "/<%if (cid!=0) {%>category/<%=cid%>/<%}else{%>index/<%}%><%=nextpage%>">Next page</a></Li>            </ul>          </nav>

[Nodejs] Nodejs Development Personal Blog (vi) data paging

Related Article

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.