微信小程序自定义组件实现tabs选项卡功能

一个自定义组件由 json wxml wxss js 4个文件组成。要编写一个自定义组件,首先需要在 json 文件中进行自定义组件声明(将 component 字段设为 true 可这一组文件设为自定义组件)

components/navigator/index.json

{ "component": true }

components/navigator/index.wxml

<!-- 自定义tab标签组件--> <!-- 标题列表--> <scroll-view scroll-x="true" wx:if="{{!ttype || ttype==2}}"> <view wx:for="{{tList}}" wx:key="*this"> <view bindtap="_swichNav" data-current="{{index}}">{{ !tname ? item.name : item[tname].name }}</view> </view> </scroll-view> <!--内容列表--> <slot> </slot>

components/navigator/index.js

//组件的对外属性,是属性名到属性设置的映射表,属性设置中可包含三个字段, type 表示属性类型、 value 表示属性初始值、 observer 表示属性值被更改时的响应函数 Component({ properties:{ //标题列表 tList:{ type: Array, value:[] }, //当前tab index currentTab:{ type:Number, value:0, observer: function (newVal, oldVal) { this.setData({ currentTab : newVal }) } } }, //组件的方法,包括事件响应函数和任意的自定义方法,关于事件响应函数的使用 methods:{ // 内部方法建议以下划线开头 _swichNav:function(e){ //自定义组件触发事件时,需要使用 triggerEvent 方法,指定事件名、detail对象和事件选项 this.triggerEvent('changeCurrent', { currentNum: e.currentTarget.dataset.current }) } } })

components/navigator/index.wxss

.scroll-view-x{ background-color: #fff; white-space: nowrap; position:fixed; z-index:10; top:0 } .scroll-view-x .scroll-view-item{ display:inline-block; margin:0 35rpx; line-height: 33px; cursor: pointer; } .on{ border-bottom: 2px solid red; color: red }

使用自定义组件

使用已注册的自定义组件前,首先要在页面的 json 文件中进行引用声明。此时需要提供每个自定义组件的标签名和对应的自定义组件文件路径:

pages/order-list/index.json

{ "navigationBarTitleText":"订单列表", "usingComponents": { "slideTab": "../../components/navigator/index" } }

这样,在页面的 wxml 中就可以像使用基础组件一样使用自定义组件。节点名即自定义组件的标签名,节点属性即传递给组件的属性值。

pages/order-list/index.wxml

<view > <slideTab tList="{{statusType}}" bind:changeCurrent="swichNav" currentTab="{{currentType}}" > <swiper current="{{currentType}}" duration="300" bindchange="bindChange"> <block> <swiper-item wx:for="{{list}}"> <view hidden="{{item.length ? true : false}}"> <image src=""></image> <view>暂无订单</view> </view> <scroll-view scroll-y="true" scroll-with-animation="true" lower-threshold="1" bindscrolltolower="scrolltolower" hidden="{{item ? flase : true}}"> <view wx:for="{{item}}" wx:key="childIndex" wx:for-item="childItem" > <view> <view>下单时间:{{childItem.dateAdd}}</view> <view>{{item.statusStr}}</view> </view> <view bindtap="orderDetail" data-id="{{childItem.id}}"> <view> <view>订单号 : {{childItem.orderNumber}} </view> <view wx:if="{{childItem.remark && childItem.remark != ''}}">备注: {{item.remark}}</view> </view> </view> <view > <scroll-view scroll-x="true"> <view wx:for="{{goodsMap[currentType][childItem.id]}}" wx:for-item="child_item"> <image src="https://www.jb51.net/{{child_item.pic}}"></image> </view> </scroll-view> </view> <view> <view>合计:¥ {{childItem.amountReal}}</view> <view hidden="{{childItem.status==0? false : true}}" bindtap="cancelOrderTap" data-id="{{childItem.id}}">取消订单</view> <view hidden="{{childItem.status==0? fslse : true}}" bindtap="toPayTap" data-id="{{childItem.id}}" data-money="{{childItem.amountReal}}">马上付款</view> </view> </view> </scroll-view> </swiper-item> </block> </swiper> </slideTab> </view>

pages/order-list/index.js

var wxpay = require('../../utils/pay.js') var app = getApp(); Page({ data:{ statusType:[ {name:"待付款",page:0}, {name:"待发货",page:0}, {name:"待收货",page:0}, {name:"待评价",page:0}, {name:"已完成",page:0}], currentType:0, list:[[],[],[],[],[]], goodsMap:[{},{},{},{},{}], logisticsMap:[{},{},{},{},{}], windowHeight:'' }, onLoad(options){ this.getList(); var systemInfo = wx.getSystemInfoSync() this.setData({ windowHeight: systemInfo.windowHeight, currentType:options.id ? options.id:0 }) }, // 点击tab切换 swichNav: function (res) { if (this.data.currentType == res.detail.currentNum) return; this.setData({ currentType: res.detail.currentNum }) } , bindChange:function(e){ this.setData({ currentType: e.detail.current }) if (!this.data.list[e.detail.current].length) this.getList(); } , getList(){ wx.showLoading(); var that = this; var postData = { token: app.globalData.token, status: that.data.currentType }; var _page = that.data.statusType[that.data.currentType].page+1 ;; wx.request({ url: app.globalData.baseUrl + '/order/list', data: postData, success: (res) => { wx.hideLoading(); var param = {}, str1 = "list[" + that.data.currentType + "]", str2 = 'statusType[' + that.data.currentType + '].page', str3 = "logisticsMap[" + that.data.currentType + "]", str4 = "goodsMap[" + that.data.currentType + "]" ; if (res.data.code == 0) { param[str1] = res.data.data.orderList ; param[str2] = _page ; param[str3] = res.data.data.logisticsMap ; param[str4] = res.data.data.goodsMap ; that.setData(param); } else { param[str1] = []; param[str3]= {}; param[str4] = {}; this.setData(param); } } }) }, orderDetail: function (e) { var orderId = e.currentTarget.dataset.id; wx.navigateTo({ url: "/pages/order-details/index?id=" + orderId }) }, cancelOrderTap: function (e) { var that = this; var orderId = e.currentTarget.dataset.id; wx.showModal({ title: '确定要取消该订单吗?', content: '', success: function (res) { if (res.confirm) { wx.showLoading(); wx.request({ url: app.globalData.baseUrl + '/order/close', data: { token: app.globalData.token, orderId: orderId }, success: (res) => { wx.hideLoading(); if (res.data.code == 0) { var param = {}, str = 'statusType[' + that.data.currentType + '].page'; param[str]=0; that.getList(); } } }) } } }) } })

pages/order-list/index.wxss

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2e1e15946f75f6d4d74a84713930233c.html