基于Vue如何封裝分頁(yè)組件
使用Vue做雙向綁定的時(shí)候,可能經(jīng)常會(huì)用到分頁(yè)功能
接下來(lái)我們來(lái)封裝一個(gè)分頁(yè)組件
先定義樣式文件 pagination.css
ul, li { margin: 0px; padding: 0px; } .page-bar { -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } .page-button-disabled { color:#ddd !important; } .page-bar li { list-style: none; display: inline-block; } .page-bar li:first-child > a { margin-left: 0px; } .page-bar a { border: 1px solid #ddd; text-decoration: none; position: relative; float: left; padding: 6px 12px; margin-left: -1px; line-height: 1.42857143; color: #337ab7; cursor: pointer; } .page-bar a:hover { background-color: #eee; } .page-bar .active a { color: #fff; cursor: default; background-color: #337ab7; border-color: #337ab7; } .page-bar i { font-style: normal; color: #d44950; margin: 0px 4px; font-size: 12px; }
js文件 pagination.js
(function (vue) { // html模板信息 var template = '<div class="page-bar"> \ <ul> \ <li><a class="{{ setButtonClass(0) }}" v-on:click="prvePage(cur)">上一頁(yè)</a></li> \ <li v-for="index in indexs" v-bind:class="{ active: cur == index }"> \ <a v-on:click="btnClick(index)">{{ index < 1 ? "..." : index }}</a> \ </li> \ <li><a class="{{ setButtonClass(1) }}" v-on:click="nextPage(cur)">下一頁(yè)</a></li> \ </ul> \ </div>' var pagination = vue.extend({ template: template, props: ['cur', 'all'], computed: { indexs: function () { var left = 1 var right = this.all var ar = [] if (this.all >= 11) { if (this.cur > 5 && this.cur < this.all - 4) { left = this.cur - 5 right = this.cur + 4 } else { if (this.cur <= 5) { left = 1 right = 10 } else { right = this.all left = this.all - 9 } } } while (left <= right) { ar.push(left) left++ } if (ar[0] > 1) { ar[0] = 1; ar[1] = -1; } if (ar[ar.length - 1] < this.all) { ar[ar.length - 1] = this.all; ar[ar.length - 2] = 0; } return ar } }, methods: { // 頁(yè)碼點(diǎn)擊事件 btnClick: function (data) { if (data < 1) return; if (data != this.cur) { this.cur = data this.$dispatch('btn-click', data) } }, // 下一頁(yè) nextPage: function (data) { if (this.cur >= this.all) return; this.btnClick(this.cur + 1); }, // 上一頁(yè) prvePage: function (data) { if (this.cur <= 1) return; this.btnClick(this.cur - 1); }, // 設(shè)置按鈕禁用樣式 setButtonClass: function (isNextButton) { if (isNextButton) { return this.cur >= this.all ? "page-button-disabled" : "" } else { return this.cur <= 1 ? "page-button-disabled" : "" } } } }) vue.Pagination = pagination })(Vue)
pagination分頁(yè)組件就封裝好了,需要使用的時(shí)候,引入以上兩個(gè)文件即可
接下來(lái)我們測(cè)試下效果
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> <title></title> <script src="vue.js"></script> <link href="pagination.css" rel="stylesheet" /> <script src="pagination.js"></script> </head> <body> <div id="app"> <vue-pagination :cur.sync="cur" :all.sync="all" v-on:btn-click="listen"></vue-pagination> <p>{{msg}}</p> </div> <script type="text/javascript"> var app = new Vue({ el: '#app', data: { // 當(dāng)前頁(yè)碼 cur: 1, // 總頁(yè)數(shù) all: 100, msg: '' }, components: { // 引用組件 'vue-pagination': Vue.Pagination }, methods: { listen: function (data) { // 翻頁(yè)會(huì)觸發(fā)此事件 this.msg = '當(dāng)前頁(yè)碼:' + data } } }) </script> </body> </html>
最終效果
頁(yè)碼切換事件在listen中處理即可
點(diǎn)擊此處下載源碼:源碼下載地址
以上所述是小編給大家介紹的基于Vue如何封裝分頁(yè)組件,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue項(xiàng)目中token驗(yàn)證登錄(前端部分)
這篇文章主要為大家詳細(xì)介紹了Vue項(xiàng)目中token驗(yàn)證登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08vue computed的set方法無(wú)效問(wèn)題及解決
這篇文章主要介紹了vue computed的set方法無(wú)效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11vue動(dòng)態(tài)繪制四分之三圓環(huán)圖效果
這篇文章主要介紹了vue動(dòng)態(tài)繪制四分之三圓環(huán)圖效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09vue中實(shí)現(xiàn)可編輯table及其中加入下拉選項(xiàng)
這篇文章主要介紹了vue中實(shí)現(xiàn)可編輯table及其中加入下拉選項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue.config.js配置proxy代理產(chǎn)生404錯(cuò)誤的原因及解決
這篇文章主要介紹了vue.config.js配置proxy代理產(chǎn)生404錯(cuò)誤的原因及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06vue子傳父關(guān)于.sync與$emit的實(shí)現(xiàn)
這篇文章主要介紹了vue子傳父關(guān)于.sync與$emit的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11VUE前端實(shí)現(xiàn)token的無(wú)感刷新方式
這篇文章主要介紹了VUE前端實(shí)現(xiàn)token的無(wú)感刷新方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11vue + any-touch實(shí)現(xiàn)一個(gè)iscroll 實(shí)現(xiàn)拖拽和滑動(dòng)動(dòng)畫效果
這篇文章主要介紹了vue + any-touch實(shí)現(xiàn)一個(gè)iscroll實(shí)現(xiàn)拖拽和滑動(dòng)動(dòng)畫效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04