antd+vue实现动态验证循环属性表单的思路

 更新时间:2021年09月16日 17:05:02   作者:魏知非  
今天通过本文给大家分享antd+vue实现动态验证循环属性表单的思路,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

希望实现查询表单的某些属性可以循环验证必填项:

需求:

1.名称,对比项,备注必填,默认为一行,可增加多行

2.根据名称,动态请求对比项列表,名称变化时,清空该行当前选择的对比项

思路:将整个搜索分成了两个表单,分别去做验证。一个是可动态添加的循环表单form,另一个为普通表单dateForm

html

			<a-form :form="form" @keyup.enter.native='searchQuery'>
				<div class="dynamic-wrap">
					<div v-for="(item,index) in formList" :key="index">
						<a-row :gutter="24">
							<a-col :span="6">
								<a-form-item label="名称" :labelCol="{span: 7}" :wrapperCol="{span: 17}">
									<a-select placeholder='请选择名称'
									          v-decorator="[`equipment[${index}]`,{ initialValue: formList[index] ? formList[index].equipment : '', rules: [{ required: true, message: '请选择设备!' }]}]"
									          @change="(e)=>equipChange(e,index)">
										<a-select-option v-for='options in formList[index].eqpList' :key='options.name' :value='options.name'>
											{{ options.name }}
										</a-select-option>
									</a-select>
								</a-form-item>
							</a-col>
							<a-col :span="6">
								<a-form-item label="对比项" :labelCol="{span: 7}" :wrapperCol="{span: 17}">
									<a-select
										placeholder="请选择对比项"
										v-decorator="[`dataCode[${index}]`,{initialValue: formList[index] ? formList[index].dataCode : '',rules: [{ required: true, message: '请选择对比项!' }]}]">
										<a-select-option v-for='option in formList[index].dataTypeList' :key='option.name' :value='option.name'>
											{{ option.name }}
										</a-select-option>
									</a-select>
								</a-form-item>
							</a-col>
							<a-col :span="6">
								<a-form-item label="备注" :labelCol="{span: 6}" :wrapperCol="{span: 18}">
									<a-input v-decorator="[`remark[${index}]`]" placeholder="请输入备注"></a-input>
								</a-form-item>
							</a-col>
							<a-col :span="2" style="padding-left: 0px">
								<a-form-item :labelCol="{span: 0}" :wrapperCol="{span: 24}">
									<template v-if="formList.length > 1">
										<a-icon type="delete" @click="removeRow(index)"/>
									</template>
								</a-form-item>
							</a-col>
						</a-row>
					</div>
				</div>
			</a-form>
 
			<a-form :form="dateForm" inline @keyup.enter.native='searchQuery'>
				<a-form-item label='查询日期' :labelCol="{span: 8}" :wrapperCol="{span: 16}"
				             style="display: inline-block;width: 300px;">
					<a-date-picker
						style="width: 200px;"
						class='query-group-cust'
						v-decorator="['startTime', { rules: [{ required: true, message: '请选择开始时间!' }] }]"
						:disabled-date='disabledStartDate'
						format='YYYY-MM-DD'
						placeholder='请选择开始时间'
						@change='handleStart($event)'
						@openChange='handleStartOpenChange'></a-date-picker>
				</a-form-item>
				<span :style="{ display: 'inline-block', width: '24px', textAlign: 'center' }">-</span>
				<a-form-item style="display: inline-block;width: 200px;">
					<a-date-picker
						style="width: 200px;"
						class='query-group-cust'
						v-decorator="['endTime', { rules: [{ required: true, message: '请选择结束时间!' }] }]"
						:disabled-date='disabledEndDate'
						format='YYYY-MM-DD'
						placeholder='请选择结束时间'
						@change='handleEnd($event)'
						:open='endOpen'
						@openChange='handleEndOpenChange'></a-date-picker>
				</a-form-item>
				<span style="margin-left: 10px">
				        <a-button type='primary' :disabled='loading' @click='searchQuery' icon='search'>查询</a-button>
				        <a-button type='primary' @click='searchReset' icon='search' style='margin-left:10px'>重置</a-button>
				        <a-button type="primary" icon="plus" @click="addRow" style='margin-left:10px'>新增查询条件</a-button>
			        </span>
			</a-form>
			<p>查询条件为:{{searchData}}</p>

js

