How to manually encapsulate paging components in Vue3.0

How to manually encapsulate paging components in Vue3.0

This article shares the specific code of the vue3.0 manual encapsulation paging component for your reference. The specific content is as follows

1. Parent component introduction

src/views/goods/components/goods-comment.vue

<!-- page indicates the default page to be displayed when initializing paging -->
    <XtxPagination @change-page='changePage' :pagesize='reqParams.pageSize' :total='total' :page='1' />
    //Call interface import {findCommentListByGoods } from '@/api/product.js'
 export default{
  setup(){
  // Preparation of screening conditions const reqParams = reactive({
      // Current page number page: 1,
      //Number of entries per pagepageSize: 10,
      // Is there a picture hasPicture: null,
      // Filter condition tag: null,
      // Sorting field sortField: null
    })
    // Listen for parameter changes watch(reqParams, () => {
   findCommentListByGoods(goods.value.id, reqParams).then(ret => {
        total.value = ret.result.counts
        list.value = ret.result.items
      })
    }, {
      immediate: true
    })
    // Control the change of page number const changePage = (page) => {
      // Modify the paging parameters and call the interface again reqParams.page = page
    }
    
  }
 }

2. Subcomponents

src/components/library/xtx-pagination.vue

<template>
  <div class="xtx-pagination">
    <a @click='changePage(false)' href="javascript:;" :class="{disabled: currentPage===1}">Previous page</a>
    <span v-if='currentPage > 3'>...</span>
    <a @click='changePage(item)' href="javascript:;" :class='{active: currentPage===item}' v-for='item in list' :key='item'>{{item}}</a>
    <span v-if='currentPage < pages - 2'>...</span>
    <a @click='changePage(true)' href="javascript:;" :class='{disabled: currentPage===pages}'>Next page</a>
  </div>
</template>
<script>
import { computed, ref } from 'vue'

export default {
  name: 'XtxPagination',
  props: {
    total:
      type: Number,
      default: 80
    },
    pagesize: {
      type: Number,
      default: 10
    }
    //Default initial page number// page: {
    // type: Number,
    // default: 1
    // }
  },
  setup (props, { emit, attrs }) {
    // attrs represents the attributes passed by the parent component, but props does not receive the attributes, which is not responsive // ​​Dynamically calculate the mid-term page number information // Number of items per page // const pagesize = 8
    // Total number of pages let pages = Math.ceil(props.total / props.pagesize)
    //Current page number// console.log(attrs.page)
    const currentPage = ref(attrs.page || 1)
    // Dynamically calculate the page number list const list = computed(() => {
      // When the value of total passed by the parent component changes, the calculated property will recalculate pages = Math.ceil(props.total / props.pagesize)
      const result = []
      // The total page number is less than or equal to 5; greater than 5
      if (pages <= 5) {
        // Total page number is less than or equal to 5 for (let i = 1; i <= pages; i++) {
          result.push(i)
        }
      } else {
        // The total page number is greater than 5
        if (currentPage.value <= 2) {
          // Left critical value for (let i = 1; i <= 5; i++) {
            result.push(i)
          }
        } else if (currentPage.value >= pages - 1) {
          // Right critical value for (let i = pages - 4; i <= pages; i++) {
            result.push(i)
          }
        } else {
          // Intermediate state for (let i = currentPage.value - 2; i <= currentPage.value + 2; i++) {
            result.push(i)
          }
        }
      }
      return result
    })
    // Control the changes of the previous and next pages const changePage = (type) => {
      if (type === false) {
        // Previous page // When the page is the first page, click operation is prohibited if (currentPage.value === 1) return
        if (currentPage.value > 1) {
          currentPage.value -= 1
        }
      } else if (type === true) {
        // Next page // When the page is the last page, click operation is prohibited if (currentPage.value === pages) return
        if (currentPage.value < pages) {
          currentPage.value += 1
        }
      } else {
        // Click on the page currentPage.value = type
      }
      emit('change-page', currentPage.value)
    }
    return { list, currentPage, pages, changePage }
  }
}
</script>
<style scoped lang="less">
.xtx-pagination {
  display: flex;
  justify-content: center;
  padding: 30px;
  > a {
    display: inline-block;
    padding: 5px 10px;
    border: 1px solid #e4e4e4;
    border-radius: 4px;
    margin-right: 10px;
    &:hover {
      color: @xtxColor;
    }
    &.active {
      background: @xtxColor;
      color: #fff;
      border-color: @xtxColor;
    }
    &.disabled {
      cursor: not-allowed;
      opacity: 0.4;
      &:hover {
        color: #333;
      }
    }
  }
  > span {
    margin-right: 10px;
  }
}
</style>

Knowledge point: attrs represents the attributes passed by the parent component, but props does not receive the attributes, which is not responsive (new in vue3)

3. Achieve results

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Table paging component based on Vue.js
  • How to encapsulate paging components based on Vue
  • Vue paging component table-pagebar usage example analysis
  • Vue implements paging component
  • Vue.js implements a custom paging component vue-paginaiton
  • Using vue.js to create paging components
  • Vue example code based on element-ui paging component encapsulation
  • Implementation code of Vue global paging component
  • Implementation method of table paging component based on vue2
  • Implementing swipe paging component example based on Vue

<<:  Solution to many line breaks and carriage returns in MySQL data

>>:  Detailed steps to install 64-bit Ubuntu system and Docker tool on Raspberry Pi 3B+

Recommend

Solution to MySQL being unable to start due to excessive memory configuration

Problem Description MySQL reports an error when s...

Summary of the differences between global objects in nodejs and browsers

In Node.js, a .js file is a complete scope (modul...

Vue directives v-html and v-text

Table of contents 1. v-text text rendering instru...

MyBatis dynamic SQL comprehensive explanation

Table of contents Preface Dynamic SQL 1. Take a l...

Nodejs implements intranet penetration service

Table of contents 1. Proxy in LAN 2. Intranet pen...

Solution to prevent caching in pages

Solution: Add the following code in <head>: ...

Vue Element front-end application development table list display

1. List query interface effect Before introducing...

Typical cases of MySQL index failure

Table of contents Typical Cases Appendix: Common ...

MySQL transaction autocommit automatic commit operation

The default operating mode of MySQL is autocommit...

Linux five-step build kernel tree

Table of contents 0. The kernel tree that comes w...

Element Timeline implementation

Table of contents Components - Timeline Custom no...

Docker container monitoring and log management implementation process analysis

When the scale of Docker deployment becomes large...

Detailed tutorial on integrating Apache Tomcat with IDEA editor

1. Download the tomcat compressed package from th...