1. 程式人生 > >Vue 分頁元件 v2.0

Vue 分頁元件 v2.0

背景

之前也寫過一個分頁元件,非常簡潔的一個分頁元件。
效果圖: 傳送門

程式碼也很簡單,看看就懂了。當頁數多起來的時候,問題也就來了。

這.......迫不得已,我把頁碼顯示的去掉,就成了

看著是沒啥問題,可是需求方不樂意了。。。

他們希望,頁碼多起來的時候能出現 ... 效果:

Vue 分頁元件 2.0

<!-- 模板 -->
<script type="text/template" id="template_pagination">
    <nav>
        <ul class="pagination"
> <li :class="{'disabled': current == 1}"><a href="javascript:;" @click="setCurrent(1)"> 首頁 </a></li> <li :class="{'disabled': current == 1}"><a href="javascript:;" @click="setCurrent(current - 1)"> 上一頁 </a></li> <li v-for
="p in grouplist" :class="{'active': current == p.val}"><a href="javascript:;" @click="setCurrent(p.val)"> {{ p.text }} </a></li> <li :class="{'disabled': current == page}"><a href="javascript:;" @click="setCurrent(current + 1)"> 下一頁</a></li> <li :class
="{'disabled': current == page}"><a href="javascript:;" @click="setCurrent(page)"> 尾頁 </a></li> </ul> <ul class="pagination pull-right"> <li><span> 共 {{ total }} 條資料 </span></li> <li><span> 每頁顯示 {{ display }} 條資料 </span></li> <li><span> 共 {{ page }} 頁 </span></li> <li><span> 當前第 {{ current }} 頁 </span></li> </ul> </nav> </script>
/**
 * [pagination 分頁元件]
 * @param  {Number} total         [資料總條數]
 * @param  {Number} display     [每頁顯示條數 default:10]
 * @param  {Number} current     [當前頁碼 default:1]
 * @param  {Number} pagegroup     [分頁條數(奇數) default:5]
 * @param  {Event} pagechange     [頁碼改動時 dispatch ]
 * @return {[type]}   [description]
 */
Vue.component('pagination', {
    template: '#template_pagination',
    props: {
        total: {            // 資料總條數
            type: Number,
            default: 0
        },
        display: {            // 每頁顯示條數
            type: Number,
            default: 10
        },
        current: {            // 當前頁碼
            type: Number,
            default: 1
        },
        pagegroup: {        // 分頁條數 -- 奇數
            type: Number,
            default: 5,
            coerce:function(v){
                v = v > 0 ? v : 5;
                return v % 2 === 1 ? v : v + 1;
            }
        }
    },
    computed: {
        page:function() { // 總頁數
            return Math.ceil(this.total / this.display);
        },
        grouplist:function(){ // 獲取分頁頁碼
            var len = this.page , temp = [], list = [], count = Math.floor(this.pagegroup / 2) ,center = this.current;
            if( len <= this.pagegroup ){
                while(len--){ temp.push({text:this.page-len,val:this.page-len});};
                return temp;
            }
            while(len--){ temp.push(this.page - len);};
            var idx = temp.indexOf(center);                
            (idx < count) && ( center = center + count - idx); 
            (this.current > this.page - count) && ( center = this.page - count);
            temp = temp.splice(center - count -1, this.pagegroup);                
            do {
                var t = temp.shift();
                list.push({
                    text: t,
                    val: t
                });
            }while(temp.length);                
            if( this.page > this.pagegroup ){
                (this.current > count + 1) && list.unshift({ text:'...',val: list[0].val - 1 });
                (this.current < this.page - count) && list.push({ text:'...',val: list[list.length - 1].val + 1 });
            }
            return list;
        }
    },
    methods: {
        setCurrent: function(idx) {
            if( this.current != idx && idx > 0 && idx < this.page + 1) {
                this.current = idx;
                this.$dispatch('pagechange',this.current);                    
            }                
        }
    }
});

使用:

<div id="app">
    <div class="container">
        <h1>  Vue 分頁元件 </h1>
        <pagination :total="total" :current.sync="current"></pagination>
        <pre>{{ $data|json }}</pre>
    </div>
</div>

<script>
var app = new Vue({
    el: '#app',
    data: {
        total: 81,     // 記錄總條數
        display: 10,   // 每頁顯示條數
        current: 1     // 當前第n頁 , 也可以 watch current 的變化 
    },
    events:{
        pagechange:function(p){     // 頁碼改變event , p 為新的 current
            console.log('pagechange',p);
        }
    }
});
</script>

效果傳送門

轉載:https://segmentfault.com/a/1190000005712999