initForm() {
				// 首先请求设备列表,存放在eqpList中
				// 初始化form表单
				this.formList.push({
					equipment: '',
					dataCode: '',
					remark: '',
					eqpList: this.eqpList,
					dataTypeList: []
				})
			},
			// 删除一行
			handleRemove(index) {
				if (this.formList.length === 1) {
					return
				}
				this.formList.splice(index, 1)
			},
			// 新增一行
			handleAdd() {
				this.formList.push({
					equipment: '',
					dataCode: '',
					remark: '',
					eqpList: this.eqpList, // 可以根据接口动态获取,这里便于演示,直接赋值了
					dataTypeList: [],// 可以根据接口动态获取并根据设备去关联
				})
			},
			equipChange(value, index) {
				// change赋值
				this.formList[index].equipment = value;
				//同步更新 当前选择的设备对应的对比项列表
				this.handleEqpIdentity(value, index)
			},
			// 根据设备查询对应的对比项列表
			handleEqpIdentity(value, index) {
				this.dataTypeList = []; //清空dataTypeList
				this.formList[index].dataTypeList = []; // 清空当前行的 dataTypeList
				//根据新的设备名称 获取对应的对比项列表
				getAction(this.url.getDataTypeList, {equipment: value})
					.then((res) => {
						if (res.success) {
							this.dataTypeList = res.result;
							this.formList[index].dataTypeList = this.dataTypeList;
							// this.formList[index].dataCode = ''; 直接赋值为空 是无效的
							//需使用 getFieldValue, setFieldsValue
							let dataCode1Arr = this.form.getFieldValue('dataCode');
							if (dataCode1Arr.length !== 0) {
								dataCode1Arr[index] = ''
							}
							this.form.setFieldsValue({dataCode: dataCode1Arr})
						} else {
							this.$message.warning(res.message)
						}
					})
					.catch(() => {
						this.$message.error('获取失败,请重试!')
					})
			},
// 点击查询
			searchQuery() {
				// 先验证循环表单
				const {form: {validateFields}} = this
				validateFields((error, values) => {
					if (!error) {
						this.dateForm.validateFields((dateErr, dateValues) => {
							//再验证日期搜索表单
							dateValues.startTime = moment(dateValues.startTime).format('YYYY-MM-DD')
							dateValues.endTime = moment(dateValues.endTime).format('YYYY-MM-DD')
							if (!dateErr) {
								this.loading = true;
								let formData = Object.assign({}, dateValues);
								//整理成后台所需的数据结构
								// 循环表单
								let searchArr = [];
								(values[`equipment`]).forEach((item, index) => {
									const obj = {
										equipment: item,
										remark: values[`remark`][index],
										dataCode: values[`dataCode`][index]
									}
									searchArr.push(obj);
 
								})
								// 日期表单
								if (!dateValues.startTime) {
									formData.startTime = moment(new Date()).format('YYYY-MM-DD')
								}
								formData.eqpInfoParamVoList = searchArr;
								this.searchData = JSON.parse(formData)
							  //	请求接口
							}
						})
					}
				})
			},

到此这篇关于antd vue实现动态验证循环属性表单的文章就介绍到这了,更多相关antd vue动态验证循环属性表单内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 在Vue开发过程中解决和预防内存泄漏问题的方法详解

    在Vue开发过程中解决和预防内存泄漏问题的方法详解

    Vue作为一款流行的前端框架,已经在许多项目中得到广泛应用,然而,随着我们在Vue中构建更大规模的应用程序,我们可能会遇到一个严重的问题,那就是内存泄漏,因此,我们需要认识到在Vue开发过程中,内存泄漏问题的重要性,本文将给大家介绍如何解决和预防内存泄漏问题
    2023-10-10
  • uniapp 小程序和app map地图上显示多个酷炫动态的标点效果(头像后端传过来)

    uniapp 小程序和app map地图上显示多个酷炫动态的标点效果(头像后端传过来)

    这篇文章主要介绍了uniapp 小程序和app map地图上显示多个酷炫动态的标点效果(头像后端传过来),本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-09-09
  • Vue手写实现异步更新详解

    Vue手写实现异步更新详解

    这篇文章主要介绍了Vue手写实现异步更新详解,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下,希望对你的学习有所帮助
    2022-08-08
  • Vue代码整洁之去重方法整理

    Vue代码整洁之去重方法整理

    在本篇文章里小编给大家整理的是关于Vue代码整洁之去重的相关知识点内容,需要的朋友们学习下。
    2019-08-08
  • 解决vue打包后vendor.js文件过大问题

    解决vue打包后vendor.js文件过大问题

    这篇文章主要介绍了解决vue打包后vendor.js文件过大问题,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-07-07
  • vue组件开发之slider组件使用详解

    vue组件开发之slider组件使用详解

    这篇文章主要为大家详细介绍了vue组件开发之slider组件的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • 如何在Vue.JS中使用图标组件

    如何在Vue.JS中使用图标组件

    这篇文章主要介绍了如何在Vue.JS中使用图标组件,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-08-08
  • 详解mpvue scroll-view自动回弹bug解决方案

    详解mpvue scroll-view自动回弹bug解决方案

    设置了scroll-top的scroll-view组件,在组件所在vue实例data发生改变时会自动回弹到最上方,非常具有实用价值,需要的朋友可以参考下
    2018-10-10
  • vue-resource 拦截器(interceptor)的使用详解

    vue-resource 拦截器(interceptor)的使用详解

    本篇文章主要介绍了vue-resource 拦截器(interceptor)的使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • vue父子组件通讯的所有方法小结

    vue父子组件通讯的所有方法小结

    本文将介绍父组件与子组件之间传递数据的四种方法,以一个简单的小demo为例,通过实例全方位解析和代码演示,便于大家理解,需要的朋友可以参考下
    2024-07-07

最新评论