Vue實(shí)現(xiàn)web分頁(yè)組件

原文地址:http://www.maoyupeng.com/web-pagination-component-for-vue.html

效果演示

效果演示

源代碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>測(cè)試分頁(yè) - www.maoyupeng.com</title>
    <style type="text/css">
        body{padding:0; margin: 0; broder:none; } #app {width: 500px; height: 200px; margin: 0 auto; text-align: center; background-color: #cccccc; } #mylink {color: #efefef; } .pagination{list-style: none; text-align: center; height: 50px; padding-top: 50px; } .pagination > li {float: left; margin: 0 5px; } [v-cloak] {display: none; } </style>
</head>
<body>
    <div id="app" v-cloak>
        <ul class="pagination">
            <li>
                <a v-if="currentPage == 1" >首頁(yè)</a>
                <a v-else href="javascript:;" @click="next(1)">首頁(yè)</a>
            </li>
            <li v-if="currentPage<=1"><a>上一頁(yè)</a></li>
            <li v-else><a href="javascript:;" @click="next(currentPage-1)">上一頁(yè)</a></li>


            <li v-for="item in pagingList">
                <a v-if="currentPage==item.key || sign ==item.key" >{{item.key}}</a>
                <a v-else href="javascript:;" @click="next(item.value)">{{item.key}}</a>
            </li>

            <li v-if="currentPage>=totalPageCount"><a>下一頁(yè)</a></li>
            <li v-else><a href="javascript:;" @click="next(currentPage+1)">下一頁(yè)</a></li>
            <li>
                <a v-if="totalPageCount == currentPage">尾頁(yè)</a>
                <a v-else href="javascript:;" @click="next(totalPageCount)">尾頁(yè)</a>
            </li>
        </ul>
        <p>共:{{totalPageCount||0}}頁(yè),當(dāng)前頁(yè)為第{{currentPage||0}}頁(yè)   設(shè)置總頁(yè)數(shù):<input style="width:20px;" v-model="totalPageCount"></p>
        <a  target="_blank" id="mylink">http://www.maoyupeng.com 帶注解版本</a>
    </div>
    
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script type="text/javascript">
        var app = new Vue({
          el: '#app',
          data: {
            // 省略的符號(hào)
            sign:'...',
            // 省略號(hào)位置
            signIndex:4,
            // 總頁(yè)數(shù)
            totalPageCount: 4,
            // 當(dāng)前頁(yè)
            currentPage:1,
            // 顯示在頁(yè)面的數(shù)組列表
            pagingList:[]
          },
          watch: {
            totalPageCount (val) {
                var that = this
                if (!val || val == '') return;
                that.currentPage = 1;
                that.init()
            },
            currentPage (val) {
                var that = this
                that.init()
            }
          },
          methods: {
            // 跳轉(zhuǎn)到某頁(yè)碼
            next (num) {
                var that = this
                if (num <= 1) that.currentPage = 1;
                else if (num >= that.totalPageCount) that.currentPage = that.totalPageCount;
                else that.currentPage = num;
            },
            // 初始化數(shù)據(jù)
            fetchData () {
                var that = this

                that.pagingList = [];
                var tmp = null;

                
                if ((that.totalPageCount) > 6) {
                    if (((that.totalPageCount-1) == (that.totalPageCount - that.currentPage)) && (that.totalPageCount - that.currentPage) > 5) {
                        for (var i=1;i<7;i++) {
                            if (i < that.signIndex) {
                                tmp = {key:i, value:i }
                            } else if (i== that.signIndex) {
                                tmp = {key:that.sign, value:0 }
                            } else if (i == (that.signIndex + 1) ) {
                                tmp = {key:that.totalPageCount - 1, value:that.totalPageCount - 1 }
                            } else {
                                tmp = {key:that.totalPageCount, value:that.totalPageCount }
                            }
                            that.pagingList.push(tmp)
                        }
                    } else if (((that.totalPageCount - that.currentPage) <= that.signIndex)){
                        var starNum = that.totalPageCount - 5;
                        for (var i=starNum;i<starNum+6;i++) {
                            tmp = {key:i, value:i }
                            that.pagingList.push(tmp)
                        }
                    } else {
                        var starNum = that.currentPage - 1;
                        for (var i=1;i<7;i++) {
                            if (i < that.signIndex) {
                                tmp = {key:(starNum - 1) + i, value:(starNum - 1) + i }
                            } else if (i== that.signIndex) {
                                tmp = {key:that.sign, value:0 }
                            } else if (i == (that.signIndex + 1) ) {
                                tmp = {key:that.totalPageCount - 1, value:that.totalPageCount - 1 }
                            } else {
                                tmp = {key:that.totalPageCount, value:that.totalPageCount }
                            }
                            that.pagingList.push(tmp)
                        }
                    }
                } else {
                    for (var i =0; i <that.totalPageCount; i++) {
                        tmp = {key:i+1, value:i+1 }
                        that.pagingList.push(tmp)
                    }
                }
            },
            init () {
                var that = this

                that.fetchData()
            }
          },
          mounted () {
            var that = this

            that.init()
          }
        })  
    </script>
</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容