uniapp的动态表单实现

目录

1.说明

2.示例

3.总结


1.说明

①在 formData 中定义个数组变量用来接受同一个字段的多个结果。

复制代码
dynamicFormData: {
	email: '',
	// domains 字段下会有多个结果
	domains: []
}

②使用 uni-forms-itemrules 属性定义单个表单域的校验规则。

复制代码
<uni-forms-item :label="item.label+' '+index" required
	:rules="[{'required': true,errorMessage: '域名项必填'}]" :key="item.id">
	...
</uni-forms-item>

name 需要动态指定,动态表单推荐使用 Array 类型,内容从左到右为绑定值的调用链。['domains',index,'value'] 等同于 dynamicFormData.domains[index].value

复制代码
<uni-forms-item 
	required
	:label="item.label+' '+index" 
	:name="['domains',index,'value']"
	:rules="[{'required': true,errorMessage: '域名项必填'}]" 
	:key="item.id"
	>
	...
</uni-forms-item>

④需要绑定值的组件的 v-model 也需要动态指定 dynamicFormData.domains[index].value

复制代码
<uni-forms-item 
	required
	:label="item.label+' '+index" 
	:name="['domains',index,'value']"
	:rules="[{'required': true,errorMessage: '域名项必填'}]" 
	:key="item.id"
	>
	<uni-easyinput v-model="dynamicFormData.domains[index].value" placeholder="请输入域名" />
</uni-forms-item>

2.示例

html层:

复制代码
			<div v-for="(item,index) in formData.parts">
				<uni-forms-item class="is-direction-top" :label="index == 0?'参与人':''" :required="index == 0"
					:rules="[{'required': true,errorMessage: '请输入参与人'}]" :key="item.id" :name="['parts',index,'value']">
					<template v-slot:label v-if="index > 0">
						<div></div>
					</template>
					<view class="dmc">
						<uni-easyinput v-model="formData.parts[index].value" placeholder="请输入参与人" />
						<uni-icons class="dmc-btn" v-if="!index" @click="addParts" size="30" type="plus"
							color="#666"></uni-icons>
						<uni-icons class="dmc-btn" v-if="index" @click="delParts(index)" size="30" type="minus"
							color="#e43d33"></uni-icons>
					</view>
				</uni-forms-item>
			</div>
			<div v-for="(item,index) in formData.workTime">
				<uni-forms-item class="is-direction-top" :label="index == 0?'工作时间':''" :required="index == 0"
					:rules="[{'required': true,errorMessage: '请输入工作时间'}]" :key="item.id"
					:name="['workTime',index,'value']">
					<template v-slot:label v-if="index > 0">
						<div></div>
					</template>
					<view class="dmc">
						<uni-datetime-picker type="datetimerange" v-model="formData.workTime[index].value" hide-second :clear-icon="false"
							placeholder="请输入工作时间"  />
						<uni-icons class="dmc-btn" v-if="!index" @click="addWorkTime" size="30" type="plus"
							color="#666"></uni-icons>
						<uni-icons class="dmc-btn" v-if="index" @click="delWorkTime(index)" size="30" type="minus"
							color="#e43d33"></uni-icons>
					</view>
				</uni-forms-item>
			</div>

js层:

复制代码
	const resetForm = () => {
		return {
			bsNo: stationInfo.bsNo,
			bsName: stationInfo.deptNm,
			fieldNo: '',
			fieldNm: '',
			cropKey: '',
			cropValue: '',
			mediNm: '',
			mediAcreage: '',
			saKey: '',
			saValue: '',
			mediPerMu: '',
			dosageMediKey: '',
			dosageMediValue: '',
			mediTotalMu: '',
			mediContent: '',
			mediModeKey: '',
			mediModeValue: '',
			participants: '',
			parts: [{ value: getUser().userName, id: Date.now() }],
			workTime: [{ value: [moment.now(), moment.now()], id: Date.now() }],
			timeList: [] as any,
			tempCount: NaN,
			mediType: 6
		}
	};
	// 初始表单数据设置
	let formData = ref(resetForm());

3.总结

当某个字段为动态时,要用数组来存放多个值;要注意校验规则的写法。

我使用的是包含id及value的对象结构用来存在动态表单的数据。

相关推荐
iOS阿玮20 小时前
“死了么”App荣登付费榜第一名!
uni-app·app·apple
wendycwb1 天前
uni-app 在真机中canvas绘制的元素悬浮,内容不随父组件滚动问题
uni-app
frontend_frank1 天前
脱离 Electron autoUpdater:uni-app跨端更新:Windows+Android统一实现方案
android·前端·javascript·electron·uni-app
三天不学习1 天前
UniApp三端实时通信实战:SignalR在H5、APP、小程序的差异与实现
微信小程序·uni-app·signalr
念你那丝微笑1 天前
uView Plus + Vue3 + TypeScript + UniApp 正确引入 UnoCSS(避坑版)
vue.js·typescript·uni-app
念你那丝微笑1 天前
vue3+ts在uniapp项目中实现自动导入 ref 和 reactive
vue.js·typescript·uni-app
游戏开发爱好者81 天前
如何使用 AppUploader 提交上传 iOS 应用
android·ios·小程序·https·uni-app·iphone·webview
iOS阿玮2 天前
AppStore卡审依旧存在,预计下周将逐渐恢复常态!
uni-app·app·apple
郑州光合科技余经理2 天前
开发实战:海外版同城o2o生活服务平台核心模块设计
开发语言·git·python·架构·uni-app·生活·智慧城市
行走的陀螺仪2 天前
在UniApp H5中,实现路由栈的持久化
前端·javascript·uni-app·路由持久化·路由缓存策略