nodejs basic tutorial - simple blog (9) - paging

design sketch:

Analysis of this renderings, combined with previous tutorials we know. The navigation bar inherits "layout.html" and the table is "user [index. HTML". The paging button is the content of this lesson. Encapsulate it in 'page.htnl';

First, create a new file views/admin/page.html

<nav aria-label="...">
    <ul class="pager">
        <li class="previous">
            <a href="/admin/user?page={{page-1}}"><span aria-hidden="true">&larr;</span> previous page</a>
        <li>Altogether{{count}}Data, per page{{limit}}Data, total{{pages}}Page, current page{{page}}Page.</li>
        <li class="next">
            <a href="/admin/user?page={{page+1}}"> next page <span aria-hidden="true">&rarr;</span></a>

In router / admin.js, change the code to;

var express=require('express')
var router=express.Router();
var User=require('../models/User')
router.use(function (req,res,next) {
    // console.log("admin",req.userInfo.isAdmin,req.userInfo)
        res.send("Sorry, only administrators can enter the background management")

 * home page
router.get('/',function (req,res,next) {
 * user management 
router.get('/user',function (req,res,next) {
     *     paging
     *     limit(Number):Limit the number of data acquired
     *     skip(Number):Ignore the number of data
     *     Two pieces of data are displayed on each page, assuming that each page is limited to two pieces of data;
     *     Page 1: 1-2 skip (0) ignores 0 items;
     *     Page 2: 3-4 skip (2) ignore 2 pieces of data on the first page;
     *     Page 3: 3-4 skip (4) ignore the four data in the first two pages;
     *     Ignored number = (current page - 1) * limit to implement paging;
    var page=Number(||1 //Current page: the number of pages passed by the front-end user through get, or the default number of current pages is 1 when there is no delivery;
    var limit=2//Number of items displayed per page;
    //Get the total number;
    User.count().then(function (count) {
        //Calculate the total number of pages;
       var pages=Math.ceil(count/limit)
        //The current page cannot be greater than the total number of pages;
        //Current page cannot be less than 1
        var skip=(page-1)*limit;//Ignored number
 *     Read all user data from the database
 * */
        User.find().limit(limit).skip(skip).then(function (users) {
            // console.log(users)




Knowledge points:
Template engine swig: {% include 'page.html'%} means that page.html is introduced into user index.html as a template. And the parameters in page.html can use the context of user_index.html (page, count,pages,limit,). In the future, all the parameters that need to refer to the page.html file must have (page, count,pages,limit,);

The difference between include and extends: the current user_index.html inherits layout.html. In addition, page.html is introduced;
In swig, we can define many block s in the layout.html template page, and the sub page user_index.html can be selectively implemented.
include contains a template to the current location, which will use the current context

Operate mongoose
1, User.find(): read all user data from the database
2. limit(Number): the total number of results and only partial results are extracted
3. skip(Number): ignore the number of data
4. User.count(): read all user data;

Tags: Database less Mongoose

Posted on Mon, 04 May 2020 23:47:06 -0400 by RickChase