An article tells you how to implement Vue front-end paging and back-end paging

An article tells you how to implement Vue front-end paging and back-end paging

1: Front-end handwritten paging (when the amount of data is small)

The front end needs to use slice interception: tableData((page-1)pageSize,pagepageSize)

clipboard.png

clipboard1.png

2: Backend paging, the frontend only needs to pay attention to the passed page and pageSize

3: Front-end handwritten paging buttons

 <body>
  <div id="app">
    <table class="table table-bordered table-condensed">
      <tr class="bg-primary">
        <th class="text-center">Sort</th>
        <th class="text-center">User Name</th>
        <th class="text-center">User's gender</th>
        <th class="text-center">City</th>
      </tr>
      <tr class="text-center active" v-for="(v,i) in list" :key="i">
        <td>{{num(i)}}</td>
        <!-- <td>{{params.pagesize}}</td> -->
        <td>{{v.name}}</td>
        <td>{{v.sex}}</td>
        <td>{{v.addr}}</td>
      </tr>       
    </table>
    <nav aria-label="Page navigation" style="text-align: center;">
      <ul class="pagination">
        <!-- Previous Page -->
        <li @click="prePage()" :class="{'disabled':params.page == 1}">
          <a aria-label="Previous">
            <span aria-hidden="true">&laquo;</span>
          </a>
        </li>
        <li :class="{'active': params.page == page}" v-for="(page,index) in pages" :key="index" @click="curPage(page)">
          <a style="cursor: pointer;">
            {{page}}
          </a>
        </li>
        <!-- Next Page -->
        <li :class="{'disabled':params.page == totalPage}" @click="next()">
          <a aria-label="Next">
            <span aria-hidden="true">&raquo;</span>
          </a>
        </li>
      </ul>
    </nav>
  </div>
</body>
window.onload = function () {
  // Only one request (trigger event) is allowed within 1 second (multiple clicks are allowed) throttle
  new Vue({
    el: '#app',
    data: {
      params:{
        page:1,
        pagesize:20,
        name:''
      },
      list: [],
      total:0, //Total number of entries totalPage:0, //Total number of pages flag: false,
    },
    created() {
      this.getData()
    },
    computed: {
      pages() {
        let totalPage = this.totalPage;
        let page = this.params.page;
        // The total number of pages is less than 10 if(totalPage < 10) return totalPage;
        // If the total number of pages is greater than 10, add ellipsis if (page <= 5) { // The first five pages // (1) If the page number is less than 5, six pages will be displayed on the left return [1,2,3,4,5,6,'...',totalPage]
        } else if (page >= totalPage - 5) { // Next five pages console.log("trigger")
          // (2) If the page number is greater than the total number of pages - 5, six returns are displayed on the right: [1,'...',totalPage-5,totalPage-4,totalPage-3,totalPage-2,totalPage-1,totalPage]
        } else { // The middle five pages // (3) The page number is between 5-(totalPage-5). The left interval cannot be less than 5 and the right interval cannot be greater than the total number of pages totalPage. Note that the current page-num on the left cannot be less than 1, and the current page+num on the right cannot be greater than the total number of pages return [1,'...',page-1,page,page+1,page+2,page+3,'...',totalPage]
        }
      },
      num() {
        let { pagesize, page} = this.params
        // (1-1) * 10 + 10 + 0 + 1 = 1;
        // (2-1) * 10 + 10 + 0 + 1 = 11 
        // First page = (current page - 1) * number of entries per page + index value + 1. Ensure that it starts from 1. return i => (page - 1) * pagesize + i + 1 // (current page - 1 * number of entries per page) + index value + 1
      }
    },
    methods: {
      getData() {
        if(this.flag) return;
        this.flag = true;
        // The following is equivalent to a timer axios.get('http://localhost:3000/user/listpage',{params:this.params}).then(res => {
          console.log('res',res.data.users)
          let { total,users } = res.data.users;
          this.total = total;
          this.totalPage = Math.ceil( this.total / this.params.pagesize);
          this.list = users
          this.flag = false;
        })
      },
      curPage(page) {
        if(page == '...') return 
        if(this.flag) return;
        this.params.page = page;
        this.getData()
      },
      prePage() {
        // if (this.params.page == '...') return 
        if (this.params.page > 1) {
          if(this.flag) return;
          --this.params.page;
          console.log('page',this.params.page)
          this.getData()
        }
      },
      next() {
        // if (this.params.page == '...') return 
        if(this.flag) return;
        console.log("execution", this.totalPage)
        if(this.params.page < this.totalPage) {
          ++this.params.page;
          console.log('page',this.params.page)
          this.getData()
        }
      },
     } 
  })
}

image.png

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • vue+Element-ui front-end realizes paging effect
  • Vue implements the complete code of front-end paging
  • Vue.js combines bootstrap front-end to achieve paging and sorting effects
  • Front-end and back-end configuration and use of Vue paging plug-in

<<:  Detailed explanation of the 14 common HTTP status codes returned by the server

>>:  Implementation of MySQL Multi-version Concurrency Control MVCC

Recommend

Enterprise-level installation tutorial using LAMP source code

Table of contents LAMP architecture 1.Lamp Introd...

Java example code to generate random characters

Sample code: import java.util.Random; import java...

Detailed explanation of the transition attribute of simple CSS animation

1. Understanding of transition attributes 1. The ...

Implementation of installing and uninstalling CUDA and CUDNN in Ubuntu

Table of contents Preface Install the graphics dr...

MySQL paging performance exploration

Several common paging methods: 1. Escalator metho...

Vue implements simple slider verification

This article example shares the implementation of...

A brief talk about JavaScript parasitic composition inheritance

Composition inheritance Combination inheritance i...

Postman automated interface testing practice

Table of contents Background Description Creating...

CSS3 countdown effect

Achieve results Implementation Code html <div ...

Commonplace talk about MySQL event scheduler (must read)

Overview MySQL also has its own event scheduler, ...

How to run Spring Boot application in Docker

In the past few days, I have studied how to run s...

Let's learn about the MySQL storage engine

Table of contents Preface 1. MySQL main storage e...

WeChat Mini Program implements the likes service

This article shares the specific code for the WeC...

Analysis of Difficulties in Hot Standby of MySQL Database

I have previously introduced to you the configura...