手写分页器

前端三大件:轮播图、分页、日历。这属于前端开发常见三种业务!!

封装分页器组件的时候:需要知道哪些条件?

1:分页器组件需要知道我一共展示多少条数据 ----total【100条数据】

2:每一个需要展示几条数据------pageSize【每一页3条数据】

3:需要知道当前在第几页-------pageNo[当前在第几页]

4:需要知道连续页码数【起始数字、结束数字:连续页码数市场当中一般5、7、9】奇数,对称好看 continues

分析过程:

已知条件: total=【99】 pageSize =【3】 pageNo=6 continues 5

4 5 6 7 8

已知条件: total=【99】 pageSize =【3】 pageNo= 1 continues 5

错误:-1 0 1 2 3 正确: 1 2 3 4 5

已知条件: total=【99】 pageSize =【3】 pageNo= 2 continues 5

错误: 0 1 2 3 4 正确:1 2 3 4 5

已知条件: total=【99】 pageSize =【3】 pageNo= 33 continues 5

错误: 31 32 33 34 35 正确:29 30 31 32 33

已知条件: total=【99】 pageSize =【3】 pageNo= 32 continues 5

错误:30 31 32 33 34 正确: 29 30 31 32 33

<!-- 主要DOM结构 -->
<div class="pagination">
    <button 
        v-if="startNumAndendNum.start != 1"                   
        @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>
    <!-- v-if不能和v-for一起使用 -->
    <button 
      v-for="(num,index) in startNumAndendNum.end" :key="index" 
      v-show="num >= startNumAndendNum.start"
      @click="$emit('getPageNo',num)"
      :class="{active:pageNo==num}">
      {{num}}</button>
​
    <button 
         v-if="startNumAndendNum.end <= this.pageTotal -       parseInt(this.continue / 2)"
    >···</button>
    <button 
         v-if="startNumAndendNum.end != this.pageTotal" 
         @click="$emit('getPageNo',pageTotal)"
    >{{pageTotal}}</button>
    <button 
         v-if="startNumAndendNum.end != this.pageTotal"  
         @click="$emit('getPageNo',pageNo + 1)"
         :class="{active:pageNo==pageTotal}"
    >下一页</button>
                                                                        
    <button style="margin-left: 30px">共 {{total}} 条</button>
  </div>
<script>
props:["pageNo","pageSize","total","continue"],
computed:{
    pageTotal(){
        return Math.ceil(this.total / this.pageSize);
    },
    //重点******
    startNumAndendNum(){
          //先定义两个变量存储起始数字与结束数字
          let start = 0;
          let end = 0;
          //连续页码数5【如果出现不正常现象,就是不够五页】
          if(this.continue > this.pageTotal){
            start = 1;
            end = this.pageTotal;
          }else{
            //正常现象
            start = this.pageNo - parseInt(this.continue / 2);
            end = this.pageNo + parseInt(this.continue / 2);
          }
          //把出现的不正常现象的起始数字与结束数字纠正
          if(start < 1){
            start = 1;;
            end = this.continue;
          }
          if(end > this.pageTotal){
            end = this.pageTotal;
            start = this.pageTotal - this.continue + 1
          }
          return {start,end}
        },
}
<script/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三年ing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值