Vue分頁(yè)組件的封裝方法
前言
這個(gè)是基于vue2的分頁(yè)封裝,仿照elementUI而寫的組件。
效果如圖
話不多說(shuō),直接上代碼
<template> ? <div class="pagination"> ? ? <!-- 總頁(yè)數(shù) --> ? ? <div class="total">共{{ total }}條</div> ? ? <!-- 選擇每頁(yè)的條數(shù) --> ? ? <select name="" id="size_select" v-model="sizes" @change="sizeChange"> ? ? ? <option v-for="item in pageSizes" :key="item" :value="item"> ? ? ? ? {{ item }}條/頁(yè) ? ? ? </option> ? ? </select> ? ? <div class="pagenum"> ? ? ? <!-- 上一頁(yè) --> ? ? ? <el-button ? ? ? ? icon="el-icon-arrow-left" ? ? ? ? :disabled="backDisabled" ? ? ? ? circle ? ? ? ? @click="back" ? ? ? ></el-button> ? ? ? <!-- 頁(yè)碼 --> ? ? ? <ul> ? ? ? ? <li ? ? ? ? ? :class="currentPage == item ? 'active' : ''" ? ? ? ? ? v-for="(item, index) in pagenum" ? ? ? ? ? :key="index" ? ? ? ? ? @click="toPage(item)" ? ? ? ? > ? ? ? ? ? {{ item }} ? ? ? ? </li> ? ? ? </ul> ? ? ? <!-- 下一頁(yè) --> ? ? ? <el-button ? ? ? ? icon="el-icon-arrow-right" ? ? ? ? :disabled="forwardDisabled" ? ? ? ? circle ? ? ? ? @click="forward" ? ? ? ></el-button> ? ? </div> ? </div> </template> ? <script> export default { ? name: "pagination", ? props: { ? ? total: { ?// 總數(shù) ? ? ? type: null, ? ? ? required: true, ? ? }, ? ? pageSizes: { // 可選擇的每頁(yè)條數(shù) ? ? ? type: Array, ? ? }, ? ? pageSize: { ?// 每頁(yè)顯示的條數(shù) ? ? ? type: Number, ? ? ? required: true, ? ? }, ? ? currentPage: { // 當(dāng)前頁(yè) ? ? ? type: Number, ? ? ? required: true, ? ? }, ? }, ? data() { ? ? return { ? ? ? sizes: this.pageSize, ?// 接收props傳來(lái)的pageSize ? ? ? nowPage: this.currentPage, ?// 接收props傳來(lái)的currentPage ? ? }; ? }, ? computed: { ? ? allPage() { ?// 計(jì)算所有的頁(yè)數(shù) ? ? ? return Math.ceil(this.total / this.pageSize); ? ? }, ? ? backDisabled() { ?// 是否禁用上一頁(yè) ? ? ? return this.currentPage == 1; ? ? }, ? ? forwardDisabled() { // 是否禁用下一頁(yè) ? ? ? return this.currentPage == this.allPage; ? ? }, ? ? pagenum() { ? // 計(jì)算顯示不同的頁(yè) ? ? ? if (this.allPage - this.nowPage > 6) { ?// ? ? ? ? ? if (this.nowPage > 6) { ? ? ? ? ? return [ ? ? ? ? ? ? 1, ? ? ? ? ? ? "...", ? ? ? ? ? ? this.nowPage - 2, ? ? ? ? ? ? this.nowPage - 1, ? ? ? ? ? ? this.nowPage, ? ? ? ? ? ? this.nowPage + 1, ? ? ? ? ? ? this.nowPage + 2, ? ? ? ? ? ? "...", ? ? ? ? ? ? this.allPage, ? ? ? ? ? ]; ? ? ? ? } else { ? ? ? ? ? if (this.allPage > 8) { ? ? ? ? ? ? return [1, 2, 3, 4, 5, 6, "...", this.allPage]; ? ? ? ? ? } else { ? ? ? ? ? ? return this.allPage; ? ? ? ? ? } ? ? ? ? } ? ? ? } else { ? ? ? ? if (this.nowPage < 6) { ? ? ? ? ? return this.allPage; ? ? ? ? } else { ? ? ? ? ? return [ ? ? ? ? ? ? 1, ? ? ? ? ? ? "...", ? ? ? ? ? ? this.allPage - 5, ? ? ? ? ? ? this.allPage - 4, ? ? ? ? ? ? this.allPage - 3, ? ? ? ? ? ? this.allPage - 2, ? ? ? ? ? ? this.allPage - 1, ? ? ? ? ? ? this.allPage, ? ? ? ? ? ]; ? ? ? ? } ? ? ? } ? ? }, ? }, ? methods: { ? ? sizeChange() { ?// 每頁(yè)限制條數(shù)改變觸發(fā)事件 ? ? ? this.$emit("sizeChange", this.sizes); ? ? }, ? ? forward() { ?// 點(diǎn)擊下一頁(yè) ? ? ? this.$emit("currentChange", (this.nowPage += 1)); ? ? }, ? ? back() { ?// 點(diǎn)擊上一頁(yè) ? ? ? this.$emit("currentChange", (this.nowPage -= 1)); ? ? }, ? ? toPage(val) { ?// 點(diǎn)擊頁(yè)數(shù) ? ? ? if (val == "...") { ? ? ? ? console.log(2); ? ? ? } else { ? ? ? ? this.nowPage = val; ? ? ? ? this.$emit("currentChange", val); ? ? ? } ? ? }, ? }, }; </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue本地模擬服務(wù)器請(qǐng)求mock數(shù)據(jù)的方法詳解
這篇文章主要給大家介紹了關(guān)于vue本地模擬服務(wù)器請(qǐng)求mock數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03解決vue 退出動(dòng)畫無(wú)效的問(wèn)題
這篇文章主要介紹了解決vue 退出動(dòng)畫無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08vue style width a href動(dòng)態(tài)拼接問(wèn)題的解決
這篇文章主要介紹了vue style width a href動(dòng)態(tài)拼接問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08使用vue-cli創(chuàng)建項(xiàng)目的圖文教程(新手入門篇)
這篇文章主要介紹了新手入門vue 使用vue-cli創(chuàng)建項(xiàng)目的圖文教程,本文是針對(duì)完全不了解過(guò)vue和npm的小白而寫的,需要的朋友可以參考下2018-05-05vue實(shí)現(xiàn)日歷表格(element-ui)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)日歷表格(element-ui),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09