uniapp + uview Select选择器(u-action-sheet)数据绑定 value,展示 text ------之------ 单个Select选择器


前言

在使用 uniapp uview 的 Select 选择器的组件时,业务逻辑需要实现绑定的是某个数据的 value 如(0,1,2),但是在页面中展示的需要是选项的文字,通过 uview 封装好的 u-action-sheet 组件,来实现


一、uniapp + uview Select选择器(u-action-sheet)数据绑定列表的 index,展示 text ------之------ 单个Select选择器

uniapp + uview Select选择器(u-action-sheet)数据绑定列表的 index,展示 text ------之------ 单个Select选择器

二、uniapp + uview Select选择器(u-action-sheet)数据绑定 value,展示 text ------之------ 单个Select选择器

1. 实现功能

页面打开传递的学生对象有 name,education 两个参数
传过来的数据是 student:{name:‘yuzu’,education:3}
页面中展示 学历为 '专科 ’ 这里 education 的 value 值和列表中的 index 无关
当选择学历后,提交表单,提交的数据的格式也是 student:{name:‘yuzu’,education:3}

2. 代码

html 代码

<template>
	<view>
		<view>
			<u-form :model="student" ref="uForm">
				<u-form-item :leftIconStyle="{color: '#888', fontSize: '32rpx'}" left-icon="account" label-width="120"
					label="姓名" prop="name">
					<u-input placeholder="请输入姓名" v-model="student.name" type="text"></u-input>
				</u-form-item>

				<u-form-item label="学历" label-width="150">
					<u-input type="select" :select-open="educationSelectShow" v-model="getStudentEducationText"
						placeholder="请选择学历" @click="educationSelectShow = true"></u-input>
				</u-form-item>
				<u-action-sheet :list="educationSelectList" v-model="educationSelectShow"
					@click="educationSelectCallback">
				</u-action-sheet>
			</u-form>
			<u-button size="default" type='primary' @click="submitForm">提交</u-button>
		</view>
	</view>
</template>

js 代码

<script>
	export default {
		data() {
			return {
				student: {
					name: '',
					education: '',
				},
				educationSelectShow: false,
				educationSelectList: [{
						value: 5,
						text: '专科'
					},
					{
						value: 9,
						text: '本科'
					},
					{
						value: 7,
						text: '研究生'
					},
				]
			}
		},
		onShow() {
			this.init();
		},
		computed: {
			getStudentEducationText:{
				set() {
					
				},
				get() {
					let eduStr=''
					console.log(this.student.education)
					this.educationSelectList.forEach((a,b)=>{
						if(a.value==this.student.education){
							console.log(a.text)
							eduStr = a.text;
						}
					})
					return eduStr;
				}
			}
		},
		methods: {
			init() {
				this.student = {
					name: 'yuzu',
					education: 7,
				}
			},
			educationSelectCallback(index) {
				uni.hideKeyboard();
				console.log(index);
				this.student.education = this.educationSelectList[index].value;
				console.log(this.student.education)
			},
			submitForm() {
				console.log(this.student) 
			}
		}
	}
</script>

<style>

</style>

3. 最终效果

在这里插入图片描述

三、uniapp + uview Select选择器(u-action-sheet)数据绑定 value,展示 text ------之------ 两个联动的Select选择器

uniapp + uview Select选择器(u-action-sheet)数据绑定 value,展示 text ------之------ 两个联动的Select选择器

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值