当前位置:   article > 正文

微信小程序自定义组件开发---分页组件_微信小程序分页组件

微信小程序分页组件

刚开始搞小程序,搞了一周后,可以开始写组件了,写个分页组件练练手:

父组件,json文件引入:

  1. "usingComponents": {
  2. "paging": "../../components/paging/index" //引入存放你组件的路径
  3. }

父组件,wxml文件使用:

<paging current-index="{{page}}" total-page="{{pageTotal}}" bind:pagingChange="pagChange"></paging>

父组件,js文件使用:

  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. page: 1,
  7. pageTotal: 10
  8. },
  9. /**
  10. * 生命周期函数--监听页面加载
  11. */
  12. onLoad: function (options) {
  13. //初始化页面
  14. },
  15. //页码改变事件
  16. pagChange: function(e){
  17. console.log("页码改变时传到父组件的值", e);
  18. }
  19. })

子组件,json文件定义为component

  1. {
  2. "component": true //只有在json文件中定义component为true才能在其他页面引用组件
  3. }

子组件,wxml文件编写:

  1. <!--components/paging/index.wxml-->
  2. <view class="page-control">
  3. <view class="page-control-btns">
  4. <view class="page-btn {{prevBtnDis?'btn-disabled':''}}" bindtap="prevPage">上一页</view>
  5. <view class="page-number" bindtap="shopPagePopup"><text>{{index}}</text>/<text>{{total}}</text></view>
  6. <view class="page-btn {{nextBtnDis?'btn-disabled':''}}" bindtap="nextPage">下一页</view>
  7. </view>
  8. <view class="page-container" hidden="{{!pageMask}}">
  9. <view class="page-mask" bindtap="hidePagePopup"></view>
  10. <view class="page-popup">
  11. <view class="page-popup-box">
  12. <view class="page-line" wx:for="{{total}}" wx:for-index="ind" data-index="{{ind+1}}" bindtap="changePage">第{{item+1}}页</view>
  13. </view>
  14. </view>
  15. </view>
  16. </view>

子组件,wxss文件样式编辑:

  1. /* components/paging/index.wxss */
  2. view,text,image{
  3. padding: 0;
  4. margin: 0;
  5. box-sizing: border-box;
  6. }
  7. .page-control{
  8. width: 100%;
  9. }
  10. .page-control .page-control-btns{
  11. width: 100%;
  12. padding: 20rpx 0;
  13. display: flex;
  14. align-items: center;
  15. justify-content: space-around;
  16. }
  17. .page-control .page-control-btns .page-number{
  18. width: 20%;
  19. text-align: center;
  20. color: #333;
  21. }
  22. .page-control .page-control-btns .page-number:active{
  23. background-color: #ddd;
  24. }
  25. .page-control .page-control-btns .page-btn{
  26. width: 30%;
  27. padding: 15rpx 20rpx;
  28. font-size: 30rpx;
  29. background-color: #0099CC;
  30. color: #fff;
  31. border-radius: 10rpx;
  32. text-align: center;
  33. }
  34. .page-control .page-control-btns .page-btn:active{
  35. opacity: .5;
  36. }
  37. .page-control .page-control-btns .btn-disabled{
  38. background-color: #ddd;
  39. color: #999;
  40. }
  41. .page-container{
  42. position: fixed;
  43. top: 0rpx;
  44. left: 0rpx;
  45. width: 100%;
  46. height: 100%;
  47. z-index: 999;
  48. }
  49. .page-mask{
  50. width: 100%;
  51. height: 100%;
  52. position: absolute;
  53. left: 0;
  54. top: 0;
  55. background-color: rgba(0,0,0,0.5);
  56. }
  57. .page-popup{
  58. width: 100%;
  59. height: 100%;
  60. display: flex;
  61. flex-wrap: wrap;
  62. align-items: center;
  63. justify-content: center;
  64. }
  65. .page-popup-box{
  66. width: 60%;
  67. margin: 0 auto;
  68. background-color: #fff;
  69. height: 60%;
  70. border-radius: 10rpx;
  71. z-index: 9999;
  72. overflow: auto;
  73. }
  74. .page-line{
  75. width: 100%;
  76. height: 80rpx;
  77. line-height: 80rpx;
  78. padding: 0rpx 20rpx;
  79. border-bottom: 1rpx solid #e2e2e2;
  80. }
  81. .page-line:active{
  82. background-color: #ddd;
  83. }

