詳解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ù)返回類(lèi)型用對(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)文章
vue keep-alive 動(dòng)態(tài)刪除組件緩存的例子
今天小編就為大家分享一篇vue keep-alive 動(dòng)態(tài)刪除組件緩存的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11Vue.js實(shí)現(xiàn)的購(gòu)物車(chē)功能詳解
這篇文章主要介紹了Vue.js實(shí)現(xiàn)的購(gòu)物車(chē)功能,結(jié)合實(shí)例形式分析了vue.js購(gòu)物車(chē)的原理、數(shù)值計(jì)算及頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-01-01淺談mvvm-simple雙向綁定簡(jiǎn)單實(shí)現(xiàn)
本篇文章主要介紹了淺談mvvm-simple雙向綁定簡(jiǎn)單實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04vue3全局組件自動(dòng)注冊(cè)功能實(shí)現(xiàn)
本文主要講述vue3的全局公共組件的自動(dòng)注冊(cè)功能,本文分步驟結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-02-02