Bootstrap

微信小程序--picker组件

index.js

// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
})

index.wxml

<!--index.wxml-->
<scroll-view class="scrollarea" scroll-y type="list">
  <view class="container">
    <view class="userinfo">
      <block wx:if="{{canIUseNicknameComp && !hasUserInfo}}">
        <button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar">
          <image class="avatar" src="{{userInfo.avatarUrl}}"></image>
        </button>
        <view class="nickname-wrapper">
          <text class="nickname-label">昵称</text>
          <input type="nickname" class="nickname-input" placeholder="请输入昵称" bind:change="onInputChange" />
        </view>
      </block>
      <block wx:elif="{{!hasUserInfo}}">
        <button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button>
        <view wx:else> 请使用2.10.4及以上版本基础库 </view>
      </block>
      <block wx:else>
        <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
        <text class="userinfo-nickname">{{userInfo.nickName}}</text>
      </block>
    </view>
    <view class="usermotto">
      <text class="user-motto">{{motto}}</text>
    </view>
  </view>
</scroll-view>

index.wxss

/**index.wxss**/
page {
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.scrollarea {
  flex: 1;
  overflow-y: hidden;
}

.userinfo {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #aaa;
  width: 80%;
}

.userinfo-avatar {
  overflow: hidden;
  width: 128rpx;
  height: 128rpx;
  margin: 20rpx;
  border-radius: 50%;
}

.usermotto {
  margin-top: 200px;
}

.avatar-wrapper {
  padding: 0;
  width: 56px !important;
  border-radius: 8px;
  margin-top: 40px;
  margin-bottom: 40px;
}

.avatar {
  display: block;
  width: 56px;
  height: 56px;
}

.nickname-wrapper {
  display: flex;
  width: 100%;
  padding: 16px;
  box-sizing: border-box;
  border-top: .5px solid rgba(0, 0, 0, 0.1);
  border-bottom: .5px solid rgba(0, 0, 0, 0.1);
  color: black;
}

.nickname-label {
  width: 105px;
}

.nickname-input {
  flex: 1;
}

picker-study.js

Page({
	data: {
		array: ['美国','中国','巴西','日本'],
		objectArray: [
			{
				id: 0,
				name: '美国'
			},
			{
				id: 1,
				name: '中国'
			},
			{
				id: 2,
				name: '巴西'
			},
			{
				id: 3,
				name: '日本'
			}
		],
		index: 0,
		multiArray: [['无脊椎动物','脊椎动物'],['扁形动物','线形动物','环节动物','软体动物','节肢动物'],['猪肉绦虫','吸血虫']],
		objectMultiArray:[
			[
				{
					id: 0,
					name: '无脊椎动物'
				},
				{
					id: 1,
					name: '脊椎动物'
				},
			],[	
				{
					id: 0,
					name: '扁形动物'
				},
				{
					id: 1,
					name: '线形动物'
				},
				{
					id: 2,
					name: '环节动物'
				},
				{
					id: 3,
					name: '软体动物'
				},
				{
					id:3,
					name:'节肢动物'
				}
			],[	
				{
					id: 0,
					name: '猪肉绦虫'
				},
				{
					id: 1,
					name: '吸血虫'
				}
			]
		],
		multiIndex: [0,0,0],
		date: '2016-09-01',
		time: '12:01',
		region:['广东省','广州市','海珠区'],
		customItem:'全部'
	},
	bindPickerChange: function(e) {
		console.log('picker发送选择改变,携带值为', e.detail.value)
		this.setData({
			index: e.detail.value
		})
	},
	bindMultiPickerChange: function(e) {
		console.log('picker发送选择改变,携带值为', e.detail.value)
		this.setData({
			multiIndex: e.detail.value
		})
	},
	bindMultiPickerColumnChange:function(e) {
		console.log('修改的列为',e.detail.column, ',值为',e.detail.value);
		var data = {
			multiArray: this.data.multiArray,
			multiIndex: this.data.multiIndex
		};
		data.multiIndex[e.detail.column] = e.detail.value;
		switch (e.detail.column) {
			case 0:
				switch (data.multiIndex[0]) {
					case 0:
						data.multiArray[1] = ['扁形动物','线形动物','环节动物','软体动物','节肢动物'];
						data.multiArray[2] = ['猪肉绦虫','吸血虫'];
						break;
					case 1:
						data.multiArray[1] = ['鱼','两栖动物','爬行动物'];
						data.multiArray[2] = ['鲫鱼','带鱼'];
						break;
				}
				data.multiArray[1] = 0;
				data.multiIndex[2] = 0;
				break;
		  case 1:
				switch (data.multiIndex[0]) {
					case 0:
						switch (data.multiIndex[1]) {
							case 0:
								data.multiArray[2] = ['猪肉绦虫','吸血虫'];
								break;
							case 1:
								data.multiArray[2] = ['蛔虫'];
								break;
							case 2:
								data.multiArray[2] = ['蚂蚁','蚂蟥'];
								break;
							case 3:
								data.multiArray[2] = ['河蚌','蜗牛','小鱼'];
								break;
							case 4:
								data.multiArray[2] = ['昆虫','甲科动物','蛛形动物','多足动物'];
								break;
						}
						break;
					case 1:
						switch (data.multiIndex[1]) {
							case 0:
								data.multiArray[2] = ['鲫鱼','带鱼'];
								break;
							case 1:
								data.multiArray[2] = ['青蛙','娃娃鱼'];
								break;
							case 2:
								data.multiArray[2] = ['蜥蜴','龟','壁虎'];
								break;
						}
						break;
				}
				data.multiIndex[2] = 0;
				console.log(data.multiIndex);
				break;
		}
		this.setData(data);
	},
	bindDateChange:function(e) {
		console.log('picker发送选择改变,携带值为', e.detail.value)
		this.setData({
			date:e.detail.value
		})
	},
	bindTimeChange:function(e) {
		console.log('picker发送选择改变,携带值为', e.detail.value)
		this.setData({
			time:e.detail.value
		})
	},
	bindRegionChange:function(e) {
		console.log('picker发送选择改变,携带值为', e.detail.value)
		this.setData({
			region:e.detail.value
		})
	},
})

picker-study.wxml

<view class="section">
  <view class="section_title">普通选择器</view>
	<picker bindchange="bindPickerChange" value="{{index}}" range="{{array}}">
	  <view class="picker">
		  当前选择: {{array[index]}}
		</view>
	</picker>
</view>
<view class="section">
  <view class="section_title">多列选择器</view>
	<picker mode="multiSelector" bindchange="bindMultiPickerChange" bindcolumnchange="bindMultiPickerColumnChange" value="{{multiIndex}}" range="{{multiArray}}">
	  <view class="picker">
		  当前选择: {{multiArray[0] [multiIndex[0]]}}, {{multiArray[1] [multiIndex[1]]}},{{multiArray[2] [multiIndex[2]]}}
		</view>
	</picker>
</view>
<view class="section">
  <view class="section_title">时间选择器</view>
	<picker mode="time" value="{{time}}" start="09:01" end="21:01" bindchange="bindTimeChange">
	  <view class="picker">
		  当前选择: {{time}}
		</view>
	</picker>
</view>
<view class="section">
  <view class="section_title">日期选择器</view>
	<picker mode="date" value="{{date}}" start="2015-09-01" end="2017-09-01" bindchange="bindDateChange">
	  <view class="picker">
		  当前选择: {{date}}
		</view>
	</picker>
</view>
<view class="section">
  <view class="section_title">市省区选择器</view>
	<picker mode="region" bindchange="bindRegionChange" value="{{region}}" custom-item="{{customItem}}">
	  <view class="picker">
		  当前选择: {{region[0]}},{{region[1]}},{{region[2]}}
		</view>
	</picker>
</view>

picker-study.wxss

/* pages/picker-study/picker-study.wxss */
@import "../weui.wxss";
.picker{
	padding: 13px;
	background-color: #FFFFFF;
}
.section{
	margin-bottom: 40px;
}
.section_title{
	margin-bottom: 8px;
	padding-left: 15px;
	padding-right: 15px;
}

;