Vue分頁(yè)組件的封裝方法
前言
這個(gè)是基于vue2的分頁(yè)封裝,仿照elementUI而寫(xiě)的組件。
效果如圖

話不多說(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單頁(yè)面如何解決多個(gè)視頻同時(shí)僅能播放一個(gè)問(wèn)題
這篇文章主要介紹了vue單頁(yè)面如何解決多個(gè)視頻同時(shí)僅能播放一個(gè)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
解決element-ui table設(shè)置列fixed時(shí)X軸滾動(dòng)條無(wú)法拖動(dòng)問(wèn)題
這篇文章主要介紹了解決element-ui table設(shè)置列fixed時(shí)X軸滾動(dòng)條無(wú)法拖動(dòng)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue3.3?+?TS4構(gòu)建實(shí)現(xiàn)ElementPlus功能的組件庫(kù)示例
Vue.js?是目前最盛行的前端框架之一,而?TypeScript?則是一種靜態(tài)類(lèi)型言語(yǔ),它能夠讓開(kāi)發(fā)人員在編寫(xiě)代碼時(shí)愈加平安和高效,本文將引見(jiàn)如何運(yùn)用?Vue.js?3.3?和?TypeScript?4?構(gòu)建一個(gè)自主打造媲美?ElementPlus?的組件庫(kù)2023-10-10
Vue Element Sortablejs實(shí)現(xiàn)表格列的拖拽案例詳解
這篇文章主要介紹了Vue Element Sortablejs實(shí)現(xiàn)表格列的拖拽案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
vue動(dòng)態(tài)禁用控件綁定disable的例子
今天小編就為大家分享一篇vue動(dòng)態(tài)禁用控件綁定disable的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
Element-ui tree組件自定義節(jié)點(diǎn)使用方法代碼詳解
本文通過(guò)實(shí)例代碼給大家介紹了Element-ui tree組件自定義節(jié)點(diǎn)使用方法 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
vue.js 實(shí)現(xiàn)圖片本地預(yù)覽 裁剪 壓縮 上傳功能
這篇文章主要介紹了vue.js 實(shí)現(xiàn)圖片本地預(yù)覽裁剪壓縮上傳功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03
setup+ref+reactive實(shí)現(xiàn)vue3響應(yīng)式功能
這篇文章介紹了通過(guò)setup+ref+reactive實(shí)現(xiàn)vue3響應(yīng)式功能,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11

