详解Vue返回值动态生成表单及提交数据的办法
作者:mqy1023 时间:2024-05-28 16:09:58
目录
主要解决的问题
一、后端返回的数据,提交到后端的数据格式如下:
二、vue前端代码如下:
总结
主要解决的问题
1、vue在循环的时候需要动态绑定不同的v-model;vue动态的表单,数据怎么绑定呢?
2、动态表单上所有name属性对应的键值对的形式提交到后端
一、后端返回的数据,提交到后端的数据格式如下:
// 后端返回的数据,根据返回类型用对应的组件
[
{
"componentType": "input",
"componentName": "username",
"required": "1", // 提交时是否要必须填写
"name": "姓名",
},
{
"componentType": "radio",
"componentName": "sex",
"required": "1",
"name": "性别",
"options": [
{
"name": "男",
"value": "0000"
},
{
"name": "女",
"value": "1111"
}
]
}
]
// 提交到服务器的数据格式
{
username: '我的姓名',
sex: '0000' // 对应”男“
}
二、vue前端代码如下:
<template>
<div class="page-container">
<div class="dynamic-content">
<div v-for="(item,idx) in infoList" :key="idx">
<input class="common-input" v-model="modelItems[idx]" v-if="item.componentType=='input'">
<van-radio-group v-model="modelItems[idx]" direction="horizontal" v-if="item.componentType=='radio'">
<van-radio :name="itemRadio.value" v-for="itemRadio in item.options" :key="itemRadio.value">
{{itemRadio.name}}
</van-radio>
</van-radio-group>
</div>
<div class="common-btn" @click="clickSubmit">提交数据</div>
</div>
</div>
</template>
<script>
import Vue from 'vue'
import { getListData } from '@/api/home'
import { RadioGroup, Radio } from 'vant'
Vue.use(Radio).use(RadioGroup)
export default {
data() {
return {
modelItems: {}, // vue在循环的时候需要动态绑定不同的v-model
infoList: []
}
},
mounted() {
this.formKeyArr = []
this.getList()
},
methods: {
getList() {
getListData()
.then((res) => {
const infoListData = res.infoList
this.infoList = infoListData
infoListData.forEach((item, index) => {
// 保存属性name和是否必填,后续提交数据用到
// { name: 'username', type: 1 }, { name: 'sex', type: 1}
this.formKeyArr.push({ name: item.componentName, type: item.required })
})
})
.catch(() => {
})
},
clickSubmit() {
const postParams = {} // 提交的数据
let isCanSubmit = true
this.formKeyArr.forEach((item, index) => {
console.log('item=', item)
if (item.type === '1' && !this.modelItems[index]) { // 所有require必须的标记符
// 请先填写完成, toast请填写完整
isCanSubmit = false
}
postParams[item['name']] = this.modelItems[index]
})
if (isCanSubmit) {
// 可以提交数据
// 可以拿到提交表单数据
// { username: '我的姓名', sex: '0000' // 对应”男“ }
console.log('postParams=', postParams)
}
}
}
}
</script>
<style lang="scss">
</style>
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注asp之家的更多内容!
来源:https://blog.csdn.net/mqy1023/article/details/122162914
标签:Vue,返回值,生成,表单
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python docx的超链接网址和链接文本操作
2021-06-05 22:10:55
![](https://img.aspxhome.com/file/2023/2/133682_0s.jpg)
Python 如何修改程序默认时区
2022-10-07 15:32:16
SQL Server 2016里的sys.dm_exec_input_buffer的问题
2024-01-14 19:06:44
ASP自动清除ACCESS数据库的日文字符
2007-11-28 17:40:00
网页设计中的层次感
2007-11-05 18:19:00
![](https://img.aspxhome.com/file/UploadPic/200711/5/2007115182428922s.png)
vee-validate vue 2.0自定义表单验证的实例
2023-07-02 17:09:33
使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件及出现问题解决方法
2021-09-17 08:53:15
![](https://img.aspxhome.com/file/2023/3/108013_0s.png)
基于python实现MUI区域滚动
2023-11-04 02:38:52
![](https://img.aspxhome.com/file/2023/6/79396_0s.png)
快速入门python学习笔记
2023-08-25 14:26:29
Flask框架学习笔记之表单基础介绍与表单提交方式
2023-02-05 09:25:44
![](https://img.aspxhome.com/file/2023/2/103542_0s.png)
Django中redis的使用方法(包括安装、配置、启动)
2022-03-09 02:15:59
![](https://img.aspxhome.com/file/2023/5/76845_0s.png)
关于Tensorflow中的tf.train.batch函数的使用
2023-11-04 19:49:54
PHP hex2bin()函数用法讲解
2023-06-06 18:51:35
![](https://img.aspxhome.com/file/2023/5/55385_0s.png)
实例演示在SQL中启用全文检索
2011-10-01 14:01:37
从错误中学习改正Go语言五个坏习惯提高编程技巧
2023-10-12 20:06:33
python常用知识梳理(必看篇)
2021-04-30 07:37:39
对python过滤器和lambda函数的用法详解
2022-04-23 06:37:47
python 按照sheet合并多个Excel的示例代码(多个sheet)
2022-07-13 05:20:20
![](https://img.aspxhome.com/file/2023/9/130619_0s.png)
Python手动实现Hough圆变换的示例代码
2023-11-03 03:12:42
![](https://img.aspxhome.com/file/2023/1/101291_0s.png)
Vue中$router与 $route的区别详解
2024-04-30 10:38:42
![](https://img.aspxhome.com/file/2023/7/130127_0s.png)