本篇内容主要讲解“Vue怎么动态生成数据字段”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Vue怎么动态生成数据字段”吧!
异步请求获取数据(一种配置数据,一种实际数据)
data () { return { config: [], list: [] }; }
props: { config: Array, list: Array }, data () { return { newConfig: [], configLength: 0, newList: [] }; }
因此需要监听数据变化,当有数据时展示子组件
configLoaded: false, listLoaded: false
定义上面两个变量来判断数据是否加载完成,在异步获取完数据后赋值为true,并监听
watch: { configLoaded (n, o) { this.configLoaded = n; }, listLoaded (n, o) { this.listLoaded = n; } },
并执行v-if
computed: { showItem () { return this.configLoaded && this.listLoaded; } }, <list-item :config="config" :list="list" v-if="showItem"></list-item>
<template> <div> <div class="item iconfont border-bottom" v-for="(item,index) of newList" :key="index"> <p v-for="(m,i) of item" :key="i">{{m.name}} {{m.text}}</p> </div> </div> </template>
<script> export default { name: 'Item', props: { config: Array, list: Array }, data () { return { newConfig: [], configLength: 0, newList: [] }; }, mounted () { this.toConfig(); }, methods: { toConfig () { this.configLength = this.config.length; for (let i in this.config) { let configItem = this.config[i]; this.newConfig.push({name: configItem.fieldName, text: configItem.fieldDesc}); } for (let l in this.list) { let item = this.list[l]; let childList = []; for (var c in this.newConfig) { let config = this.newConfig[c]; for (let key in item) { if (config.name === key) { childList.push({name: config.text, text: item[key]}); } } } this.newList.push(childList); } } } }; </script> <style lang="stylus" ref="stylesheet/stylus"> </style>
checkbox 多选,没有默认值的时候,一定要给一个空数组,不然就展示不出来
<el-form ref="myForm" :model="form" :rules="rules" :disabled="disabledBoolean"> <el-row> <el-col> <div v-for="(item ,index) in extendFieldColumns" :key="index" > <el-col v-if="item.type === 'input'" :span="defaultSpan"> <el-form-item :label-width="defaultLabelWidth" size="small" :rules="extendFieldRules[item.prop]" :prop="'extendField.'+item.prop" :label="item.label+':'" > <el-input v-model="form.extendField[item.prop]" :maxlength="item.maxlength" placeholder="请输入内容"></el-input> </el-form-item> </el-col> <el-col v-if="item.type === 'radio'" :span="defaultSpan"> <el-form-item :label-width="defaultLabelWidth" size="small" :rules="extendFieldRules[item.prop]" :prop="'extendField.'+item.prop" :label="item.label+':'" > <el-radio-group v-model="form.extendField[item.prop]" > <el-radio v-for="(option,index1) in item.dicData" :key="index1" :label="option.label" >{{option.label}}</el-radio> </el-radio-group> </el-form-item> </el-col> <el-col v-if="item.type === 'select'" :span="defaultSpan" > <el-form-item :label-width="defaultLabelWidth" size="small" :prop="'extendField.'+item.prop" :label="item.label+':'" :rules="extendFieldRules[item.prop]"> <el-select v-model="form.extendField[item.prop]" placeholder="请选择"> <el-option v-for="(option,index2) in item.dicData" :key="index2" :label="option.label" :value="option.label"> </el-option> </el-select> </el-form-item> </el-col> <el-col v-if="item.type === 'checkbox'" :span="defaultSpan" > <el-form-item :label-width="defaultLabelWidth" size="small" :prop="'extendField.'+item.prop" :label="item.label+':'" :rules="extendFieldRules[item.prop]"> <el-checkbox-group v-model="form.extendField[item.prop]" > <el-checkbox v-for="city in item.dicData" :label="city.label" :key="city.label">{{city.label}}</el-checkbox> </el-checkbox-group> </el-form-item> </el-col> <el-col v-if="item.type === 'number'" :span="defaultSpan" > <el-form-item :label-width="defaultLabelWidth" size="small" :prop="'extendField.'+item.prop" :label="item.label+':'" :rules="extendFieldRules[item.prop]"> <el-input-number v-model="form.extendField[item.prop]" :max="item.maxlength" ></el-input-number> </el-form-item> </el-col> <el-col v-if="item.type === 'textarea'" :span="defaultSpan" > <el-form-item :label-width="defaultLabelWidth" size="small" :prop="'extendField.'+item.prop" :label="item.label+':'" :rules="extendFieldRules[item.prop]"> <el-input v-model="form.extendField[item.prop]" :maxlength="item.maxlength" type="textarea" placeholder="请输入内容" > </el-input> </el-form-item> </el-col> <el-col v-if="item.type === 'date'" :span="defaultSpan" > <el-form-item :label-width="defaultLabelWidth" size="small" :prop="'extendField.'+item.prop" :label="item.label+':'" :rules="extendFieldRules[item.prop]"> <el-date-picker v-model="form.extendField[item.prop]" type="date" placeholder="选择日期"> </el-date-picker> </el-form-item> </el-col> </div> </el-col> </el-row> </el-form>
到此,相信大家对“Vue怎么动态生成数据字段”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。