Vue分頁組件的封裝方法
更新時間:2022年07月05日 16:44:28 作者:kanami154
這篇文章主要為大家詳細(xì)介紹了Vue分頁組件的封裝方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
前言
這個是基于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() { ?// 計算所有的頁數(shù)
? ? ? return Math.ceil(this.total / this.pageSize);
? ? },
? ? backDisabled() { ?// 是否禁用上一頁
? ? ? return this.currentPage == 1;
? ? },
? ? forwardDisabled() { // 是否禁用下一頁
? ? ? return this.currentPage == this.allPage;
? ? },
? ? pagenum() { ? // 計算顯示不同的頁
? ? ? 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() { ?// 點擊下一頁
? ? ? this.$emit("currentChange", (this.nowPage += 1));
? ? },
? ? back() { ?// 點擊上一頁
? ? ? this.$emit("currentChange", (this.nowPage -= 1));
? ? },
? ? toPage(val) { ?// 點擊頁數(shù)
? ? ? if (val == "...") {
? ? ? ? console.log(2);
? ? ? } else {
? ? ? ? this.nowPage = val;
? ? ? ? this.$emit("currentChange", val);
? ? ? }
? ? },
? },
};
</script>以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決element-ui table設(shè)置列fixed時X軸滾動條無法拖動問題
這篇文章主要介紹了解決element-ui table設(shè)置列fixed時X軸滾動條無法拖動問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue3.3?+?TS4構(gòu)建實現(xiàn)ElementPlus功能的組件庫示例
Vue.js?是目前最盛行的前端框架之一,而?TypeScript?則是一種靜態(tài)類型言語,它能夠讓開發(fā)人員在編寫代碼時愈加平安和高效,本文將引見如何運用?Vue.js?3.3?和?TypeScript?4?構(gòu)建一個自主打造媲美?ElementPlus?的組件庫2023-10-10
Vue Element Sortablejs實現(xiàn)表格列的拖拽案例詳解
這篇文章主要介紹了Vue Element Sortablejs實現(xiàn)表格列的拖拽案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Element-ui tree組件自定義節(jié)點使用方法代碼詳解
本文通過實例代碼給大家介紹了Element-ui tree組件自定義節(jié)點使用方法 ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09
vue.js 實現(xiàn)圖片本地預(yù)覽 裁剪 壓縮 上傳功能
這篇文章主要介紹了vue.js 實現(xiàn)圖片本地預(yù)覽裁剪壓縮上傳功能,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-03-03
setup+ref+reactive實現(xiàn)vue3響應(yīng)式功能
這篇文章介紹了通過setup+ref+reactive實現(xiàn)vue3響應(yīng)式功能,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11

