欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 【小程序】分页组件封装

【小程序】分页组件封装

2025/11/5 11:36:23 来源:https://blog.csdn.net/weixin_43957384/article/details/143620503  浏览:    关键词:【小程序】分页组件封装

参考elementui中分页组件

请添加图片描述

README.md

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

代码

  1. pagination.ttml
<view class="pagination"><dg-buttonicon="fanyezuo"type="text"bind:handleClick="handlePrevClick"disabled="{{currentPage === 1}}"tt-if="{{ pageCount > 0 && !(hideOnSinglePage && pageCount === 1) }}"/><viewclass="pager"bindtap="onPagerClick"><viewtt-if="{{ pageCount > 0 && !(hideOnSinglePage && pageCount === 1) }}"class="number {{currentPage === 1 ? 'active' : ''}}"data-pager="{{1}}">1</view><viewclass="number pager-more"tt-if="{{showPrevMore}}">...</view><viewtt:for="{{pagers}}"tt:for-item="pager"class="number {{currentPage === pager ? 'active' : ''}}"data-pager="{{pager}}">{{ pager }}</view><viewclass="number pager-more"tt-if="{{showNextMore}}">...</view><viewtt-if="{{ pageCount > 1 }}"class="number {{currentPage === pageCount ? 'active' : ''}}"data-pager="{{pageCount}}">{{ pageCount }}</view></view><dg-buttonicon="fanyeyou"type="text"bind:handleClick="handleNextClick"disabled="{{currentPage === pageCount}}"tt-if="{{ pageCount > 0 && !(hideOnSinglePage && pageCount === 1) }}"/>
</view>
  1. pagination.json
{"component": true,"usingComponents": {"dg-button": "/components/button/button"}
}
  1. pagination.js
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
.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改变的时候赋值给这个值

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词