Main issues solved1. Vue needs to dynamically bind different v-models when looping; how to bind data to Vue's dynamic form? 2. All key-value pairs corresponding to the name attributes on the dynamic form are submitted to the backend 1. The data returned by the backend and submitted to the backend are in the following format:// The data returned by the backend is used with the corresponding component according to the return type [ { "componentType": "input", "componentName": "username", "required": "1", // Whether it is required to fill in when submitting "name": "Name", }, { "componentType": "radio", "componentName": "sex", "required": "1", "name": "Gender", "options": [ { "name": "Male", "value": "0000" }, { "name": "Female", "value": "1111" } ] } ] // Data format submitted to the server { username: 'My name', sex: '0000' // corresponds to "male" } 2. The vue front-end code is as follows:<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">Submit data</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 needs to dynamically bind different v-models when looping infoList: [] } }, mounted() { this.formKeyArr = [] this.getList() }, methods: { getList() { getListData() .then((res) => { const infoListData = res.infoList this.infoList = infoListData infoListData.forEach((item, index) => { // Save the attribute name and whether it is required, which will be used for subsequent data submission // { name: 'username', type: 1 }, { name: 'sex', type: 1} this.formKeyArr.push({ name: item.componentName, type: item.required }) }) }) .catch(() => { }) }, clickSubmit() { const postParams = {} // Submitted data let isCanSubmit = true this.formKeyArr.forEach((item, index) => { console.log('item=', item) if (item.type === '1' && !this.modelItems[index]) { // All require tags // Please fill in the form first, please fill in the toast form completely isCanSubmit = false } postParams[item['name']] = this.modelItems[index] }) if (isCanSubmit) { //Can submit data //Can get the submitted form data //{ username: 'my name', sex: '0000' //corresponds to "male" } console.log('postParams=', postParams) } } } } </script> <style lang="scss"> </style> SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Dynamically add tables in HTML_PowerNode Java Academy
>>: How to quickly query 10 million records in Mysql
Table of contents About Triggers Use of triggers ...
Study plans are easily interrupted and difficult ...
Relative Length Units em Description: Relative len...
(I) Method 1: Define it in advance directly in th...
Table of contents 1. Commonly used string functio...
Table of contents Problem 1: Destruction 1. How t...
I'm playing with big data recently. A friend ...
When talking about the screen reading software op...
String extraction without delimiters Question Req...
text OK, next it’s time to show the renderings. O...
Table of contents introduce Link start Continue t...
Preface You may often receive warning emails from...
0. Why do we need remote development? When develo...
Rendering Code - Take the blue and yellow rings a...
After installing centos 8, the following error wi...