// components/paging/index.js Component({ /** * 组件的属性列表 */ properties: { currentIndex: { //当前页码 type: Number, value: 1 }, totalPage: { type: Number } }, /** * 组件的初始数据 */ data: { index: 1, total: 0, pageMask: false, prevBtnDis: true, nextBtnDis: true }, /** * 组件的方法列表 */ lifetimes: { // 在组件实例进入页面节点树时执行 attached: function () { this.setData({ index: this.data.currentIndex, total: this.data.totalPage }) }, // 在组件实例被从页面节点树移除时执行 detached: function () { } }, methods: { //每次改变页码就调用该函数 currentChangeEmit: function (touchState) { // 自定义组件向父组件传值 const option = { currentIndex: this.data.index, touchState: true }; if (touchState) option.touchState = !touchState; // pagingChange 自定义名称事件,父组件中使用 this.triggerEvent('pagingChange', option) /* 在父组件中写上bind:pagingChanget="get_emit",在父组件中就需要调用get_emit事件 */ }, //开启页码弹窗 shopPagePopup: function () { this.setData({ pageMask: true }) }, //关闭页码弹窗 hidePagePopup: function () { this.setData({ pageMask: false }) }, //更改页码点击事件 changePage: function (e) { //console.log("更改页码事件:",e); this.setData({ pageMask: false, index: e.currentTarget.dataset.index }) if (this.data.prevBtnDis || this.data.nextBtnDis) { this.currentChangeEmit(true); }else{ this.currentChangeEmit(); } this.judgeBtnDis(); this.setData({ index: this.data.currentIndex, total: this.data.totalPage }) }, //上一页点击事件 prevPage: function () { let num = (this.data.index == 1) ? 1 : this.data.index - 1; this.setData({ index: num }) if (this.data.prevBtnDis) { this.currentChangeEmit(true); } else { this.currentChangeEmit(); } this.judgeBtnDis(); this.setData({ index: this.data.currentIndex, total: this.data.totalPage }) }, //下一页点击事件 nextPage: function () { let num = (this.data.index == this.data.total) ? this.data.total : this.data.index + 1; this.setData({ index: num }) if (this.data.nextBtnDis) { this.currentChangeEmit(true); } else { this.currentChangeEmit(); } this.judgeBtnDis(); this.setData({ index: this.data.currentIndex, total: this.data.totalPage }) }, //判断按钮是否为disabled judgeBtnDis: function () { let index = this.data.index; if (index == this.data.total) { this.setData({ nextBtnDis: true }) if(index==1){ this.setData({ prevBtnDis: true }) }else{ this.setData({ prevBtnDis: false }) } } else if (index == 1){ this.setData({ prevBtnDis: true }) if (index == this.data.total){ this.setData({ nextBtnDis: true }) }else{ this.setData({ nextBtnDis: false }) } }else{ this.setData({ prevBtnDis: false }) this.setData({ nextBtnDis: false }) } }, }, observers: { 'totalPage': function(rate) { this.setData({ index: this.data.currentIndex, total: this.data.totalPage }) } } })