微信小程序开发交流qq群   173683895

   承接微信小程序开发。扫码加微信。

需求: 通过JS条件判断,满足条件就弹出Picker给用户选择一个数组里面的数据。

有些朋友可能会有疑问:

1.为什么要使用自定义的Picker组件,不是有原生的组件吗?

回答:因为小程序的Picker组件是需要用户手动触发的。并不满足上述需求。

2.这个需求我知道在小程序文档里面有一个API可以满足这个需求。wx.showActionSheet(Object object)//显示操作菜单

回答:wx.showActionSheet()有长度的限制,最多只能6个选项。

那么为了满足这些条件,我们需要自己写了,我把这个选择器封装成了一个组件,可以方便的调用

目录:

1.自定义组件效果图

2.自定义组件源码1:

3.引用组件

4.自定义组件源码2

1.效果图1:

效果图2:

点击确定以后:

2.自定义组件源码1:

// my-picker.js
Component({/*** 组件的属性列表*/properties: {//配置页面传过来的值,key值要一一对应'list': {type: Array, //必填,目前接受的类型包括:String,Number,Boolean, Object, Array, null(表示任意类型)value: [] //可选,默认值,如果页面没传值过来就会使用默认值 },'showDialog': {type: Boolean, //必填,目前接受的类型包括:String,Number,Boolean, Object, Array, null(表示任意类型)value: false //可选,默认值,如果页面没传值过来就会使用默认值 }},data: {},/*** 组件的方法列表*/methods: {radioChange: function (e) {let radioChange_value = e.currentTarget.dataset.data;let picker_id = e.currentTarget.dataset.idthis.setData({radioChange_value, picker_id})// console.log('value值为:', radioChange_value, 'picker_id值为:', picker_id)},toggleDialog() {this.setData({showDialog: !this.data.showDialog});},freeBack: function () {var that = thisthat.setData({showDialog: !this.data.showDialog})console.log('his.data.radioChange_value', this.data.radioChange_value)this.triggerEvent('myPickerChange', this.data.radioChange_value);},freetoBack: function () {var that = thisthat.setData({showDialog: !this.data.showDialog,value: 'show',})},pickerCancel() {// 滚动选择器 - 取消this.pickerHandler()this.triggerEvent('cancel', arr, {})}}
})

wxml

<view class="free-dialog {{ showDialog ? 'free-dialog--show' : '' }}"><view class="free-dialog__mask" bindtap="toggleDialog" /><view class="free-dialog__container"><view style="padding: 5% 5% 15%;"><view bindtap='freetoBack' class="free-button free-dialog-reset">取消</view><view bindtap='freeBack' class="free-button free-dialog-submit">确定</view><radio-group class='free-radios'><label class="free-radio" wx:for="{{list}}" wx:key="" catchtap="radioChange" data-data='{{item}}' data-id="{{index}}" style="{{index==picker_id?'background:#48c23d;color:#fff;':'background:#fff;color:#000;'}}"><label class="free-text">{{item}}</label></label></radio-group></view></view>
</view>

wxss

.free-dialog__mask {position: fixed;top: 0;left: 0;right: 0;bottom: 0;z-index: 10;background: rgba(0, 0, 0, 0.7);display: none;
}
.free-dialog__container {position: fixed;left: 0;bottom: 0;width: 750rpx;background: white;transform: translateY(150%);transition: all 0.4s ease;z-index: 11;
}
.free-dialog--show .free-dialog__container {transform: translateY(0);
}
.free-dialog--show .free-dialog__mask {display: block;
}
/*模态框中的内容*/
.free-button{display: inline-block;width:100rpx;text-align: center;font-size:20px;color:#707070;margin-bottom:20px;
}
.free-dialog-submit{float: right;color:#48c23d;
}
radio-group{margin:10rpx 0rpx;
}
radio-group>label{width:48%;display: inline-block;border:1px solid #ddd;padding:10px 0px;margin:0px 2px 2px;
}radio-group label radio{width:100%;z-index: 3;display: none;
}
.checked{background:#48c23d;color:#fff;
}
radio-group label .free-text{width:100%;text-align: center;display: inline-block;
}

json

{"component": true,"usingComponents": {}
}

这里面包含了页面给自定义组件传递数据,自定义组件给页面传递数据,自定义组件的事件。

3.引用组件:

<myPicker  wx:if='{{configType==1&&address_list!=[]}}' bindmyPickerChange="myPickerChange"showDialog='{{myPicker_show}}' list='{{address_list}}'>
</myPicker>

js

Page({data: {myPicker_show:false,address_list:['1','2','3','4','5','6','7'],  },// 自定义组件选择的结果myPickerChange(e) {console.log('自定义组件选择的结果',e.detail)this.setData({address: e.detail})}
})

json

{"navigationBarTitleText": "资料填写","usingComponents": {"myPicker": "../../../components/myPicke/myPicke"
}
}

4.自定义组件源码2

// my-picker.js
Component({/*** 组件的属性列表*/properties: {//配置页面传过来的值,key值要一一对应'list': {type: Array, //必填,目前接受的类型包括:String,Number,Boolean, Object, Array, null(表示任意类型)value: [] //可选,默认值,如果页面没传值过来就会使用默认值 },'showDialog': {type: Boolean, //必填,目前接受的类型包括:String,Number,Boolean, Object, Array, null(表示任意类型)value: false //可选,默认值,如果页面没传值过来就会使用默认值 }},data: {},/*** 组件的方法列表*/methods: {radioChange: function (e) {let radioChange_value = e.currentTarget.dataset.data;let picker_id = e.currentTarget.dataset.idthis.setData({radioChange_value, picker_id})// console.log('value值为:', radioChange_value, 'picker_id值为:', picker_id)},myPickerHide() {this.triggerEvent('myPickerHide', '');},freeBack: function () {var that = thisthis.triggerEvent('myPickerHide', '');console.log('his.data.radioChange_value', this.data.radioChange_value)this.triggerEvent('myPickerChange', this.data.radioChange_value);},}
})

wxml

<view class="free-dialog {{ showDialog ? 'free-dialog--show' : '' }}"><view class="free-dialog__mask" bindtap="myPickerHide" /><view class="free-dialog__container"><view class='title_line' style=""><view bindtap='myPickerHide' class="free-button free-dialog-reset">取消</view><view bindtap='freeBack' class="free-button free-dialog-submit">确定</view></view><scroll-view class='free-radios' scroll-y='true'><view class="free-radio" wx:for="{{list}}" wx:key="" catchtap="radioChange" data-data='{{item}}' data-id="{{index}}" style="{{index==picker_id?'color:#48c23d;':'color:#999;'}}"><view class="free-text">{{item}}</view></view></scroll-view></view>
</view>

wxml

.free-dialog__mask {position: fixed;top: 0;left: 0;right: 0;bottom: 0;z-index: 9998;background: rgba(0, 0, 0, 0.7);display: none;
}
.title_line{padding-top: 20rpx;border-bottom: 1px solid #f2f2f2;margin-bottom: 30rpx;
}
.free-dialog__container {position: fixed;left: 0;bottom: 0;width: 750rpx;background: white;transform: translateY(150%);transition: all 0.4s ease;z-index: 9999;
}
.free-dialog--show .free-dialog__container {transform: translateY(0);
}
.free-dialog--show .free-dialog__mask {display: block;
}
/*模态框中的内容*/
.free-button{display: inline-block;width:100rpx;text-align: center;font-size:36rpx;color:#707070;margin-bottom:10px;
}
.free-radios{height: 385rpx;padding-bottom: 40rpx;
}
.free-radio{padding: 10rpx;text-align: center;
}
.free-dialog-submit{float: right;color:#48c23d;
}
radio-group{margin:10rpx 0rpx;
}
radio-group>label{width:100%;display: inline-block;/* border:1px solid #ddd; */padding:10px 0px;margin:0px 2px 2px;
}.checked{background:#48c23d;color:#fff;
}
radio-group label .free-text{width:100%;margin: 20rpx;text-align: center;font-size:28rpx;border-top: 1px solid #f2f2f2;border-bottom: 1px solid #f2f2f2;display: inline-block;
}

json

{"component": true,"usingComponents": {}
}

微信小程序自定义组件之Picker组件相关推荐

  1. 微信小程序自定义弹出框组件,模拟wx.showModal

    微信小程序开发交流qq群   173683895 效果图: 代码 wxml <view wx:if='{{showModal}}'><view class='mask_layer' ...

  2. 微信小程序 - 自定义头部导航栏组件(详解) + iphoneX以上遮挡小黑条适配问题

    1. 导航栏计算: 导航栏总高度=状态栏高度+胶囊高度+(胶囊距顶距离-胶囊高度)*2 navHeight = statusBarHeight + menuButtonObject.height + ...

  3. 微信小程序多项选择器_微信小程序——多列选择器picker组件

    一.效果图 二.picker属性 一个简单地多列选择器只要给picker组件加属性mode="multiSelector"即可,绑定数据时使用range来绑定一个数组作为显示内容, ...

  4. 微信小程序自定义状态栏navigationBar样式组件,适配所有机型

    一.在app.json设置navigationStyle自定义 "window": {"navigationStyle": "custom" ...

  5. 微信小程序自定义图片上传组件

    实现功能: 点击上传按钮可上传图片,并且可定义最多可上传的图片数量.限制图片的大小.类型和来源. 点击图片可预览图片. 点击删除按钮可删除图片. <!-- image-upload.wxml - ...

  6. 微信小程序自定义select下拉选择组件

    微信小程序自定义select下拉选择组件 微信小程序原生开发中,常用到的是从底部弹起的滚动选择器(picker),而有些项目需要用到下拉选择,话不多说,下面就可以把下拉选择封装成一个自定义组件: 1. ...

  7. 微信小程序自定义组件方案

    前言:小程序已于11月初开放了小程序组件功能,但事件方面还不是很完善,有的组件暂时可能还是要用其他方式来实现,这里简单记录下开发小程序自定义组件的要点. 在小程序官方开发组件开发功能之前,自定义组件的 ...

  8. 微信小程序自定义组件,提示组件

    微信小程序自定义组件,这里列举了一个常用的提示自定义组件,调用自定义组件中的方法和字段.仅供参考和学习. 编写组件: 在根目录下添加"components"目录,然后像添加Page ...

  9. 微信小程序自定义组件(二)

    微信小程序自定义组件 ps 由于作业部落貌似出了点问题,耽误了点时间,找了一个stackedit.io准备写.无奈,这是要自己建编辑器的节奏啊.没有一个能靠的注 为何存在组件 组件间的关系 使用rel ...

  10. 微信小程序下拉框插件_微信小程序自定义select下拉选项框组件的实现代码_清玖_前端开发者...

    知识点:组件,animation,获取当前点击元素的索引与内容 微信小程序中没有select下拉选项框,所以只有自定义.自定义的话,可以选择模板的方式,也可以选择组件的方式来创建. 这次我选择了组件, ...

最新文章

  1. C语言试卷终稿,C语言试卷终稿B1.doc
  2. Window 2000 网络操作命令全释
  3. mysql无法解析方程式索引
  4. 交叉编译_Golang交叉编译
  5. 02-requests模块的概述
  6. Qt通过ODBC读取excel文件
  7. 【会议】2008-10-27
  8. 豪郑3000亿、发布6款车,许家印「速成」马斯克?
  9. datatable转list方法(有借鉴到他人)
  10. 使用TortoiseSVN将文件回退到某个版本
  11. 详解ASR语音转写场景下的应用
  12. 马成荣版计算机应用基础 教案,计算机基础教案.doc
  13. PS Adobe软件使用 快捷键
  14. 计算机保研面试英文,计算机保研面试英文自我介绍范文
  15. CVPR 2022 | 谷歌提出mip-NeRF 360:全景NeRF越来越丝滑!
  16. 车联网TBOX国六OBD排放终端远程在线监控系统
  17. 660 - 循环基础-利息计算
  18. C2. Pokémon Army (hard version)(贪心分治)
  19. unipush+java+个推实现app消息推送
  20. UE中渲染自发光通道的折衷方案

热门文章

  1. OpenCV中图像以Mat类型保存时各通道数据在内存中的组织形式及python代码访问各通道数据的简要方式...
  2. Error 0162 - Setup data integrity check failure after updating BIOS via Thinkvantage
  3. delphi自定义事件处理
  4. ExecutorService 的理解与使用
  5. Android开发——布局性能优化的一些技巧(一)
  6. 新公司研发能力低下,何去何从?
  7. PHP数组实际占用内存大小的分析
  8. 正则表达式简介及在C++11中的简单使用
  9. Windows 7 64位机上OpenCV2.4.3的编译、安装与配置
  10. 使用纯C++实现SQL Server2005 数据库读写操作详细步骤