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

Implement a simple search engine based on MySQL

Table of contents Implementing a search engine ba...

JS deep and shallow copy details

Table of contents 1. What does shallow copy mean?...

How to make a List in CocosCreator

CocosCreator version: 2.3.4 Cocos does not have a...

Common failures and reasons for mysql connection failure

=================================================...

How to run Spring Boot application in Docker

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

How to reset the root password of Mysql in Windows if you forget it

My machine environment: Windows 2008 R2 MySQL 5.6...

Pure CSS to achieve cool neon light effect (with demo)

I have recently been following the CSS Animation ...

Summary of event handling in Vue.js front-end framework

1. v-on event monitoring To listen to DOM events,...

JavaScript counts the number of times a character appears

This article example shares the specific code of ...

Mobile Internet Era: Responsive Web Design Has Become a General Trend

We are in an era of rapid development of mobile In...

How to implement Svelte's Defer Transition in Vue

I recently watched Rich Harris's <Rethinki...

Sample code for generating QR code using js

Some time ago, the project needed to develop the ...

Tutorial on installing jdk1.8 on ubuntu14.04

1. Download jdk download address我下載的是jdk-8u221-li...

vitrualBox+ubuntu16.04 install python3.6 latest tutorial and detailed steps

Because I need to use Ubuntu+Python 3.6 version t...