vue 分页自定义封装

封装 js
// 分页封装
(function () {

Vue.component('pagination', {
    template: `
         <div class="pagination" style=" display: flex;justify-content: end;align-items: center;">
           <div style="font-size: 14px;margin-right:10px"">共<span style="margin:0 5px">{{total}}</span>条</div>
           <div >
           <el-select v-model="pageSize" placeholder="请选择" class="pageSizes" @change="selectChange" >
           <el-option
             class="fpage"
             v-for="item in pageSizes"
             :key="item.value"
             :label="item.label"
             :value="item.value">
            </el-option>
            </el-select>
           </div>
           <div style="display: flex;align-items: center;margin:0 10px"">
           <i class="el-icon-arrow-left currentPage" :class="{button:currentPage==1}" @click="leftClick"></i>
           <ul style="margin:0 10px;display: flex;" >
           <template v-for="(item,index) in countListdate ">
           <li :class="{current:item==currentPage,button:count==1}"  kye="index" style="margin:0 10px" class='currentPage' @click="currentPageClick(item)">{{item}}<li>
           </template>
           </ul>
           <i class="el-icon-arrow-right currentPage" :class="{button:currentPage==countList.length}"  @click="rightClick" ></i>
           </div>
           <div>
           </div>
           <div  style="display: flex;align-items: center;" class="right">
           <div style="font-size: 14px;">前往</div>
           <div style="margin:0 10px">
           <el-input v-model="herfPage"  @blur="herfPageBlur" >
           </el-input>
           </div> 
           <div style="font-size:14px;">页</div>
           </div>
         </div>
        `,
    //  <div v-if="count>5">...</div>
    data() {
        return {
            total: 21, //总条数
            count: 1, //总页数 
            countList: ["1"], // 初始总页数 
            countListdate: ['1'], //页面展示总页数
            pageSize: 10, // 容量
            currentPageSon: 1,
            currentPage: 1, //当前页数
            pageSizes: [], //分页选择
            herfPage: '1',
        }
    },
    props: ['total', 'currentPage', 'pageSize', 'pageSizes'],
    mounted() {

    },
    destroyed() {

    },
    watch: {
        total(val) {
            this.count = Math.ceil(this.total / this.pageSize)
            if (this.count == 0) {
                this.countList = ["1"]
                this.countListdate = ['1']
            } else {
                this.countList = []
                for (let index = 0; index < this.count; index++) {
                    this.countList.push(`${index+1}`)
                }
                console.log("this.countList", this.countList);
                if (this.countList.length > 8) {
                    this.pagesDate(1)
                } else {
                    this.countListdate = this.countList
                }


            }

        },
        currentPage(val) {
            this.pagesDate(val)

        },
        pageSize(val) {
            this.pageSize = val
            this.herfPage = '1'
            if (val == '全部') {
                this.count = 1
                this.countList = ["1"]
                this.countListdate = ['1']
            } else {
                this.count = Math.ceil(this.total / this.pageSize)
                if (this.count == 0) {
                    this.countList = ["1"]
                    this.countListdate = ['1']
                } else {
                    this.countList = []
                    for (let index = 0; index < this.count; index++) {
                        this.countList.push(`${index+1}`)
                    }
                    if (this.countList.length > 8) {
                        this.pagesDate(1)
                    } else {
                        this.countListdate = this.countList
                        setTimeout(() => {
                            this.currentPage = 1
                        }, 500)
                    }


                }
            }
        }
    },

    methods: {
        leftClick() {
            if (this.currentPage > 1) {
                let num = this.currentPage--
                this.pagesDate(num)
                this.$emit('current-change', this.currentPage)
                // this.herfPage = num - 1
            }

        },
        rightClick() {
            if (Number(this.currentPage) < Number(this.countList.length)) {
                let num = this.currentPage++
                this.pagesDate(num)
                this.$emit('current-change', this.currentPage)
                // this.herfPage = num + 1
            }

        },

        // 初始化
        init() {
            this.count = this.total % this.pageSize
        },
        // 当前页
        currentPageClick(val) {
            this.herfPage = val
            if (this.count == 1) {
                return
            } else {
                this.$emit('current-change', val)
            }

        },
        // 页数数组
        pagesDate(val, type) {
            if (val == "全部") {
                this.countList = ["1"]
                this.countListdate = ['1']
                return
            }
            if (this.countList.length > 6) {
                if ((Number(this.countList.length) - Number(val)) == 0 || (Number(this.countList.length) - Number(val)) == 1 || (Number(this.countList.length) - Number(val)) == 2 || (Number(this.countList.length) - Number(val)) == 3) {
                    this.countListdate = this.countList.slice(Number(this.countList.length) - 7, this.countList.length)
                }
                if (val == 1 || val == 2 || val == 3) {
                    this.countListdate = this.countList.slice(0, 7)
                    if (type == 'ry') {
                        setTimeout(() => {
                            this.currentPage = 1
                        }, 500)
                    }
                }
                if (val > 3 && (Number(this.countList.length) - Number(val)) > 3) {
                    this.countListdate = []
                    this.countListdate.push(`${val}`)
                    this.countListdate.unshift(`${Number(val)-1}`)
                    this.countListdate.unshift(`${Number(val)-2}`)
                    this.countListdate.unshift(`${Number(val)-3}`)
                    this.countListdate.push(`${Number(val) + 1}`)
                    this.countListdate.push(`${Number(val) + 2}`)
                    this.countListdate.push(`${Number(val) + 3}`)
                }


            } else {
                this.countListdate = this.countList
            }
        },
        // 触发容量
        selectChange(val) {

            if (val == '全部' || val == "") {
                this.pageSize = '1'
                this.currentPage = 1
                this.herfPage = '1'
                this.pagesDate(val)
            }
            this.$emit('size-change', val, '1')



        },
        // 页面跳转 
        herfPageBlur() {
            if (parseInt(this.count) >= parseInt(this.herfPage)) {
                this.currentPage = this.herfPage
                this.pagesDate(this.currentPage)
                this.$emit('current-change', this.herfPage)
            } else {
                this.herfPage = this.currentPage
                this.$emit('current-change', this.herfPage)
            }
        }
    },
})

})();

        <div class="block" style="display: flex;justify-content: flex-end;  margin: 5px 0;">
            <pagination :current-page="formInline.pageNum" :total="formInline.total" :page-sizes="pageSizeList"
                @size-change="handleSizeChange" :page-size="formInline.pageSize"
                @current-change="handleCurrentChange">
            </pagination>
        </div>

