动态表单提交:子组件通过index区分,和父页面内容一起动态提交

在这里插入图片描述
父页面动态表单


<template>
	<view>
		<form @submit="formSubmit" class="grace-form">
			<view v-for="(item,index) in formData.students" :key="index">
			
				<view class="flex-start line-goods">
					<view>损耗数量:</view>
					<view>
						<input v-model="item.bad_count" type="number" class="inputcs" />
					</view>
				</view>

				<view class="flex-start line-goods">
					<view class="goods-tit">归还照片:</view>
					<view>
						<!--upimage是上传图片子组件,同过index判断不同的值。-->
						<upimage :tourid="tour_id" @files="getFiles" :index="index">
						</upimage>
					</view>
				</view>
			</view>
			<view class=" line-btn" @tap="submit">提交</view>
		</form>
	</view>
</template>
<script>
	import upimage from '../../../graceUI/components/backgoods.vue'
	export default {
		components: {
			upimage
		},
		data() {
			return {
				used_give: null,
				tour_id: null,
				// formData 用于记录全部表单数据
				formData: {
					students: []
				},
				filesa: null
			}
		},
		onLoad(option) {
			this.tour_id = option.tour_id
			this.getmaterialList()
		},
		methods: {
			//得到子组件返回的值+index
			getFiles(vals) {
				const {v,index} = vals
				if (v) {
					this.formData.students[index].file = v;
				}
			},
			getmaterialList() {
				this.$http.get('/XXXXXXXXXXX', {
					tour_id: this.tour_id
				}).then(res => {
					if (res.data.result === 1) {
						let server = res.data.data
						server.forEach(item => {
							this.formData.students.push({
								goods_id: item.goods_id,
								goods_name: item.goods_name,
								goods_count: item.used_count,
								goods_src: this.$url + item.goods_src,
								file: ''
							})
						})

					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					}
				})
			},
			submit: function() {
				// 请利用 v-model 将表单元素数据全部记录在 formData 变量内
				// 提交时即可获取全部表单数据 
				console.log(this.formData);
			},
		}
	}
</script>

子页面

//从父页面接收过来值
props: ['tourid', 'index'],
methods: {
	submit() {
		//获取图片路径,拆分上传图片的路径拼接
		let imgpat = this.$refs.uUpload.lists;
		let str = ''
		for (var i = 0; i < imgpat.length; i++) {
			str += imgpat[i].response.data + ",";
		}
		str = str.slice(0, str.length - 1);
		this.files.push(this.$url + str);
		//通过$emit传给父页面
		this.$emit('files', {
			index: this.index,
			v: this.files
		});
		//关闭图片上传
		this.$refs.popup.close();
	},
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
组件表单未通过校验时,组件可以通过监听组件的校验状态来禁用提交按钮或者显示错误提示信息,以避免不合法的数据提交到后台。一种常用的方法是在组件中定义一个事件,当组件表单校验状态发生改变时,触发该事件并传递校验状态给组件组件通过监听该事件来更新提交按钮的状态或者显示错误提示信息。具体实现方法可以参考以下示例代码: 组件: ```html <template> <el-form ref="form" :model="formData" :rules="rules" @change="handleChange"> <!-- 表单项 --> </el-form> </template> <script> export default { data() { return { formData: {}, // 表单数据 rules: {}, // 表单校验规则 isFormValid: false, // 表单校验状态 }; }, methods: { handleChange() { // 表单校验状态发生改变,触发事件通知组件 this.isFormValid = this.$refs.form.validate(); this.$emit('form-change', this.isFormValid); }, }, }; </script> ``` 组件: ```html <template> <child-form @form-change="handleFormChange"></child-form> <el-button :disabled="!isFormValid" @click="handleSubmit">提交</el-button> </template> <script> import ChildForm from './ChildForm.vue'; export default { components: { ChildForm, }, data() { return { isFormValid: false, // 组件表单校验状态 }; }, methods: { handleFormChange(isValid) { // 组件表单校验状态发生改变,更新组件状态 this.isFormValid = isValid; }, handleSubmit() { // 组件提交逻辑 if (this.isFormValid) { // 提交表单数据 } else { // 显示错误提示信息 } }, }, }; </script> ``` 在上述示例中,组件通过 `handleChange` 方法监听表单项的变化,当表单校验状态发生改变时,触发 `form-change` 事件并传递校验状态给组件组件通过监听 `form-change` 事件来更新 `isFormValid` 的状态,从而禁用或启用提交按钮。在提交表单时,组件会检查表单的校验状态,如果校验通过则提交表单数据,否则显示错误提示信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值