詳解Vue返回值動(dòng)態(tài)生成表單及提交數(shù)據(jù)的辦法
主要解決的問(wèn)題
1、vue在循環(huán)的時(shí)候需要?jiǎng)討B(tài)綁定不同的v-model;vue動(dòng)態(tài)的表單,數(shù)據(jù)怎么綁定呢?
2、動(dòng)態(tài)表單上所有name屬性對(duì)應(yīng)的鍵值對(duì)的形式提交到后端
一、后端返回的數(shù)據(jù),提交到后端的數(shù)據(jù)格式如下:
// 后端返回的數(shù)據(jù),根據(jù)返回類型用對(duì)應(yīng)的組件
[
{
"componentType": "input",
"componentName": "username",
"required": "1", // 提交時(shí)是否要必須填寫(xiě)
"name": "姓名",
},
{
"componentType": "radio",
"componentName": "sex",
"required": "1",
"name": "性別",
"options": [
{
"name": "男",
"value": "0000"
},
{
"name": "女",
"value": "1111"
}
]
}
]
// 提交到服務(wù)器的數(shù)據(jù)格式
{
username: '我的姓名',
sex: '0000' // 對(duì)應(yīng)”男“
}
二、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">提交數(shù)據(jù)</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在循環(huán)的時(shí)候需要?jiǎng)討B(tài)綁定不同的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和是否必填,后續(xù)提交數(shù)據(jù)用到
// { name: 'username', type: 1 }, { name: 'sex', type: 1}
this.formKeyArr.push({ name: item.componentName, type: item.required })
})
})
.catch(() => {
})
},
clickSubmit() {
const postParams = {} // 提交的數(shù)據(jù)
let isCanSubmit = true
this.formKeyArr.forEach((item, index) => {
console.log('item=', item)
if (item.type === '1' && !this.modelItems[index]) { // 所有require必須的標(biāo)記符
// 請(qǐng)先填寫(xiě)完成, toast請(qǐng)?zhí)顚?xiě)完整
isCanSubmit = false
}
postParams[item['name']] = this.modelItems[index]
})
if (isCanSubmit) {
// 可以提交數(shù)據(jù)
// 可以拿到提交表單數(shù)據(jù)
// { username: '我的姓名', sex: '0000' // 對(duì)應(yīng)”男“ }
console.log('postParams=', postParams)
}
}
}
}
</script>
<style lang="scss">
</style>
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue3中的ref,toRef,toRefs三個(gè)的作用使用小結(jié)
Vue3中ref、reactive、toRef、toRefs都是與響應(yīng)式數(shù)據(jù)相關(guān)的,就此做一份筆記作為區(qū)別,本文重點(diǎn)給大家講解vue3中的ref,toRef,toRefs三個(gè)是干嘛的,有什么作用,感興趣的朋友跟隨小編一起看看吧2022-11-11
Vue實(shí)現(xiàn)開(kāi)心消消樂(lè)游戲算法
這篇文章主要介紹了使用Vue寫(xiě)一個(gè)開(kāi)心消消樂(lè)游戲,游戲算法在文中給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Vue內(nèi)聯(lián)處理器中訪問(wèn)方法和訪問(wèn)事件參數(shù)詳解
在 Vue 3 中,使用組合式 API 時(shí),可以通過(guò)內(nèi)聯(lián)事件處理器來(lái)直接在模板中編寫(xiě)事件處理邏輯,內(nèi)聯(lián)事件處理器不僅可以直接執(zhí)行簡(jiǎn)單的操作,還可以調(diào)用組件中的方法,并訪問(wèn)事件參數(shù),下面將詳細(xì)講解如何在內(nèi)聯(lián)事件處理器中調(diào)用方法以及訪問(wèn)事件參數(shù),需要的朋友可以參考下2024-12-12
Vue filter 過(guò)濾當(dāng)前時(shí)間 實(shí)現(xiàn)實(shí)時(shí)更新效果
這篇文章主要介紹了Vue filter 過(guò)濾當(dāng)前時(shí)間 實(shí)現(xiàn)實(shí)時(shí)更新效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
vue axios請(qǐng)求頻繁時(shí)取消上一次請(qǐng)求的方法
這篇文章主要介紹了vue axios請(qǐng)求頻繁時(shí)取消上一次請(qǐng)求的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11

