ElementUI 表单中一个 el-form-item 下多个

2021-03-22  本文已影响0人  Cherry丶小丸子
demo.png
<template>
    <el-form :model="form" ref="ruleForm" label-width="100px">
        <el-form-item label="负责人" prop="principal" :rules="rules.principal">
            <el-input v-model="form.principal"></el-input>
        </el-form-item>
        <el-form-item label="活动列表">
            <el-row :gutter="20" v-for="(item, index) in form.activities" :key="index" style="margin-bottom: 20px;">
                <el-col :span="12">
                    <!-- 
                        :prop="'activities.' + index + '.name'"
                        :prop="`activities.${index}.name`"

                        以上两种写法实现的效果一样,只是写法不同
                    --> 
                    <el-form-item :label="`名称${index + 1}`" :prop="`activities.${index}.name`" :rules="rules.name">
                        <el-input v-model="item.name" placeholder="请输入活动名称"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <!-- 
                        :prop="'activities.' + index + '.content'"
                        :prop="`activities.${index}.content`"

                        以上两种写法实现的效果一样,只是写法不同
                    --> 
                    <el-form-item :label="`主题${index + 1}`" :prop="`activities.${index}.content`" :rules="rules.content">
                        <el-input v-model="item.content" placeholder="请输入活动主题"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="2" style="text-align:center; font-size: 16px">
                    <i class="el-icon-circle-plus-outline" @click="addItem" v-if="index === 0"></i>
                    <i class="el-icon-remove-outline" @click="removeItem(index)" v-else></i>
                </el-col>
            </el-row>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    export default {
        data () {
            return {
                form: {
                    principal: '',
                    activities: [{
                        name: '',
                        content: ''
                    }]
                },
                rules: {
                    principal: [
                        { required: true, message: '请输入负责人', trigger: 'blur' }
                    ],
                    name: [
                        { required: true, message: '请输入活动名称', trigger: 'blur' }
                    ],
                    content: [
                        { required: true, message: '请输入活动主题', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            submitForm (formName) {
                this.$refs[formName].validate((valid) => {
                    if (!valid) return console.log('未通过校验')
                })
            },
            addItem () {
                this.form.activities.push({
                    name: '',
                    content: ''
                })
            },
            removeItem (index) {
                this.form.activities.splice(index, 1)
            },
            resetForm (formName) {
                this.$refs[formName].resetFields()
            }
        }
    }
</script>
otherDome.png
<el-form :model="templateForm" :rules="templateFormRules" ref="templateForm" size="small" label-position="top">
    <el-row :gutter="20" v-for="(item, index) of templateForm.publicConfig" :key="index">
        <el-col :span="12" v-for="(itemc, indexc) of item" :key="indexc">
            <!-- 
                :prop="'publicConfig.' + index + '.' + indexc + '.configValue'"
                :prop="`publicConfig.${index}.${indexc}.configValue`"

                以上两种写法实现的效果一样,只是写法不同
            --> 
            <el-form-item :label="itemc.configChineseName + ':'" :prop="`publicConfig.${index}.${indexc}.configValue`">
                <el-input v-model="itemc.configValue" :placeholder="'请输入' + itemc.configChineseName"></el-input>
            </el-form-item>
        </el-col>
    </el-row>
</el-form>

<script>
    export default {
        data () {
            return {
                templateForm: {
                    publicConfig: []
                }
            }
        },
        methods: {
            /**
             * 获取配置
             */
            getDefaultConfig(){
                this.$axios.getDefaultConfig().then(res => {
                    this.templateForm.publicConfig = this.splitArray(res.data.data, 2);
                })
            },
            /**
             * 数组的数据格式转换拆分-----一维转二维
             * @param {Object} arr 源数组
             * @param {Object} len 一维的长度
             */
            splitArray(arr, len) {
                let arrLen = arr.length;
                let result = [];
                for (let i = 0; i < arrLen; i += len) {
                    result.push(arr.slice(i, i + len));
                }
                return result;
            }
        }
    }
</script>
上一篇下一篇

猜你喜欢

热点阅读