基于bootstrap的分页器,Vue组件

3 篇文章 0 订阅
1 篇文章 0 订阅

基于bootstrap的分页器,需要引入bootstrap的相关依赖,点击页码数字可以进行相应的响应(换页/加载数据)

Pagenation.vue

<template>
    <div id="pagenation" class="row justify-content-center">
        <div class="col-auto">
            <ul v-if="pagenation.total>pagenation.pageSize" class="pagination ">
                <li class="page-item">
                    <a href="javascript:void(0);" @click="startPage" class="page-link">首页</a>
                </li>
                <li v-for="i in displayPage"
                    :key="'pagenation_'+i" :class="pagenationItemActive(i)">
                    <a href="#" @click="selectPage(i)" class="page-link">{{i}}</a>
                </li>
                <li class="page-item ">
                    <a href="javascript:void(0);" @click="endPage" class="page-link">尾页</a>
                </li>
            </ul>
        </div>
        <div class="col-auto  mb-3">
            <div class="input-group" style="width: 150px">
                <input type="text" class="form-control" :placeholder="jumpPageNumPlaceholder" v-model.number="jumpPageNum">
                <div class="input-group-append">
                    <button @click="jumpPageClick" @blur="jumpPageBlur" class="input-group-text">跳转</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Pagenation",
        data() {
            return {
                jumpPageNum: '',
                jumpPageNumPlaceholder: '跳转到...'
            }
        },
        props: {
            pagenation: Object,
            // pagenation: {
            //     total: 22,
            //     pageSize: 10,
            //     pageNum: 1,
            // },

            pageNumChange: Function,
        },
        computed: {
            displayPage() {
                let totalPage = this.pagenation.total % this.pagenation.pageSize === 0 ? (parseInt(this.pagenation.total / this.pagenation.pageSize)) : (parseInt(this.pagenation.total / this.pagenation.pageSize) + 1)
                let pageNum = this.pagenation.pageNum;
                if (totalPage < 10) {
                    return totalPage;
                } else {
                    let arr = []
                    if (pageNum < 5) {
                        for (let i = 1; i <= 10; i++) {
                            arr.push(i)
                        }
                    } else if (pageNum >= totalPage - 5) {
                        for (let i = totalPage - 9; i <= totalPage; i++) {
                            arr.push(i)
                        }
                    } else {
                        for (let i = pageNum - 4; i <= pageNum + 5 && i < totalPage; i++) {
                            arr.push(i)
                        }
                    }
                    return arr
                }
            }
        },
        watch: {
            'pagenation.pageNum'(newValue, oldValue) {
                console.log(newValue, oldValue)
                this.pageNumChange(newValue, oldValue)
            },
        },
        methods: {
            pagenationItemActive(i) {
                if (i == this.pagenation.pageNum) {
                    return 'page-item active'
                } else {
                    return 'page-item'
                }
            },
            selectPage(pageNum) {
                this.pagenation.pageNum = pageNum
            },
            startPage() {
                if (this.pagenation.pageNum !== 1) {
                    this.pagenation.pageNum = 1
                }
            },
            endPage() {
                let totalPage = this.pagenation.total % this.pagenation.pageSize === 0 ? (parseInt(this.pagenation.total / this.pagenation.pageSize)) : (parseInt(this.pagenation.total / this.pagenation.pageSize) + 1)
                if (this.pagenation.pageNum !== totalPage) {
                    this.pagenation.pageNum = totalPage
                }
            },
            jumpPageBlur() {
                this.jumpPageNumPlaceholder = '跳转到...'
            },
            jumpPageClick() {
                let regex = /^[0-9]+$/;
                if (regex.test(this.jumpPageNum)) {
                    let jumpPageNum = parseInt(this.jumpPageNum)
                    if (jumpPageNum > 0 && jumpPageNum <= (this.pagenation.total % this.pagenation.pageSize === 0 ? (parseInt(this.pagenation.total / this.pagenation.pageSize)) : (parseInt(this.pagenation.total / this.pagenation.pageSize) + 1))) {
                        this.pagenation.pageNum = jumpPageNum
                    } else {
                        this.jumpPageNumPlaceholder = '超出范围'
                    }
                } else {
                    this.jumpPageNumPlaceholder = '输入错误'
                }
                this.jumpPageNum = '';
            }
        }
    }
</script>

<style scoped>

</style>

应用:

引入组件Pagenation.vue

  <!--分页器-->
<Pagenation :pagenation="pagenation" :page-num-change="pageNumChange"></Pagenation>
data提供的数据
pagenation: {
    total: 22,//总数据量
    pageSize: 10,//每页显示数据量
    pageNum: 1,//当前页码
},

methods定义的方法pageNumChange:当当前页码改变时触发,用于加载数据

pageNumChange(newPageNum, oldPageNum){
//可以传入两个参数,newPageNum:新的页码,oldPageNum:之前的页码
  console.log("pageNumChange",newPageNum, oldPageNum)
},
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Bootstrap的分页组件可以帮助我们快速地实现分页功能。下面是一个简单的例子,演示如何使用Bootstrap的分页组件: ```html <!DOCTYPE html> <html> <head> <title>Bootstrap分页组件示例</title> <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css"> </head> <body> <div class="container"> <h1>Bootstrap分页组件示例</h1> <hr> <div class="row"> <div class="col-sm-12"> <table class="table table-striped"> <thead> <tr> <th>#</th> <th>姓名</th> <th>年龄</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>张三</td> <td>18</td> </tr> <tr> <td>2</td> <td>李四</td> <td>20</td> </tr> <tr> <td>3</td> <td>王五</td> <td>22</td> </tr> <tr> <td>4</td> <td>赵六</td> <td>24</td> </tr> <tr> <td>5</td> <td>钱七</td> <td>26</td> </tr> </tbody> </table> <nav> <ul class="pagination"> <li class="page-item disabled"> <a class="page-link" href="#" tabindex="-1" aria-disabled="true">上一页</a> </li> <li class="page-item active" aria-current="page"> <a class="page-link" href="#">1</a> </li> <li class="page-item"> <a class="page-link" href="#">2</a> </li> <li class="page-item"> <a class="page-link" href="#">3</a> </li> <li class="page-item"> <a class="page-link" href="#">下一页</a> </li> </ul> </nav> </div> </div> </div> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script> </body> </html> ``` 在这个例子中,我们首先引入了Bootstrap的CSS和JavaScript文件。然后,我们创建了一个包含表格和分页组件的容。分页组件使用了`<nav>`和`<ul>`标签来实现,每个页码都是一个`<li>`元素,其中当前页码使用了`active`类。我们还可以使用`disabled`类来禁用上一页和下一页按钮。最后,我们引入了jQuery和Bootstrap的JavaScript文件,以便分页组件正常工作。 这样,我们就完成了一个基于Bootstrap的分页组件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值