子组件,js文件逻辑编写:

  1. // components/paging/index.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. currentIndex: { //当前页码
  8. type: Number,
  9. value: 1
  10. },
  11. totalPage: {
  12. type: Number
  13. }
  14. },
  15. /**
  16. * 组件的初始数据
  17. */
  18. data: {
  19. index: 1,
  20. total: 10,
  21. pageMask: false,
  22. prevBtnDis: true,
  23. nextBtnDis: false
  24. },
  25. /**
  26. * 组件的方法列表
  27. */
  28. lifetimes: {
  29. // 在组件实例进入页面节点树时执行
  30. attached: function () {
  31. this.setData({
  32. index: this.data.currentIndex,
  33. total: this.data.totalPage
  34. })
  35. },
  36. // 在组件实例被从页面节点树移除时执行
  37. detached: function () {
  38. }
  39. },
  40. methods: {
  41. //每次改变页码就调用该函数
  42. currentChangeEmit: function (touchState) {
  43. // 自定义组件向父组件传值
  44. const option = {
  45. currentIndex: this.data.index,
  46. touchState: true
  47. };
  48. if (touchState) option.touchState = !touchState;
  49. // pagingChange 自定义名称事件,父组件中使用
  50. this.triggerEvent('pagingChange', option)
  51. /*
  52. 在父组件中写上bind:pagingChanget="get_emit",在父组件中就需要调用get_emit事件
  53. */
  54. },
  55. //开启页码弹窗
  56. shopPagePopup: function () {
  57. this.setData({
  58. pageMask: true
  59. })
  60. },
  61. //关闭页码弹窗
  62. hidePagePopup: function () {
  63. this.setData({
  64. pageMask: false
  65. })
  66. },
  67. //更改页码点击事件
  68. changePage: function (e) {
  69. //console.log("更改页码事件:",e);
  70. this.setData({
  71. pageMask: false,
  72. index: e.currentTarget.dataset.index
  73. })
  74. if (this.data.prevBtnDis || this.data.nextBtnDis) {
  75. this.currentChangeEmit(true);
  76. }else{
  77. this.currentChangeEmit();
  78. }
  79. this.judgeBtnDis();
  80. },
  81. //上一页点击事件
  82. prevPage: function () {
  83. let num = (this.data.index == 1) ? 1 : this.data.index - 1;
  84. this.setData({
  85. index: num
  86. })
  87. if (this.data.prevBtnDis) {
  88. this.currentChangeEmit(true);
  89. } else {
  90. this.currentChangeEmit();
  91. }
  92. this.judgeBtnDis();
  93. },
  94. //下一页点击事件
  95. nextPage: function () {
  96. let num = (this.data.index == this.data.total) ? this.data.total : this.data.index + 1;
  97. this.setData({
  98. index: num
  99. })
  100. if (this.data.nextBtnDis) {
  101. this.currentChangeEmit(true);
  102. } else {
  103. this.currentChangeEmit();
  104. }
  105. this.judgeBtnDis();
  106. },
  107. //判断按钮是否为disabled
  108. judgeBtnDis: function () {
  109. let index = this.data.index;
  110. if (index == this.data.total) {
  111. this.setData({
  112. nextBtnDis: true
  113. })
  114. if(index==1){
  115. this.setData({
  116. prevBtnDis: true
  117. })
  118. }else{
  119. this.setData({
  120. prevBtnDis: false
  121. })
  122. }
  123. } else if (index == 1){
  124. this.setData({
  125. prevBtnDis: true
  126. })
  127. if (index == this.data.total){
  128. this.setData({
  129. nextBtnDis: true
  130. })
  131. }else{
  132. this.setData({
  133. nextBtnDis: false
  134. })
  135. }
  136. }else{
  137. this.setData({
  138. prevBtnDis: false
  139. })
  140. this.setData({
  141. nextBtnDis: false
  142. })
  143. }
  144. }
  145. }
  146. })

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/96943
推荐阅读
相关标签
  

闽ICP备14008679号