node. JS Blog Instance (vii) Paging feature

Source: Internet
Author: User

The original tutorial Https://github.com/nswbmw/N-blog/wiki/_pages the seventh chapter, due to the version and other reasons, in the original tutorial based on a slight change can be achieved.

Add paging functionality to your blog's homepage and user pages. Settings: page and user pages display up to 10 articles per page.
MongoDB's skip and limit operations are used here.
Open the Post.js and modify the Post.getall function as follows:

Read the article and its related information Post.getten = function (name,page,callback) {  //Open database  Mongodb.open (err, db) {    if ( ERR) {      return callback (ERR);    }    Read Posts Collection    db.collection (' posts ', function (err, collection) {      if (err) {        mongodb.close ();        Return callback (ERR);      }      var query = {};      if (name) {        query.name = name;      }  Use count to return the number of documents for a particular query      Collection.count (query,function (err,total) {///query based on queries and skip previous (page-1) *10 results, returned after 10 Results Collection.find (query,{skip: (page-1) *10,limit:10}). Sort ({time:-1}). ToArray (function (err, docs) {Mongodb.close (); if (err) {return callback (ERR);//Failed! Returns err}//parsing markdown to Htmldocs.foreach (function (doc) {doc.post = markdown.tohtml (doc.post);}); Callback (null, docs,total);//success! Returns the result of the query as an array});});};
Open Index.js, modify App.get ('/', function (Req,res) {as follows:
<span style= "White-space:pre" ></span>app.get ('/', function (req, res) {//Determines whether it is the first page and converts the number of pages requested into number type Var Page=req.query.p?parseint (REQ.QUERY.P): 1;//Query and return to page 10 of article Post.getten (null,page, function (err, posts,total) {if (ERR) {posts = [];} Res.render (' index ', {title: ' Home ', Page:page,isfirstpage: (page-1) = = 0,islastpage: ((page-1) * + posts.length) = = To Tal,user:req.session.user,posts:posts,success:req.flash (' success '). ToString (), Error:req.flash (' Error '). ToString ()});})    ;
Note: The number of pages obtained by REQ.QUERY.P here is in string form, and we need to convert it to numbers by parseint () for later use. At the same time the post.getall changed to Post.getten.
Modify the App.get ('/u/:name ') in Index.js as follows:

<span style= "White-space:pre" ></span>app.get ('/u/:name ', function (req, res) {var page=req.query.p? parseint (REQ.QUERY.P): 1;//checks if the user exists User.get (req.params.name, function (err, user) {if (!user) {req.flash (' Error ', ' User does not exist! '); return Res.redirect ('/');//The user does not exist, then jumps to the home page}//query and returns the user page 10 articles post.getten (user.name,page, function (err, posts, Total) {if (err) {Req.flash (' error ', err); return res.redirect ('/');} res.render (' user ', {title:user.name,posts:posts, Page:page,isfirstpage: (page-1) = = 0,islastpage: ((page-1) * + posts.length) = = Total,user:req.session.user,succe Ss:req.flash (' success '). ToString (), Error:req.flash (' Error '). ToString ()});}); });
Under the blog/views/folder, create a new Paging.ejs and add the following code:
<BR/><div>  <% if (!isfirstpage) {%>    <span class= "prepage" ><a title= "Previous page" href= "? P =<%= page-1%> "> Previous </a></span>  <%}%>  <% if (!islastpage) {%>    <span class= "NextPage" ><a title= "Next" href= "? p=<%= page+1%>" > Next </a></span>  <%}%> </div>
This is done by if (!isfirstpage) to determine whether the first page, not the first page displays "previous page", and if (!islastpage) to determine whether it is the last page, not the last page will display "next page."
Next, introduce pagination on the home page and user pages. Modify Index.ejs and User.ejs to add a line of code before <%-include footer%>:
<%-include paging%>
In Style.css, add the following styles:
. prepage a{float:left;text-decoration:none;padding:.5em 1em;color: #ff0000; font-weight:bold;}. NextPage a{float:right;text-decoration:none;padding:.5em 1em;color: #ff0000; font-weight:bold;}. Prepage a:hover,.nextpage a:hover{text-decoration:none;background-color: #ff0000; color: #f9f9f9;-webkit-transition : color. 2s linear;}
Effect:

11 articles Posted above = =

Click the next page


The code should be more active and drink plenty of water.


node. JS Blog Instance (vii) Paging feature

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.