分页组件-Pagination

分页组件-Pagination

<template>
    <div class="pagination">
        <button :disabled="pageNo == 1 || total == 0" @click="$emit('getPageNo',pageNo - 1)">上一页</button>
        <button v-if="startNumAndEndNum.start > 1" @click="$emit('getPageNo',1)" :class="{active:pageNo == 1}" >1</button>
        <button v-if="startNumAndEndNum.start > 2" >•••</button>
        <button v-for="(number,index) in startNumAndEndNum.end" :key="index" v-if="number >= startNumAndEndNum.start" @click="$emit('getPageNo',number)" :class="{active:pageNo == number}" >{{number}}</button>
        <button v-if="startNumAndEndNum.end < totalPage - 1">•••</button>
        <button v-if="startNumAndEndNum.end < totalPage" @click="$emit('getPageNo',totalPage)" :class="{active:pageNo == totalPage}" >{{totalPage}}</button>
        <button :disabled="pageNo == totalPage || total == 0" @click="$emit('getPageNo',pageNo + 1)" >下一页</button>

        <button style="margin-left: 30px">共 {{total}} 条</button>
    </div>
</template>
  
  <script>
export default {
    name: "Pagination",
    props:['pageNo','pageSize','total','continues'],
    computed:{
        // 获取总页数
        totalPage(){
            return Math.ceil(this.total/this.pageSize)
        },
        // 获取连续页的开始值和结束值
        startNumAndEndNum(){
            const {pageNo,continues,totalPage} = this
            let start = 0  // 初始化开始数字
            let end = 0    // 初始化结束数字
            // 判断如果连续的页数大于总页数
            if(continues > totalPage){
                start = 1
                end = totalPage
            }else{
                start = pageNo - parseInt(continues / 2)
                end = pageNo + parseInt(continues / 2)
                // 判断如果start<1
                if(start < 1){
                    start = 1
                    end = continues
                }
                // 判断end>总页数
                if(end > totalPage){
                    end = totalPage
                    start = totalPage - continues + 1
                }
            }
            return {start,end}
        }
    }
}
</script>
  
  <style lang="less" scoped>
  .pagination {
      text-align: center;
      button {
          margin: 0 5px;
          background-color: #f4f4f5;
          color: #606266;
          outline: none;
          border-radius: 2px;
          padding: 0 4px;
          vertical-align: top;
          display: inline-block;
          font-size: 13px;
          min-width: 35.5px;
          height: 28px;
          line-height: 28px;
          cursor: pointer;
          box-sizing: border-box;
          text-align: center;
          border: 0;
  
          &[disabled] {
              color: #c0c4cc;
              cursor: not-allowed;
          }
  
          &.active {
              cursor: not-allowed;
              background-color: orange;
              color: #fff;
          }
      }
  }
  </style>

在其他组件使用分页组件

<!-- 分页 -->
<template>
    <Pagination :pageNo="page" :pageSize="limit" :total="total" :continues="continues" @getPageNo="getPageNo" />
</template>
<script>
	import Pagination from './Pagination'
	export default {
    	data() {
	        return {
	            page:1, // 当前第几页
	            limit:3,  // 每页显示的数量
	            total:90,   // 总数
	            continues:5,  // 连续的页数
	        }
    	},
    	components:{
				Pagination 
		},
	    methods:{
	        // 获取当前点击的第几页
	        // 分页组件传来点击的第几页pageNo
	        getPageNo(pageNo){
	            this.page = pageNo
	            this.getData()
	        }
	    }
    }
}
</script>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值