vue封裝自定義分頁器組件與使用方法分享
前言
分頁是開發(fā)各種系統(tǒng)時候最常用的功能,下面為本人封裝的一個分頁組件。
實現(xiàn)分頁器操作需要以下參數(shù)
- 當前頁: pageNo
- 每頁展示條數(shù): pageSize
- 數(shù)據(jù)總條數(shù) : total
- 連續(xù)頁碼數(shù):continues (一般為奇數(shù),頁面對稱更美觀)
分頁器在各大項目中出現(xiàn)的頻率較多,我們可以封裝成靜態(tài)組件,并全局注冊這個組件。
1.全局注冊組件方法:在mian.js文件中操作
import Pagination from '@/components/Pagination' // 組件路徑 Vue.component(Pagination.name,Pagination)
2.其他頁面分頁器的時候可以直接使用。父頁面向子頁面?zhèn)鬟f數(shù)據(jù),使用prop傳遞。
<template> <Pagination :pageNo="this.pageNo" :pageSize="this.pageSize" :total="93" :continues="5" @getPageNo="getPageNo" /> </template> <script> export default { name: "index", data(){ return{ pageNo:1, pageSize:4, } }, methods:{ getPageNo(pageNo){ this.pageNo=pageNo } } } </script>
分頁器 Pagination.vue
<template> <div class="pagination"> <!-- 上 --> <button :disabled="pageNo == 1" @click="getPageNo(pageNo - 1)"> 上一頁 </button> <button v-if="startNumAndEndNum.start > 1" @click="getPageNo(1)" :class="{ active: pageNo == 1 }" >1 </button> <button v-if="startNumAndEndNum.start > 2" @click="getPageNo(pageNo - continues)" >··· </button> <!-- 中間部分 --> <button v-for="(page, index) in generateMiddlePage" :key="index" @click="getPageNo(page)" :class="{ active: pageNo == page }"> {{ page }} </button> <!-- 下 --> <button v-if="startNumAndEndNum.end < totalPage - 1" @click="getPageNo(pageNo +continues)" >··· </button> <button v-if="startNumAndEndNum.end < totalPage" @click="getPageNo(totalPage)" :class="{active:pageNo==totalPage}"> {{ totalPage }} </button> <button :disabled="pageNo == totalPage" @click="getPageNo(pageNo + 1)"> 下一頁 </button> <button style="margin-left: 30px">共 {{ total }} 條</button> </div> </template> <script> export default { name: "Pagination", props: ["pageNo", "pageSize", "total", "continues"], computed: { //計算出總頁數(shù) totalPage() { //向上取整 return Math.ceil(this.total / this.pageSize); }, //計算出頁碼的起始和結(jié)束數(shù)字 startNumAndEndNum() { const {continues, pageNo, totalPage} = this; //先定義兩個變量存儲起始數(shù)字與結(jié)束數(shù)字 let start = 0, end = 0; if (continues > totalPage) { start = 1; end = totalPage; } else { //起始數(shù)字 start = pageNo - parseInt(continues / 2); //結(jié)束數(shù)字 end = pageNo + parseInt(continues / 2); if (start < 1) { start = 1; end = continues; } if (end > totalPage) { end = totalPage; start = totalPage - continues + 1; } } return {start, end}; }, //過濾掉小于起始頁的頁碼 generateMiddlePage() { let arr = []; //避免頁面中同時使用 v-for和v-if for (let i = 0; i <= this.startNumAndEndNum.end; i++) { arr.push(i) } let temp = arr.filter(item => { return item >= this.startNumAndEndNum.start }) return temp } }, methods: { getPageNo(val) { //自定義事件子頁面向父頁面?zhèn)鲄?,計算屬性? this.$emit('getPageNo', val) }, } }; </script> <style lang="less" scoped> .pagination { text-align: center; button { margin: 0 5px; background-color: #f4f4f5; color: #606266; outline: none; border-radius: 2px; padding: 0 4px; vertical-align: top; display: inline-block; font-size: 13px; min-width: 35.5px; height: 28px; line-height: 28px; cursor: pointer; box-sizing: border-box; text-align: center; border: 0; &[disabled] { color: #c0c4cc; cursor: not-allowed; } &.active { cursor: not-allowed; background-color: #409eff; color: #fff; } } } .active { background: skyblue; } </style>
總結(jié)
到此這篇關(guān)于vue封裝自定義分頁器組件與使用方法的文章就介紹到這了,更多相關(guān)vue封裝自定義分頁器組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3項目中配置sass,vite報錯Undefined mixin問題
這篇文章主要介紹了vue3項目中配置sass,vite報錯Undefined mixin問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02稍微學一下Vue的數(shù)據(jù)響應式(Vue2及Vue3區(qū)別)
這篇文章主要介紹了稍微學一下 Vue 的數(shù)據(jù)響應式(Vue2 及 Vue3),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11vue.js+Element實現(xiàn)表格里的增刪改查
本篇文章主要介紹了vue.js+Element實現(xiàn)增刪改查,具有一定的參考價值,有興趣的可以了解一下。2017-01-01