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
Array deduplication is usually encountered during...
To use standard CSS3 to achieve the shadow effect...
1. Business Scenario I have been doing developmen...
In the vertical direction, you can set the row al...
In MySQL, you can specify multiple indexes for a ...
question: The commonly used command "ll"...
Preface There are often some articles on the Inte...
1. Remove MySQL a. sudo apt-get autoremove --purg...
Table of contents 1. The role of watch in vue is ...
There was a shaking barrage on TikTok a while ago...
Report an error The Apache\Nginx service started ...
It is difficult to find good image material websi...
Two cases: 1. With index 2. Without index Prerequ...
Table of contents 1. Operation of js integer 2. R...
MySQL5.6 How to create SSL files Official documen...