current-page // 当前 页数
page-size // 当前 容量
total
size-change //分页 页数

current-change//分页 容量

page-sizes// 分页参数

     pageSizeList: [{
            value: '500',
            label: '500/页'
        }, {
            value: '1000',
            label: '1000/页'
        }, {
            value: '5000',
            label: '5000/页'
        }, {
            value: '10000',
            label: '10000/页'
        }, {
            value: '全部',
            label: '全部'
        }],
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue3中封装分页组件的方法与Vue2有所不同。以下是一个基本的封装分页组件的方法: 1. 首先,在你的项目中创建一个名为`Pagination.vue`的组件文件。 2. 在`Pagination.vue`组件中,你可以使用`<template>`标签来定义组件的结构。可以使用`<div>`标签来包裹分页组件的内容,比如页码和按钮。 3. 在`<script>`标签中,你需要导入`vue`并声明组件。你可以使用`ref`来追踪当前页码,并且使用`computed`属性来计算总页数。 4. 在组件内部,你可以创建一个`methods`对象,并在其中定义一些方法来处理页码的变化。比如,你可以创建`goToPage`方法来跳转到指定的页码。 5. 最后,在`<style>`标签中,你可以定义组件的样式,如页码的颜色和按钮的样式。 在你的项目中使用这个封装的分页组件的方法如下: 1. 在需要使用分页功能的组件中,使用`import`关键字导入刚刚封装的`Pagination`组件。 2. 在`components`属性中注册`Pagination`组件。 3. 在`<template>`标签中使用自定义的分页组件。可以使用`v-model`指令来双向绑定当前页码。 通过上述步骤,你就可以在Vue3中封装一个分页组件并在项目中使用了。这个组件可以提供分页功能,让用户可以方便地切换页码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [基于Vue如何封装分页组件](https://download.csdn.net/download/weixin_38550605/12789728)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Vue3 element-ui实现Pagination分页组件--封装分页](https://blog.csdn.net/coinisi_li/article/details/128952886)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值