el-form动态标题和输入值,并且最后一个输入框不校验

需求:给了固定的label,叫xx单位,要输入单位的信息,但是属性名称都一样的,UI画图也是表单的形式,所以改为动态添加的形式,实现方式也很简单,循环就完事了,连着表单校验也动态

1.效果如下

2.代码实现+讲解

因为需求是最后一个单位没有必要必填,所以在rules这里判断了一下,主要就是循环遍历了el-form-item,之后prop为了区分也是用index来区分了

html 复制代码
 <el-dialog title="新建单位" :close-on-click-modal="false" :close-on-press-escape="false" :visible.sync="unitDialog" width="30%">
            <el-form
                :model="formInline"
                ref="unitRuleForm"
                label-width="130px"
                class="demo-form-inline"
                label-position="left"
            >
                <el-form-item
                    v-for="(item, index) in formInline.formData"
                    :key="index"
                    :label="item.organizations_type"
                    :prop="'formData[' + index + '].organizations_name'"
                    :rules="
                        index < formInline.formData.length - 1
                            ? {
                                  required: true,
                                  message: `${item.organizations_type}不能为空`,
                                  trigger: 'blur'
                              }
                            : null
                    "
                >
                    <el-input v-model="item.organizations_name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="unitDialog = false">取 消</el-button>
                <el-button type="primary" @click="saveUnitData()">确 定</el-button>
            </span>
        </el-dialog>
javascript 复制代码
formInline: {
                formData: [
                    {
                        project_id: 0,
                        organizations_type: 'xx单位1',
                        organizations_name: ''
                    },
                    {
                        project_id: 0,
                        organizations_type: 'xx单位2',
                        organizations_name: ''
                    },
                    {
                        project_id: 0,
                        organizations_type: 'xx单位3',
                        organizations_name: ''
                    },
                    {
                        project_id: 0,
                        organizations_type: 'xx单位4',
                        organizations_name: ''
                    },
                    {
                        project_id: 0,
                        organizations_type: 'xx单位5',
                        organizations_name: ''
                    },
                    {
                        project_id: 0,
                        organizations_type: 'xx单位6',
                        organizations_name: ''
                    }
                ] //参建单位列表
            },

文章到此结束,希望对你有所帮助~

相关推荐
欧阳呀3 分钟前
Vue+element ui导入组件封装——超级优雅版
前端·javascript·vue.js·elementui
清风徐来QCQ5 分钟前
css总结
前端
天***88961 小时前
js封装一个双精度算法实现
开发语言·前端·javascript
Algebraaaaa1 小时前
什么是前端、后端与全栈开发,Qt属于什么?
开发语言·前端·qt
胡斌附体1 小时前
使用Electron创建helloworld程序
前端·javascript·electron·nodejs·pc
toobeloong1 小时前
Electron 从低版本升级到高版本 - webview通信的改造
前端·javascript·electron
im_AMBER2 小时前
React 01
前端·javascript·笔记·react.js·前端框架·web
@大迁世界2 小时前
React 19.2.0 有哪些新变化
前端·javascript·react.js·前端框架·ecmascript
华仔啊3 小时前
用 Vue3 + Canvas 做了个超实用的水印工具,同事都在抢着用
前端·vue.js·canvas