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
I logged into the backend to check the solution t...
filter is generally used to filter certain values...
Object's hasOwnProperty() method returns a Bo...
Table of contents As a global variable Variable H...
Table of contents Introduction to Arrays Array li...
Table of contents The origin of JSBridge The bidi...
1. Set CORS response header to achieve cross-doma...
1. Introduction Recently, I helped a friend to ma...
Table of contents 1. Filter unique values 2. Shor...
Table of contents Rendering Install Code Implemen...
Table of contents Linux environment variables and...
This article mainly introduces how to implement a...
Table of contents Loop - for Basic use of for loo...
Table of contents 1. writable: writable 2. enumer...
MySQL is a very powerful relational database. How...