Vue分頁組件的封裝方法
更新時(shí)間:2022年07月05日 16:44:28 作者:kanami154
這篇文章主要為大家詳細(xì)介紹了Vue分頁組件的封裝方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
前言
這個(gè)是基于vue2的分頁封裝,仿照elementUI而寫的組件。
效果如圖

話不多說,直接上代碼
<template>
? <div class="pagination">
? ? <!-- 總頁數(shù) -->
? ? <div class="total">共{{ total }}條</div>
? ? <!-- 選擇每頁的條數(shù) -->
? ? <select name="" id="size_select" v-model="sizes" @change="sizeChange">
? ? ? <option v-for="item in pageSizes" :key="item" :value="item">
? ? ? ? {{ item }}條/頁
? ? ? </option>
? ? </select>
? ? <div class="pagenum">
? ? ? <!-- 上一頁 -->
? ? ? <el-button
? ? ? ? icon="el-icon-arrow-left"
? ? ? ? :disabled="backDisabled"
? ? ? ? circle
? ? ? ? @click="back"
? ? ? ></el-button>
? ? ? <!-- 頁碼 -->
? ? ? <ul>
? ? ? ? <li
? ? ? ? ? :class="currentPage == item ? 'active' : ''"
? ? ? ? ? v-for="(item, index) in pagenum"
? ? ? ? ? :key="index"
? ? ? ? ? @click="toPage(item)"
? ? ? ? >
? ? ? ? ? {{ item }}
? ? ? ? </li>
? ? ? </ul>
? ? ? <!-- 下一頁 -->
? ? ? <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: { // 可選擇的每頁條數(shù)
? ? ? type: Array,
? ? },
? ? pageSize: { ?// 每頁顯示的條數(shù)
? ? ? type: Number,
? ? ? required: true,
? ? },
? ? currentPage: { // 當(dāng)前頁
? ? ? type: Number,
? ? ? required: true,
? ? },
? },
? data() {
? ? return {
? ? ? sizes: this.pageSize, ?// 接收props傳來的pageSize
? ? ? nowPage: this.currentPage, ?// 接收props傳來的currentPage
? ? };
? },
? computed: {
? ? allPage() { ?// 計(jì)算所有的頁數(shù)
? ? ? return Math.ceil(this.total / this.pageSize);
? ? },
? ? backDisabled() { ?// 是否禁用上一頁
? ? ? return this.currentPage == 1;
? ? },
? ? forwardDisabled() { // 是否禁用下一頁
? ? ? return this.currentPage == this.allPage;
? ? },
? ? pagenum() { ? // 計(jì)算顯示不同的頁
? ? ? 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() { ?// 每頁限制條數(shù)改變觸發(fā)事件
? ? ? this.$emit("sizeChange", this.sizes);
? ? },
? ? forward() { ?// 點(diǎn)擊下一頁
? ? ? this.$emit("currentChange", (this.nowPage += 1));
? ? },
? ? back() { ?// 點(diǎn)擊上一頁
? ? ? this.$emit("currentChange", (this.nowPage -= 1));
? ? },
? ? toPage(val) { ?// 點(diǎn)擊頁數(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)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
vue style width a href動(dòng)態(tài)拼接問題的解決
這篇文章主要介紹了vue style width a href動(dòng)態(tài)拼接問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
使用vue-cli創(chuàng)建項(xiàng)目的圖文教程(新手入門篇)
這篇文章主要介紹了新手入門vue 使用vue-cli創(chuàng)建項(xiàng)目的圖文教程,本文是針對(duì)完全不了解過vue和npm的小白而寫的,需要的朋友可以參考下2018-05-05
vue實(shí)現(xiàn)日歷表格(element-ui)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)日歷表格(element-ui),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09

