Vue3 encapsulates its own paging component

Vue3 encapsulates its own paging component

This article example shares the specific code of vue3 encapsulating its own paging component for your reference. The specific content is as follows

background

When browsing list-type data, if there is a lot of data and all of it is requested at once, performance loss and loading delays may occur. In this case, the paging component plays a key role. It can request only part of the data without taking up too much page space. If you want to view other data, you can initiate a request by changing the page number and refresh the page data.

Now we encapsulate the paging component ourselves

Component required parameters

  • total attribute: used to pass the total number of data
  • Pagesize attribute: how many data items are displayed per page
  • currentPage property: current default page number
  • Change -page event: an event triggered when the page number changes , the parameter is the current page number

Component landing code my-pagination.vue

<template>
  <div class="my-pagination">
    <a href="javascript:;" :class="{ disabled: currentPage === 1 }" @click="changePage(false)">Previous page</a>
    <span v-if="currentPage > 3">...</span>
    <a
      href="javascript:;" 
      v-for="item in list"
      :key="item"
      :class="{ active: currentPage === item }"
      @click="changePage(item)"
      >{{ item }}</a
    >
    <span v-if="currentPage < pages - 2">...</span>
    <a href="javascript:;" :class="{ disabled: currentPage === pages }" @click="changePage(true)">Next page</a>
  </div>
</template>
<script>
import { computed, ref } from 'vue-demi'
export default {
  name: 'MyPagination',
  props: {
    total:
      type: Number,
      default: 80
    },
    pagesize: {
      type: Number,
      default: 10
    }
  },
  setup(props, { emit, attrs }) {
    // Current page const currentPage = ref(attrs.currentPage)
    // Calculate the total number of pages const pages = computed(() => Math.ceil(props.total / props.pagesize))
    // Page number display combination const list = computed(() => {
      const result = []
      // When the total number of pages is less than or equal to 5 pages if (pages <= 5) {
        for (let i = 1; i <= pages; i++) {
          result.push(i)
        }
      } else {
        // When the total number of pages is greater than 5 pages // Control the presence or absence of ellipsis at the two extremes, and the number of page numbers displayed is centered with the selected page number if (currentPage.value <= 2) {
          for (let i = 1; i <= 5; i++) {
            result.push(i)
          }
        } else if (currentPage.value >= 3 && currentPage.value <= pages.value - 2) {
          for (let i = currentPage.value - 2; i <= currentPage.value + 2; i++) {
            result.push(i)
          }
        } else if (currentPage.value > pages.value - 2) {
          for (let i = pages.value - 4; i <= pages.value; i++) {
            result.push(i)
          }
        }
      }
      return result
    })
    // Click on the previous page to change the page number const changePage = type => {
      // Click the previous page button if (type === false) {
        if (currentPage.value <= 1) return
        currentPage.value -= 1
      } else if (type === true) {
        // Click the next page button if (currentPage.value >= pages.value) return
        currentPage.value += 1
      } else {
        // Click on the page currentPage.value = type
      }
      // Pass the current page number to the parent component, and perform related operations in this event emit('change-page', currentPage.value)
    }
    return { currentPage, pages, list, changePage }
  }
}
</script>
<style scoped lang="less">
.my-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>

Using Components

<XtxPagination 
:total="total" 
:pagesize="reqParams.pagesize" 
:currentPage="1" 
@change-page="changePage" />

Effect

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

<<:  Detailed explanation of the implementation process of ServerSocket default IP binding

>>:  How to operate json fields in MySQL

Recommend

Understanding and using React useEffect

Table of contents Avoid repetitive rendering loop...

Mysql modify stored procedure related permissions issue

When using MySQL database, you often encounter su...

Differentiate between null value and empty character ('') in MySQL

In daily development, database addition, deletion...

jQuery clicks on the love effect

This article shares the specific code of jQuery&#...

Circular progress bar implemented with CSS

Achieve results Implementation Code html <div ...

How to modify the port mapping of a running Docker container

Preface When docker run creates and runs a contai...

A simple example of using Vue3 routing VueRouter4

routing vue-router4 keeps most of the API unchang...

Docker deploys mysql to achieve remote connection sample code

1.docker search mysql查看mysql版本 2. docker pull mys...

HTML basics HTML structure

What is an HTML file? HTML stands for Hyper Text M...

Semantics: Is Html/Xhtml really standards-compliant?

<br />Original text: http://jorux.com/archiv...

Quickly install MySQL5.7 compressed package on Windows

This article shares with you how to install the M...

Understand the principles and applications of JSONP in one article

Table of contents What is JSONP JSONP Principle J...