uniapp的底部滚动选择器picker

官方:picker | 微信开放文档​​​​​​

案例:性别的选择器

在template中:

<view class="li">
				<view class="name"><text class="red">*</text>性别</view>
				<picker v-if="sexList.length > 0" class="input" mode="selector"
					@change="bindPickerChange($event, sexList, 'sex', 'sexIndex')" :value="sexIndex" :range="sexList"
					range-key='name'>
					<view class="uni-input">{{decodeURI(sexList[sexIndex].name)}}</view>
				</picker>
			</view>

在data中:

return {
				
				sexList: [],
				sexIndex: 0,}

初始化并获取码表:当然,没有码表你就想象一个对象值。反正给range里面的。用来显示性别男,性别女的选项框。官方文档可以看range这个参数,就知道了。

sexList为给事件传了个固定值,自行设置

initDict() {
				
				this.getAAA('MABIAOZHI', 'sexList')}

调用接口获取码表值:

这边的this【list】是指,getAAA内传的值是什么。举个栗子:getAAA(zhege,nage),那么this【nage】==nage

getAAA(type, list) {
				this.###(这边###为接口).(这边是接口方法)([type]).then(res => {
					console.log(res)
                    //这边data为未选择时默认显示的值
					const data = [{
						code: '',
						name: '请选择'
					}]
                    //把码表值的数组添加到this[list]数组内
					this[list] = data.concat(res.data[type])
				})
			},

触发选择的事件:

bindPickerChange(e, list, files, indexFiles, ids) {
				console.log('---', e, list, files, indexFiles)
				const index = e.detail.value
				if (ids) {
					this.createInfo[files] = list[index].id || ''
				} else {
					this.createInfo[files] = list[index].code
				}
				this[indexFiles] = index
			},

案例日期选择器:

<view class="li">
				<view class="name"><text class="red">*</text>生日</view>
				<picker mode="date" class="input" :value="createInfo.birthday" fields="day" :start="startDate"
					:end="endDate" @change="bindDateChange($event, 'birthday')">
					<text class="uni-input">{{createInfo.birthday || '请选择'}}</text>
				</picker>
			</view>

createInfo对象内有值birthday:

// 时间的绑定
			bindDateChange(e, files) {
				this.createInfo[files] = e.detail.value
			},

关于:end和:start

computed: {
			startDate() {
				const data = this.getDate('start')
				return data
			},
			endDate() {
				const data = this.getDate('end')
				return data
			}
		},

返回计算过的值:

getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 80;
					month = '01'
				} else if (type === 'end') {
					year = year;
					month = month > 9 ? month : '0' + month;
				}
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}`;
			},

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值