【小程序】分页组件封装

参考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改变的时候赋值给这个值

相关推荐
小疯仔7 小时前
轨迹在地图上“漂“了 500 米?一文吃透 WGS-84→GCJ-02 坐标转换引擎(GPX→JSON 实战源码剖析)
小程序·json·notepad++
lhldsg12 小时前
全民健身解决方案软件开发实战:从架构设计到部署指南
java·前端·数据库·小程序
2501_9160074717 小时前
使用Apple Dashboards显示和自定iOS应用性能指标与数据可视化指南
android·ios·小程序·https·uni-app·iphone·webview
软件技术新观察18 小时前
企业数字化开发科普:软件定制、小程序、APP 开发全解析
人工智能·小程序
凡泰AI19 小时前
小程序容器技术解析:一个能够同时在多端APP运行同一个小程序的SDK需要关注哪些内容?
开发语言·小程序·mpaas·uni·技术实践
爱折腾的编程老炮19 小时前
潮玩小程序订单模型-一张表装下所有玩法
spring boot·小程序·uni-app
万岳科技程序员小金19 小时前
同城O2O外卖系统源码开发详解:从APP、小程序到后台管理平台的完整架构解析
小程序·架构·同城外卖系统源码·外卖app开发·外卖小程序开发·外卖软件开发·外卖平台搭建
博、、19 小时前
全民健身解决方案小程序系统开发实战:从架构设计到上线指南
小程序
2501_9151063220 小时前
Flutter iOS混淆打包详细教程与步骤
android·flutter·ios·小程序·uni-app·iphone·webview
码云数智-大飞1 天前
2026 年跨端开发决战:小程序原生 vs uni-app vs Taro 深度对比
小程序·uni-app·taro