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


Posted in Javascript onJuly 14, 2018

本文为大家分享了微信小程序实现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" class="scroll-view-x" wx:if="{{!ttype || ttype==2}}">
 <view class="scroll-view-item" wx:for="{{tList}}" wx:key="*this">
 <view class="{{currentTab==(index) ? 'on' : ''}}" 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" style="height: {{windowHeight-35}}px;margin-top:35px;">
 <block>
  <swiper-item wx:for="{{list}}">
  <view class="no-order" hidden="{{item.length ? true : false}}">
  <image src="../../assets/imgs/no-order.png" class="no-order-img"></image>
  <view class="text">暂无订单</view>
  </view>
  <scroll-view scroll-y="true" class="order-list" scroll-with-animation="true" lower-threshold="1" bindscrolltolower="scrolltolower" style="height: {{windowHeight-35}}px;" hidden="{{item ? flase : true}}">
  <view class="a-order" wx:for="{{item}}" wx:key="childIndex" wx:for-item="childItem" >
  <view class="order-date">
   <view class="date-box">下单时间:{{childItem.dateAdd}}</view>
   <view class="status {{(childItem.status==-1 || childItem.status==4) ? '':'red'}}">{{item.statusStr}}</view>
  </view>
  <view class="goods-info" bindtap="orderDetail" data-id="{{childItem.id}}">
   <view class="goods-des">
   <view>订单号 : {{childItem.orderNumber}} </view>
   <view wx:if="{{childItem.remark && childItem.remark != ''}}">备注: {{item.remark}}</view>
   </view>
  </view>
  <view >
   <scroll-view class="goods-img-container" scroll-x="true">
   <view class="img-box" wx:for="{{goodsMap[currentType][childItem.id]}}" wx:for-item="child_item">
    <image src="{{child_item.pic}}" class="goods-img"></image>
   </view>
   </scroll-view>
  </view>
  <view class="price-box">
   <view class="total-price">合计:¥ {{childItem.amountReal}}</view>
   <view class="btn cancel-btn" hidden="{{childItem.status==0? false : true}}" bindtap="cancelOrderTap" data-id="{{childItem.id}}">取消订单</view>
   <view class="btn topay-btn" 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

.container{
 width: 100%;
 background-color: #F2f2f2;
}
.status-box{
 width:100%;
 height: 88rpx;
 line-height: 88rpx;
 display: flex;
 justify-content: space-between;
 align-items: center;
 background-color: #fff;
}
.status-box .status-label{
 width: 150rpx;
 height: 100%;
 text-align: center;
 font-size:28rpx;
 color:#353535;
 box-sizing: border-box;
 position: relative;
}
.status-box .status-label.active{
 color:#e64340;
 border-bottom: 6rpx solid #e64340;
}
.status-box .status-label .red-dot{
 width: 16rpx;
 height: 16rpx;
 position: absolute;
 left: 116rpx;
 top:23rpx;
 background-color: #f43530;
 border-radius: 50%;
}
.no-order{
 width: 100%;
 position: absolute;
 bottom: 0;
 top:0;
 left: 0;
 right: 0;
 text-align: center;
 padding-top: 203rpx;
 background-color: #F2f2f2;
}
.no-order-img{
 width: 81rpx;
 height: 96rpx;
 margin-bottom: 31rpx;
}
.no-order .text{
 font-size:28rpx;
 color:#999999;
 text-align: center
}
.order-list{
 width: 100%;
}
.order-list .a-order{
 width: 100%;
 background-color: #fff;
 margin-top: 20rpx;
}
.order-list .a-order .order-date{
 padding: 0 30rpx;
 height: 88rpx;
 display: flex;
 justify-content: space-between;
 font-size:26rpx;
 color:#000000;
 align-items: center;
}
.order-list .a-order .order-date .red{
 font-size:26rpx;
 color:#e64340;
}
.a-order .goods-info,
.goods-img-container{
 width: 720rpx;
 margin-left: 30rpx;
 border-top: 1rpx solid #eee;
 border-bottom: 1rpx solid #eee;
 padding: 30rpx 0;
 display: flex;
 align-items: center;
}
.goods-info .img-box{
 width: 120rpx;
 height: 120rpx;
 overflow: hidden;
 margin-right: 30rpx;
 background-color: #f7f7f7;
}
.goods-info .img-box .goods-img,
.goods-img-container .img-box .goods-img{
 width: 120rpx;
 height: 120rpx;
}
.goods-info .goods-des{
 width: 540rpx;
 height: 78rpx;
 line-height: 39rpx;
 font-size:26rpx;
 color:#000000;
 overflow: hidden;
}
.goods-img-container{
 height: 180rpx;
 box-sizing: border-box;
 white-space: nowrap;
}
.goods-img-container .img-box{
 width: 120rpx;
 height: 120rpx;
 overflow: hidden;
 margin-right: 20rpx;
 background-color: #f7f7f7;
 display: inline-block;
}
.order-list .a-order .price-box{
 position: relative;
 width: 720rpx;
 height: 100rpx;
 margin-left: 30rpx;
 box-sizing: border-box;
 padding: 20rpx 30rpx 20rpx 0;
 display: flex;
 align-items: center;
 justify-content: space-between;
 font-size:26rpx;
}
.order-list .a-order .price-box .total-price{
 font-size:26rpx;
 color:#e64340;
}
.a-order .price-box .btn{
 width: 166rpx;
 height: 60rpx;
 box-sizing: border-box;
 text-align: center;
 line-height: 60rpx;
 border-radius: 6rpx;
 margin-left: 20rpx;
}
.a-order .price-box .cancel-btn{
 border: 1rpx solid #ccc;
 position: absolute;
 right: 216rpx;
 top:20rpx;
}
.a-order .price-box .topay-btn{
 border:1px solid #e64340;
 color: #e64340;
}

