【小程序】分页组件封装

参考elementui中分页组件

README.md

参数 说明
total 总条目数
currentPage 当前页数
pageSize 每页显示个数
pagerCount 页码按钮的数量,当总页数超过该值时会折叠
hideOnSinglePage 只有一页时是否隐藏

代码

  1. pagination.ttml
html 复制代码
<view class="pagination">
  <dg-button
    icon="fanyezuo"
    type="text"
    bind:handleClick="handlePrevClick"
    disabled="{{currentPage === 1}}"
    tt-if="{{ pageCount > 0 && !(hideOnSinglePage && pageCount === 1) }}"
  />
  <view
    class="pager"
    bindtap="onPagerClick"
  >
    <view
      tt-if="{{ pageCount > 0 && !(hideOnSinglePage && pageCount === 1) }}"
      class="number {{currentPage === 1 ? 'active' : ''}}"
      data-pager="{{1}}"
    >
      1
    </view>
    <view
      class="number pager-more"
      tt-if="{{showPrevMore}}"
    >
      ...
    </view>
    <view
      tt:for="{{pagers}}"
      tt:for-item="pager"
      class="number {{currentPage === pager ? 'active' : ''}}"
      data-pager="{{pager}}"
    >
      {{ pager }}
    </view>
    <view
      class="number pager-more"
      tt-if="{{showNextMore}}"
    >
      ...
    </view>
    <view
      tt-if="{{ pageCount > 1 }}"
      class="number {{currentPage === pageCount ? 'active' : ''}}"
      data-pager="{{pageCount}}"
    >
      {{ pageCount }}
    </view>
  </view>
  <dg-button
    icon="fanyeyou"
    type="text"
    bind:handleClick="handleNextClick"
    disabled="{{currentPage === pageCount}}"
    tt-if="{{ pageCount > 0 && !(hideOnSinglePage && pageCount === 1) }}"
  />
</view>
  1. pagination.json
javascript 复制代码
{
  "component": true,
  "usingComponents": {
    "dg-button": "/components/button/button"
  }
}
  1. pagination.js
javascript 复制代码
Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    total: {
      type: Number,
      value: 0,
      observer: 'handlePagers'
    },
    currentPage: {
      type: Number,
      value: 1,
      observer: 'handlePagers'
    },
    pageSize: {
      type: Number,
      value: 10
    },
    // 页码按钮的数量,当总页数超过该值时会折叠【大于等于 5 且小于等于 21 的奇数】
    pagerCount: {
      type: Number,
      value: 5
    },
    // 只有一页时是否隐藏
    hideOnSinglePage: {
      type: Boolean,
      value: true
    }
  },
  data: {
    showPrevMore: false,
    pagers: [], // 中间的数字列表
    showNextMore: false,
    pageCount: 0, // 总页数
  },
  methods: {
    handlePagers: function () {
      this.setData({
        pageCount: Math.max(1, Math.ceil(this.properties.total / this.properties.pageSize))
      })

      const pagerCount = this.properties.pagerCount;
      const halfPagerCount = (pagerCount - 1) / 2;

      const currentPage = Number(this.properties.currentPage);
      const pageCount = Number(this.data.pageCount);

      let showPrevMore = false;
      let showNextMore = false;

      if (pageCount > pagerCount) {
        if (currentPage > pagerCount - halfPagerCount) {
          showPrevMore = true;
        }

        if (currentPage < pageCount - halfPagerCount) {
          showNextMore = true;
        }
      }

      const array = [];

      if (showPrevMore && !showNextMore) {
        const startPage = pageCount - (pagerCount - 2);
        for (let i = startPage; i < pageCount; i++) {
          array.push(i);
        }
      } else if (!showPrevMore && showNextMore) {
        for (let i = 2; i < pagerCount; i++) {
          array.push(i);
        }
      } else if (showPrevMore && showNextMore) {
        const offset = Math.floor(pagerCount / 2) - 1;
        for (let i = currentPage - offset ; i <= currentPage + offset; i++) {
          array.push(i);
        }
      } else {
        for (let i = 2; i < pageCount; i++) {
          array.push(i);
        }
      }

      this.setData({
        showPrevMore,
        showNextMore,
        pagers: array
      })

    },
    onPagerClick: function (e) {
      const pager = e.target.dataset.pager;
      // 防止用户点击的是...
      if(!pager) {
        return
      }
      this.triggerEvent('currentChange', pager);
    },
    handlePrevClick: function(){
      if(this.properties.currentPage > 1){
        const newVal = this.properties.currentPage - 1;
        this.triggerEvent('currentChange', newVal);
      }
    },
    handleNextClick: function(){
      if(this.properties.currentPage < this.data.pageCount){
        const newVal = this.properties.currentPage + 1;
        this.triggerEvent('currentChange', newVal);
      }
    },
  }
})
  1. pagination.ttss
css 复制代码
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
}
.pager {
  display: flex;  
  align-items: center;
  justify-content: center;
}
.number {
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: rgba(0,0,0,0.65);
  border: 1px solid transparent;
  border-radius: 2px;
}
.active {
  color: var(--color-primary);
  border-color: var(--color-primary);
}
.pager-more {
  color: #CCCCCC;
}

我组件中 没有改变currentPage 是通过emit 然后在外层改变的 如果大家想要在组件中改变的话

那就在那几个函数中新增一个字段 curPage 代表页码。然后currentPage改变的时候赋值给这个值

相关推荐
2501_9151063213 分钟前
iOS 使用记录和能耗监控实战,如何查看电池电量消耗、App 使用时长与性能数据(uni-app 开发调试必备指南)
android·ios·小程序·uni-app·cocoa·iphone·webview
じòぴé南冸じょうげん8 小时前
小程序的project.private.config.json是无依赖文件,那可以删除吗?
前端·小程序·json
2501_916013749 小时前
HTTPS 抓包难点分析,从端口到工具的实战应对
网络协议·http·ios·小程序·https·uni-app·iphone
2501_9159184111 小时前
uni-app 项目 iOS 上架效率优化 从工具选择到流程改进的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张12 小时前
如何在不同 iOS 设备上测试和上架 uni-app 应用 实战全流程解析
android·ios·小程序·https·uni-app·iphone·webview
微三云-轩12 小时前
区块链:重构企业数字化的信任核心与创新动力
人工智能·小程序·区块链·生活·我店
2501_915918411 天前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
黑马源码库miui520861 天前
JAVA同城打车小程序APP打车顺风车滴滴车跑腿源码微信小程序打车源码
java·微信·微信小程序·小程序·uni-app
一口十个小甜虾1 天前
微信小程序体验版,当打开调试模式正常访问,关闭之后无法访问
微信小程序·小程序
悟空码字1 天前
微信开放平台第三方平台,可以管理多个微信小程序
微信·小程序·开放平台