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

Blog    

Recommend

JavaScript commonly used array deduplication actual combat source code

Array deduplication is usually encountered during...

JavaScript example code to determine whether a file exists

1. Business Scenario I have been doing developmen...

HTML table tag tutorial (25): vertical alignment attribute VALIGN

In the vertical direction, you can set the row al...

Reasons and solutions for MySQL selecting the wrong index

In MySQL, you can specify multiple indexes for a ...

Will this SQL writing method really cause the index to fail?

Preface There are often some articles on the Inte...

Summary of Vue watch monitoring methods

Table of contents 1. The role of watch in vue is ...

Solve the problem of PhPStudy MySQL startup failure under Windows system

Report an error The Apache\Nginx service started ...

The best 9 foreign free picture material websites

It is difficult to find good image material websi...

Determine whether MySQL update will lock the table through examples

Two cases: 1. With index 2. Without index Prerequ...

Forty-nine JavaScript tips and tricks

Table of contents 1. Operation of js integer 2. R...

MySQL configuration SSL master-slave replication

MySQL5.6 How to create SSL files Official documen...