栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

vue Pagination 简单封装分页组件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

vue Pagination 简单封装分页组件

示例




Events
事件名称说明回调参数
current-changecurrent-change currentPage 改变时会触发 当前页当前页
prev-click用户点击上一页按钮改变当前页后触发当前页
next-click用户点击下一页按钮改变当前页后触发当前页
Attributes
参数说明类型可选值默认值
total总条数Number-0
currentPage当前页数 需添加.sync修饰符Number-1
pageSize每页显示条目个数Number-10
pageCount页码按钮的数量,当总页数超过该值时会折叠Number大于等于 5 且小于等于 21 的奇数7
页面代码

js代码
export default {
    name: 'pagination',
    data() {
        return {
            leftIcon: '<',
            rightIcon: '>',
            page: 1,
            size: 10,
            inputValue: '',
            ifFocus: false,
        }
    },
    computed: {
    	// 总分页数
        totalPage() {
            console.log(Math.ceil(this.total / this.pageSize), this.pageCount);
            return Math.ceil(this.total / this.pageSize);
        },
        nums() {
            return Math.floor((this.pageCount - 3) / 2)
        },
        // 分页数据列表
        pageList() {
            const listTotal = this.totalPage;
            let count = this.pageCount;
            let list = [];
            const num = this.nums
            // console.log(num);
            if (listTotal <= count) {
                for (let i = 0; i < listTotal - 2; i++) {
                    list.push(i + 2);
                }
            } else {
                if ((this.page - num - 1) <= 1) {
                    // 从2开始
                    for (let i = 0; i < count - num ; i++) {
                        list[i] = i + 2;
                    }
                } else if ((this.page + num + 1) >= listTotal) {
                    // 从listTotal减小
                    for (let i = 0; i < count - num; i++) {
                        list[i] = listTotal - (count -num - i);
                    }
                } else {
                    // 左边
                    for (let i = 0; i < num; i++) {
                        list[i] = this.page - (num - i);
                        // console.log(i + 1);
                    }
                    // 中间数
                    list[num] = this.page;
                    // console.log(num + 1);
                    // 右边数
                    for (let i = 0; i < num; i++) {
                        list[num + 1 + i] = this.page + i + 1;
                        // console.log(num + 2 + i);
                    }
                }
            }
            console.log(list);
            return list
        },
        pageCountFloor() {
            return Math.floor(this.pageCount / 2)
        }
    },
    watch: {
        currentPage(newValue) {
            if (newValue !== this.page) this.page = newValue;
        },
        page(newValue) {
            if (newValue !== this.inputValue) this.inputValue = newValue;
        },
    },
    created() {
        if (this.currentPage !== this.page) this.page = this.currentPage;
        if (this.page !== this.inputValue) this.inputValue = this.page;
    },
    methods: {
        // 点击页数
        handlePage(item) {
            if (item === this.page) return
            this.page = item;
            this.$emit('update:currentPage', item); 
            this.$emit('current-change', item); 
        },
        // 输入框改变
        inputChange(e) {
            const value = Number(e.target.value);
            if (value !== 'NaN' && this.$utils.strType(value) === 'Number') {
                if (value > 0 && value <= this.totalPage) {
                    this.handlePage(value)
                } else this.inputValue = this.page;
            } else this.inputValue = this.page;
        },
        // 上一页
        prevClick() {
            if (this.page === 1) return
            this.handlePage(this.page - 1)
        },
        // 下一页
        nextClick() {
            if (this.page === this.totalPage) return
            this.handlePage(this.page + 1)
        },
        nextEllipsisClick() {
            if (this.page === 1) this.handlePage(this.page + this.nums + 2);
            else this.handlePage(this.page + this.nums);
        },
        prevEllipsisClick() {
            console.log(this.nums);
            if (this.page === this.totalPage) this.handlePage(this.page - this.nums - 2);
            else this.handlePage(this.page - this.nums);
        },
    }
}
样式代码

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1016470.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号