效果图

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

项目地址:微信小程序实现tabs选项卡功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
VBScript版代码高亮
Jun 26 Javascript
top.location.href 没有权限 解决方法
Aug 05 Javascript
JQuery 动画卷页 返回顶部 动画特效(兼容Chrome)
Feb 15 Javascript
JS的encodeURI和java的URLDecoder.decode使用介绍
May 08 Javascript
原生js实现下拉框功能(支持键盘事件)
Jan 13 Javascript
js 输入框 正则表达式(菜鸟必看教程)
Feb 19 Javascript
一个可复用的vue分页组件
May 15 Javascript
JavaScript for循环 if判断语句(学习笔记)
Oct 11 Javascript
npm 更改默认全局路径以及国内镜像的方法
May 16 Javascript
element-ui循环显示radio控件信息的方法
Aug 24 Javascript
详解js创建对象的几种方法及继承
Apr 12 Javascript
vue路由切换时取消之前的所有请求操作
Sep 01 Javascript
微信小程序scroll-view仿拼多多横向滑动滚动条
Apr 21 #Javascript
微信小程序实现自上而下字幕滚动
Jul 14 #Javascript
微信小程序scroll-view实现字幕滚动
Jul 14 #Javascript
微信小程序仿RadioGroup改变样式的处理方案
Jul 13 #Javascript
通过封装scroll.js 获取滚动条的值
Jul 13 #Javascript
详解vue.js根据不同环境(正式、测试)打包到不同目录
Jul 13 #Javascript
Angular5.0 子组件通过service传递值给父组件的方法
Jul 13 #Javascript
You might like
PHP的变量类型和作用域详解
2014/03/12 PHP
PHP实现QQ登录实例代码
2016/01/14 PHP
php封装的page分页类完整实例
2016/10/18 PHP
YII2框架中添加自定义模块的方法实例分析
2020/03/18 PHP
使用jquery.qrcode生成彩色二维码实例
2014/08/08 Javascript
基于jQuery实现的扇形定时器附源码下载
2015/10/20 Javascript
解决JavaScript数字精度丢失问题的方法
2015/12/03 Javascript
轮播图组件js代码
2016/08/08 Javascript
JavaScript实现的微信二维码图片生成器的示例
2016/10/26 Javascript
小程序关于请求同步的总结
2019/05/05 Javascript
js实现九宫格布局效果
2020/05/28 Javascript
vue如何在项目中调用腾讯云的滑动验证码
2020/07/15 Javascript
[29:59]完美世界DOTA2联赛PWL S3 Forest vs access 第二场 12.11
2020/12/13 DOTA
Python 面向对象 成员的访问约束
2008/12/23 Python
Python中Random和Math模块学习笔记
2015/05/18 Python
简单介绍Python中的readline()方法的使用
2015/05/24 Python
Python 中的lambda函数介绍
2018/10/10 Python
python 读取Linux服务器上的文件方法
2018/12/27 Python
谈谈Python中的while循环语句
2019/03/10 Python
Python面向对象程序设计类的封装与继承用法示例
2019/04/12 Python
值得收藏的10道python 面试题
2019/04/15 Python
python游戏开发之视频转彩色字符动画
2019/04/26 Python
python百行代码自制电脑端网速悬浮窗的实现
2020/05/12 Python
5行Python代码实现图像分割的步骤详解
2020/05/25 Python
pandas DataFrame运算的实现
2020/06/14 Python
用python实现名片管理系统
2020/06/18 Python
伦敦最有品味的百货:Liberty London
2016/11/12 全球购物
美国著名的女性内衣零售商:Frederick’s of Hollywood
2018/02/24 全球购物
Whistles官网:英国女装品牌
2020/08/14 全球购物
璀璨的珍珠、密钉和个性化珠宝:Lily & Roo
2021/01/21 全球购物
数控专业推荐信范文
2013/12/02 职场文书
树转促学习心得体会
2014/09/10 职场文书
秋季运动会演讲稿
2014/09/16 职场文书
分析Python list操作为什么会错误
2021/11/17 Python
php去除数组中为0的元素的实例分析
2021/11/17 PHP
多台电脑共享文件怎么设置?多台电脑共享文件操作教程
2022/04/08 数码科技