This article example shares the specific code of vue3 encapsulating its own paging component for your reference. The specific content is as follows backgroundWhen 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
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:
|
<<: Detailed explanation of the implementation process of ServerSocket default IP binding
>>: How to operate json fields in MySQL
Table of contents Avoid repetitive rendering loop...
Setting min-width and max-width properties in tab...
When using MySQL database, you often encounter su...
Specify in CSS style file #class td /*Set the tab...
In daily development, database addition, deletion...
Table of contents Preface 1. Prepare new partitio...
This article shares the specific code of jQuery...
Achieve results Implementation Code html <div ...
Preface When docker run creates and runs a contai...
routing vue-router4 keeps most of the API unchang...
1.docker search mysql查看mysql版本 2. docker pull mys...
What is an HTML file? HTML stands for Hyper Text M...
<br />Original text: http://jorux.com/archiv...
This article shares with you how to install the M...
Table of contents What is JSONP JSONP Principle J...