elementUI分頁組件封裝


在實際開發需求,產品需要的分頁組件比較簡單,只可以一頁頁地翻,就是為了防止用於直接翻看最后的數據(因為有一些歷史數據數據量比較大,查看的意義不大,檢索效率比較低也比較忙,因為不希望用戶在翻頁的時候可以隨意翻看很久之前的數據)

因此需要根據實際需求進行分頁組件封裝

 

以下封裝的分頁組件,勉強夠用,但是還不夠完善,有需要的用於可以再次基礎上繼續完善

 1 <template>
 2   <el-pagination @size-change="handleSizeChange" background @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="pageSizes" :page-size="pageSize" :total="total" :layout="layout">
 3     <slot>
 4       <ul class="el-pager">
 5         <li class="number active">{{currentPage}}</li>
 6         <li class="number" @click="handlePage(item)" v-for="item in pager">{{item}}</li>
 7       </ul>
 8     </slot>
 9   </el-pagination>
10 </template>
11 <script>
12 export default {
13   props: {
14     currentPage: {
15       type: [String, Number],
16       default: 1
17     },
18     total: {
19       type: [String, Number],
20       default: 0
21     },
22     pageSizes: {
23       type: Array,
24       default:function(){
25         return  [10,20,50,100,200,300,400]
26       }
27     },
28     pageSize: {
29       type: [String, Number],
30       default: 10
31     },
32     layout: {
33       type: String,
34       default: 'total,prev,slot,next,sizes'
35     }
36   },
37   data() {
38     return {
39     }
40   },
41   computed:{
42     pager:function(){
43       let pager=this.total/this.pageSize
44       pager=Math.ceil(pager)//總頁數
45       if(pager<this.currentPage){
46         return []
47       }
48       let flag=this.currentPage+4
49       if(flag>pager){//大於總頁數
50         let arr=[]
51         let total=pager-this.currentPage
52         for(let i=1;i<=total;i++){
53           arr.push(this.currentPage+i)
54         }
55         return arr
56       }else if(flag<=pager){
57         return [this.currentPage+1,this.currentPage+2,this.currentPage+3,this.currentPage+4]
58       }
59     }
60   },
61   methods: {
62     handlePage(page){
63       this.handleCurrentChange(page)
64     },
65     handleSizeChange(val) {
66       this.$emit('size-change', val)
67     },
68     handleCurrentChange(val) {
69       this.$emit('current-change', val)
70     }
71   }
72 }
73 
74 </script>
75 <style lang="scss" scoped>
76 .page {
77   text-align: center;
78   color: #409EFF;
79 }
80 
81 </style>

 

 頁面使用:

  1、在main.js頁面全局注冊分頁組件

// 全局注冊分頁組件
import newPagination from '@/components/newPagination'
Vue.component('newPagination', newPagination)

 

  2、頁面直接調用

 <new-pagination @current-change="handleCurrentChangeExp" :page-size=listQryExp.limit layout="total, prev, pager, next" :total=totalExp></new-pagination>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM