SoFunction
Updated on 2025-04-06

Detailed explanation of the dynamic generation of Vue return value and the method of submitting data

Mainly solved problems

1. When vue is looping, different v-models need to be dynamically bound; how to bind data to vue dynamic forms?

2. Submit the key-value pairs corresponding to all name attributes on the dynamic form to the backend

1. The data returned by the backend is submitted to the backend as follows:

// The data returned by the backend is used to use the corresponding component according to the return type[
	{
	    "componentType": "input",
	    "componentName": "username",
	    "required": "1", // Whether it is necessary to fill in the submission	    "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'  // Corresponding 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="=='input'">
          <van-radio-group v-model="modelItems[idx]" direction="horizontal" v-if="=='radio'">
            <van-radio :name="" v-for="itemRadio in " :key="">
              {{}}
            </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'
(Radio).use(RadioGroup)
export default {
  data() {
    return {
      modelItems: {}, // vue needs to dynamically bind different v-models when looping      infoList: []
    }
  },
  mounted() {
     = []
    ()
  },
  methods: {
    getList() {
      getListData()
        .then((res) => {
          const infoListData = 
           = infoListData
          ((item, index) => {
          	// Save the attribute name and whether it is required, and use it for subsequent submission of data.          	// { name: 'username', type: 1 }, { name: 'sex', type: 1}
            ({ name: , type:  })
          })
        })
        .catch(() => {
        })
    },
    clickSubmit() {
      const postParams = {} // Submitted data      let isCanSubmit = true
      ((item, index) => {
        ('item=', item)
        if ( === '1' && ![index]) { // All required tags          // Please fill in it first, please fill in it toast.          isCanSubmit = false
        }
        postParams[item['name']] = [index]
      })
      if (isCanSubmit) {
      	// Can submit data      	// You can get the data on submitting form      	// { username: 'My name', sex: '0000' // Corresponding to "male" }      	('postParams=', postParams)
      }
    }
  }
}
</script>
<style lang="scss">
</style>

Summarize

That’s all for this article. I hope it can help you and I hope you can pay more attention to more of my content!