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: ''
                    }
                ] //参建单位列表
            },

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

相关推荐
IT_陈寒30 分钟前
Vue这个坑我跳了两次,原来问题出在这
前端·人工智能·后端
kyriewen33 分钟前
我用 50 行代码重写了 React Router 核心,终于搞懂了前端路由原理
前端·javascript·react.js
WebInfra2 小时前
Rspack 2.1 发布:React Compiler 提速 10 倍!
前端
李明卫杭州2 小时前
CSS 媒体查询详解:一文掌握响应式设计的核心技术
前端
lichenyang4532 小时前
从 H5 按钮到 OpenHarmony 能力调用:我如何理解 ASCF 的运行链路
前端
下家3 小时前
我放弃了 Vue/React,选择自研框架
前端·前端框架
Asize3 小时前
HTML5 Canvas 基础:从按帧动画到 ECharts 数据可视化
前端·javascript·canvas
默_笙3 小时前
🎄 后端给我一堆扁平数据,我 10 行代码把它变成了树
前端·javascript
Mahut3 小时前
我用 Electron + FFmpeg 做了一个本地视频处理工作站 ClipForge
前端·ffmpeg·electron
前端Hardy3 小时前
又一个 AI 神器火了!
前端·javascript·后端