【2.0版本】可自行增减的动态表单

介绍

关注编者博客的都知道小编的文章中有一篇动态表单文章,跳转链接,话说这篇文章写的是真的,,,,差,这篇文章小编就暂时不删了,留着提醒小编。好了,估计你看到这都烦了吧!!!
小白话不多说,下面就是对你们好的时候,直接放效果图,看各位是否喜欢,说错了,怎么能是喜欢呢,呸呸呸,应该是否符合你的口味,都看这了眼神下移,就可以看到效果图啦!!
单个动态表单

效果图

单个动态表单效果图

在这里插入图片描述

多个动态表单效果图

多个动态表单:效果图
在这里插入图片描述

重点部分讲解

效果都看了吧,能看到这的估计有点符合你的需求,这里稍微多啰嗦几句:用户角度:用户可以自动根据要录入数据的多少来显示并填报信息;开发者角度:如何实现自动控制数据量呢?只要你学到了,发现不难,它是通过控制表单中的长度来控制的,难点也就在这

这里小编主要一单个动态表单来进行详解:多个动态表单代码会自动附在最后。

正常情况下,表单prop定义:

<el-form :model="warnPeopleForm_one" ref="warnPeopleFormRef_one" :show-close="true">
    <el-row :gutter="10">
        <el-col :span="8">
            <el-form-item :prop="username" label="姓名" label-width="100px">
                <el-input type="text" v-model="item.username" style="width:150px;"></el-input>
            </el-form-item>
        </el-col>
        <el-col :span="9">
            <el-form-item :prop="phone_number" label="手机号" label-width="100px">
                <el-input type="text" v-model="item.phone_number" style="width:200px;"></el-input>
            </el-form-item>
        </el-col>
    </el-row>
</el-form>

而动态表单定义:

<el-form :model="warnPeopleForm_one" ref="warnPeopleFormRef_one" :show-close="true">
    <el-row :gutter="10" v-for="(item,index) in warnPeopleForm_one.object" :key="index">
        //这里要注意:v-for="(item,index) in warnPeopleForm_one.object" :key="index",这里有一个for循环遍历
        <el-col :span="8">
            <el-form-item :prop="'object.' + index + '.username'" label="姓名" label-width="100px">
                <el-input type="text" v-model="item.username" style="width:150px;"></el-input>
            </el-form-item>
        </el-col>
        <el-col :span="9">
            <el-form-item :prop="'object.' + index + '.phone_number'" label="手机号" label-width="100px">
                <el-input type="text" v-model="item.phone_number" style="width:200px;"></el-input>
            </el-form-item>
        </el-col>
        <el-col :span="7">
            <el-button style="margin-top:8px;" type="success" plain round circle size="small" v-if=" index == warnPeopleForm_one.object.length - 1 " :v-show="warnPeopleForm_one.object[index] == ''? false:true" @click="addSender_1()" class="el-icon-circle-plus"></el-button>
            //type=“success”的按钮主要是新增新的一个表单的按钮控制。这里它借助v-if判断
            warnPeopleForm_one.object的长度和下标是否相同,相同时新增按钮显示,这里要特别注意:
            新增按钮应该显示在最后一个表单里
            <el-button style="margin-top:8px;" type="danger" size="small" plain round circle v-if="warnPeopleForm_one.object.length > 1" @click="removeSender_1(index)" class="el-icon-remove"></el-button>
            //type="danger"的按钮主要是来控制取消多余表单,这个也有个特别注意:
            当warnPeopleForm_one.object的长度小于1时,应该让其不显示,其它情况让它都显示,
            这里用户可以更好的自由的控制那个取消
        </el-col>
    </el-row>
</el-form>

看完,你会发现:两者的prop定义不同。先看data中是如何定义:

正常情况下,data定义:

warnPeopleForm_one: { 
     username:'',
     phone_number: '',
},

而动态表单的data定义:

warnPeopleForm_one: {   //一级推送
     object: [
           {
                username:'',
                phone_number: '',
                risk_level: '',   //小编这里主要是根据后端要求,将该条数据标注字段
          }
     ]
},

这里object为一个数组格式,在通过下标index来控制显示几个,说到这,你可能会想到,数据传值怎么呢
我们可以直接this.warnPeopleForm_one.object这样你就可以接到动态表单中的所有值,格式为:

[ {username: '人员1',phone_number:'人员1手机号',}, {username:'人员2',phone_number:'人员2手机号'}, ... {username:'人员3',phone_number:'人员3手机号'} ]

事件:

addSender_1(){  //添加更多联系人
            this.warnPeopleForm_one.object.push( { username:'', phone_number: '', risk_level: '1'})
        },
        removeSender_1(index){   //移除
            this.warnPeopleForm_one.object.splice( index, 1)
        },
        sub_all(warnPeopleFormRef_one){  //提交
            this.$refs.warnPeopleFormRef_one.validate(valid => {
                console.log(valid);
                console.log(this.warnPeopleForm_one.object);
            })
            var level = ""
            if(this.num == 1){
                level = '1'
            }else if(this.num == 2){
                level = '2'
            }else if(this.num == 3){
                level = '3'
            }
            const params = {
                // "risk_level": level,    //该字段根据自己的情况来定就好
                "user_list": this.warnPeopleForm_one.object,
            }
            // console.log(params);
            this.$http.post('',params).then(res => {
                // console.log(res);
                if(res.data.code == 200){
                    this.resultVisible = false
                    this.$notify({
                        title:"温馨提示",
                        type:"success",
                        message:this.$createElement("i",{style:"color:green"}, res.data.msg)
                    })
                    this.getData()
                }else{
                    this.$notify({
                        title:"温馨提示",
                        type:"warning",
                        message:this.$createElement("i",{style:"color:orange"}, res.data.msg)
                    })
                }
            })
        }

标记:多个动态表单代码:

<template>
    <div>
        <el-dialog :visible="warnVisible" :show-close="false" :title="title_name" width="800px">
            <div>
                <el-row>
                    <el-col :span="12">
                        <span style="width:100px;"><span style="color:red;"><b>*</b></span>工点</span>
                        <el-select @focus="get_workpoint()" v-model="workPointId" style="width:210px;margin-left:33px;" :rules="[{required: true, message: '必填项', trigger: 'blur'}]">
                            <el-option v-for="item in work_point_list" :key="item.index" :value="item.id" :label="item.name"></el-option>
                        </el-select>
                    </el-col>
                    <el-col :span='12' style="text-align:right;">
                        <span>"<span style="color:red;"><b>*</b></span>"<span style="background-color:#ffff99;">代表:必填项</span></span>
                    </el-col>
                </el-row>
            </div>
            <!-- 一级人员设置(30%以上) -->
            <div>
                <div style=" height:3vh;line-height:3vh;background-color:#FF7777;margin:10px 0px 10px 0px;"><span><b>一级人员设置(30%以上)</b></span></div>
                <el-form :model="warnPeopleForm_one" ref="warnPeopleFormRef_one" :rules="warnPeopleForm_one_rules">
                    <el-row :gutter="10" v-for="(item,index) in warnPeopleForm_one.object" :key="index">
                        <el-col :span="8">
                            <el-form-item :prop="'object.' + index + '.username'" label="姓名" label-width="100px">
                                <el-input type="text" v-model="item.username" style="width:150px;"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="9">
                            <el-form-item :prop="'object.' + index + '.phone_number'" label="手机号" label-width="100px">
                                <el-input type="text" v-model="item.phone_number" style="width:200px;"></el-input>
                            </el-form-item>
                            
                        </el-col>
                        <!-- <el-col :span="2">
                            <el-form-item :v-show="false" :prop="'object.' + index + '.risk_level'" label="" label-width="">
                                <el-input disabled type="text" v-model="item.risk_level" style="width:20px;"></el-input>
                            </el-form-item>
                        </el-col> -->
                        <el-col :span="7">
                            <el-button style="margin-top:8px;" type="success" plain round circle size="small" v-if=" index == warnPeopleForm_one.object.length - 1 " :v-show="warnPeopleForm_one.object[index] == ''? false:true" @click="addSender_1()" class="el-icon-circle-plus"></el-button>
                            <el-button style="margin-top:8px;" type="danger" size="small" plain round circle v-if="warnPeopleForm_one.object.length > 1" @click="removeSender_1(index)" class="el-icon-remove"></el-button>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <!-- 二级人员设置(15%~30%-->
            <div>
                <div style="height:3vh;line-height:3vh;background-color:#FFC299;margin:10px 0px 10px 0px;"><span><b>二级人员设置(15%~30%</b></span></div>
                <el-form :model="warnPeopleForm_two" ref="warnPeopleFormRef_two" :rules="warnPeopleForm_two_rules">
                    <el-row :gutter="10" v-for="(item,index) in warnPeopleForm_two.object" :key="index">
                        <el-col :span="8">
                            <el-form-item :prop="'object.' + index + '.username'" label="姓名" label-width="100px" >
                                <el-input type="text" v-model="item.username" style="width:150px;"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="9">
                            <el-form-item :prop="'object.' + index + '.phone_number'" label="手机号" label-width="100px">
                                <el-input type="text" v-model="item.phone_number" style="width:200px;"></el-input>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="2">
                            <el-form-item :v-show="false" :prop="'object.' + index + '.risk_level'" label="" label-width="">
                                <el-input disabled type="text" v-model="item.risk_level" style="width:20px;"></el-input>
                            </el-form-item>
                        </el-col> -->
                        <el-col :span="7">
                            <el-button style="margin-top:8px;" type="success" plain round circle size="small" v-if=" index == warnPeopleForm_two.object.length - 1 " @click="addSender_2()" class="el-icon-circle-plus"></el-button>
                            <el-button style="margin-top:8px;" type="danger" size="mini" plain round circle v-if="warnPeopleForm_two.object.length > 1" @click="removeSender_2(index)" class="el-icon-remove"></el-button>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <!-- 三级人员设置(15%以内) -->
            <div>
                <div style="height:3vh;line-height:3vh;background-color:#FCFC9D;margin:10px 0px 10px 0px;"><span><b>三级人员设置(15%以内)</b></span></div>
                <el-form :model="warnPeopleForm_three" ref="warnPeopleFormRef_three" :rules="warnPeopleForm_three_rules">
                    <el-row :gutter="10" v-for="(item,index) in warnPeopleForm_three.object" :key="index">
                        <el-col :span="8">
                            <el-form-item :prop="'object.' + index + '.username'" label="姓名" label-width="100px">
                                <el-input type="text" v-model="item.username" style="width:150px;"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="9">
                            <el-form-item :prop="'object.' + index + '.phone_number'" label="手机号" label-width="100px">
                                <el-input type="text" v-model="item.phone_number" style="width:200px;"></el-input>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="2">
                            <el-form-item :v-show="false" :prop="'object.' + index + '.risk_level'" label="" label-width="">
                                <el-input disabled type="text" v-model="item.risk_level" style="width:20px;"></el-input>
                            </el-form-item>
                        </el-col> -->
                        <el-col :span="7">
                            <el-button style="margin-top:8px;" type="success" plain round circle size="small" v-if=" index == warnPeopleForm_three.object.length - 1 " @click="addSender_3()" class="el-icon-circle-plus"></el-button>
                            <el-button style="margin-top:8px;" type="danger" size="mini" plain round circle v-if="warnPeopleForm_three.object.length > 1" @click="removeSender_3(index)" class="el-icon-remove"></el-button>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <div style="margin-top:30px;">
                <el-button type="warning" round size="mini" @click="rest()">重置</el-button>
                <el-button type="info" round size="mini" @click="cel()">取消</el-button>
                <el-button type="primary" round size="mini" @click="sub_all('object')">新增提交</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    props:{
        warnVisible:{
            type: Boolean,
            require:true
        },
        title_name: {
            type: String,
            require: true
        },
        project_org_Id: {
            type: String,
            require: true
        },
        getData: {
            type: Function,
            require: true
        }
    },
    data(){
        var phone_ze = (rule, value, callback) => {
            const reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
            if(!reg.test(value)){
                callback(new Error('手机号格式不正确'))
            }else{
                callback()
            }
        }
        var name_ze = (rule, value, callback) => {
            const reg = /^[\u4e00-\u9fa5]{2,4}$/
            if(!reg.test(value)){
                callback(new Error('名字长度为2~4'))
            }else{
                callback()
            }
        }
        return {
            work_point_list: [], 
            workPointId: '',   //工点id
            warnPeopleForm_one: {   //一级推送
                object: [
                    {
                        username:'',
                        phone_number: '',
                        risk_level: '1'
                    }
                ]
            },
            warnPeopleForm_one_rules:{
                name:[{required: true, message: name_ze, trigger: 'blur'}],
                phone: [{required: true, message: phone_ze, trigger: 'blur'}]
            },
            warnPeopleForm_two: {   //二级推送
                object: [
                    {
                        username:'',
                        phone_number: '',
                        risk_level: '2'
                    }
                ]
            },
            warnPeopleForm_two_rules:{
                name:[{required: true, message: name_ze, trigger: 'blur'}],
                phone: [{required: true, message: phone_ze, trigger: 'blur'}]
            },
            warnPeopleForm_three: {   //三级推送
                object: [
                    {
                        username:'',
                        phone_number: '',
                        risk_level: '3'
                    }
                ]
            },
            list: [],
            warnPeopleForm_three_rules:{
                name:[{required: true, message: name_ze, trigger: 'blur'}],
                phone: [{required: true, message: phone_ze, trigger: 'blur'}]
            },
        }
    },
    methods:{
        cel(){  //取消按钮
            this.$emit("update:warnVisible",false)
        },
        rest(){  //重置
            this.$refs.warnPeopleFormRef_one.resetFields()
            this.$refs.warnPeopleFormRef_two.resetFields()
            this.$refs.warnPeopleFormRef_three.resetFields()
            this.workPointId = ''
        },
        get_workpoint(){
            this.$http.get('/url/?project_id=' + this.project_org_Id,{ headers: { 'AUTHORIZATION':sessionStorage.token }}).then(res => {

                if(res.data.code == 200){
                    this.work_point_list = res.data.data
                }else{
                    this.$notify({
                        title:"温馨提示",
                        type:"warning",
                        message:this.$createElement("i",{style:"color:orange"},res.data.msg)
                    })
                }
            })
        },
        // 一级告警推送人--------------------------------------
        addSender_1(){  //添加更多联系人
            this.warnPeopleForm_one.object.push( { username:'', phone_number: '', risk_level: '1'})
        },
        removeSender_1(index){   //移除
            this.warnPeopleForm_one.object.splice( index, 1)
        },
        // 二级告警推送人--------------------------------------
        addSender_2(){  //添加更多联系人
            this.warnPeopleForm_two.object.push( { username:'', phone_number: '', risk_level: '2'})
        },
        removeSender_2(index){   //移除
            this.warnPeopleForm_two.object.splice( index, 1)
        },
        // 三级告警推送人--------------------------------------
        addSender_3(){  //添加更多联系人
            this.warnPeopleForm_three.object.push( {username:'', phone_number: '', risk_level: '3'})
        },
        removeSender_3(index){   //移除
            this.warnPeopleForm_three.object.splice( index, 1)
        },
        sub_all(warnPeopleFormRef_three,warnPeopleFormRef_two,warnPeopleFormRef_one){  //提交
            console.log('3333');
            this.$refs.warnPeopleFormRef_three.validate(valid => {
                console.log(valid);
                console.log(this.warnPeopleForm_three.object);
            })
            console.log('2222');
            this.$refs.warnPeopleFormRef_two.validate(valid => {
                console.log(valid);
                console.log(this.warnPeopleForm_two.object);
            })
            console.log('1111');
            this.$refs.warnPeopleFormRef_one.validate(valid => {
                console.log(valid);
                console.log(this.warnPeopleForm_one.object);
            })
            var list  = []
            for(var i=0; i< this.warnPeopleForm_one.object.length; i++ ){
                if(this.warnPeopleForm_one.object[i].username !=='' && this.warnPeopleForm_one.object[i].phone_number !== ''){
                    list.push(this.warnPeopleForm_one.object[i])
                }
            }
            for(var i=0; i< this.warnPeopleForm_two.object.length; i++ ){
                if(this.warnPeopleForm_two.object[i].username !=='' && this.warnPeopleForm_two.object[i].phone_number !== ''){
                    list.push(this.warnPeopleForm_two.object[i])
                }
            }
            console.log(typeof(this.warnPeopleForm_one.object[0].username));
            console.log('---');
            for(var i=0; i< this.warnPeopleForm_three.object.length; i++ ){
                if(this.warnPeopleForm_three.object[i].username !=='' && this.warnPeopleForm_three.object[i].phone_number !== ''){
                    list.push(this.warnPeopleForm_three.object[i])
                }
            }
            if(this.project_org_Id !== '' || this.project_org_Id !== 'undefined'){
                // console.log(typeof(this.project_org_Id));
                const params = {
                    'project_id': this.project_org_Id,
                    'work_point_id': String(this.workPointId),
                    'user_list': list,
                }
                // console.log(params);
                this.$http.post('/url/', params).then( res => {
                    if(res.data.code == 200){
                        this.$notify({
                            title:"温馨提示",
                            type:"success",
                            message:this.$createElement("i",{style:"color:green"},res.data.msg)
                        })
                        this.$emit("update:warnVisible",false)
                        this.getData()
                        this.$refs.warnPeopleFormRef_one.resetFields()
                        this.$refs.warnPeopleFormRef_two.resetFields()
                        this.$refs.warnPeopleFormRef_three.resetFields()
                        this.workPointId = ''
                    }else{
                        this.$notify({
                            title:"温馨提示",
                            type:"warning",
                            message:this.$createElement("i",{style:"color:orange"},res.data.msg)
                        })
                    }
                })
            }else{
                this.$notify({
                    title:"温馨提示",
                    type:"warning",
                    message:this.$createElement("i",{style:"color:orange"},'工点为必填项,请选择_')
                })
            }
        }
    },
    mounted(){
        
    }
}
</script>
<style>

</style>
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
amis 是一个低代码前端框架,它使用 JSON 配置来生成页面,可以节省页面开发工作量,极大提升开发前端页面的效率。 目前在百度广泛用于内部平台的前端开发,已有 100+ 部门使用,创建了 3w+ 页面。 特点: 1、不需要懂前端:在百度内部,大部分 amis 用户之前从来没写过前端页面,也不会 JavaScript,却能做出专业且复杂的后台界面,这是所有其他前端 UI 库都无法做到的; 2、不受前端技术更新的影响:百度内部最老的 amis 页面是 4 年多前创建的,至今还在使用,而当年的 Angular/Vue/React 版本现在都废弃了,当年流行的 Gulp 也被 Webpack 取代了,如果这些页面不是用 amis,现在的维护成本会很高; 3、享受 amis 的不断升级:amis 一直在提升细节交互体验,比如表格首行冻结、下拉框大数据下不卡顿等,之前的 JSON 配置完全不需要修改; 4、可以完全使用可视化页面编辑器 来制作页面:一般前端可视化编辑器只能用来做静态原型,而 amis 可视化编辑器做出的页面是可以直接上线的。 5、提供完整的界面解决方案:其它 UI 框架必须使用 JavaScript 来组装业务逻辑,而 amis 只需 JSON 配置就能完成完整功能开发,包括数据获取、表单提交及验证等功能,做出来的页面不需要经过二次开发就能直接上线; 6、内置 100+ 种 UI 组件:包括其它 UI 框架都不会提供的富文本编辑器、条件组合等,能满足各种页面组件展现的需求,而且对于特殊的展现形式还可以通过 自定义组件 来扩充; 7、容器支持无限级嵌套:可以通过组合来满足各种布局需求; 8、经历了长时间的实战考验:amis 在百度内部得到了广泛使用,在 4 年多的时间里创建了 3 万+ 页面,从内容审核到机器管理,从数据分析到模型训练,amis 满足了各种各样的页面需求,最复杂的页面有超过 1 万行 JSON 配置。   amis前端低代码框架 更新日志: v1.1.7 Feature Wrapper 组件 style 支持动态获取 数据映射支持 cookie 获取 内置 filter 新增 map 方法 Rating 组件支持清空 Tabs 的 activeKey 支持变量 Excel 导出支持自定义文件名 数据映射的 key 可以支持 . 或者 [] 来指定路径 Tree-Selector 支持懒加载 升级 ECharts 到 5.1.1 升级 Monaco-Editor 到 0.24.0 Enhancement 升级 mst 到 3 的最新版本 开发使用 concurrently 避免新开一个窗口 data-link 优化 Wizard 组件新增 startStep 配置项 按钮 tooltip 整理,支持 disabledTip Each 组件空状态时文字居左,同时将空数组状态也认为是空状态 去掉 Tab line 模式下顶部的 padding Uuid 有值时不设置,没值自动设置 TextArea 组件最小行数限制 & 静态展示超出等 Form 远端校验显示报错时,可以再次提交 Nav 的 mapTree 需要 depthFirst Checkboxes 分组样式优化 DateTime-Range下拉增加 popoverClassName 属性,可以自定义弹框的 className; 父级有缩放比时弹框宽度计算问题修复; Date 快捷键支持上月底 autoFill 支持多选 CRUD 的 toolbar 默认不再将最后一个组件放右边 接口兼容多种 json 返回格式 CRUD filterable 判断是否选中不要那么严格 Button-Group disabled 统一使用透明度的方式来实现,不然无法区分选中状态是哪个 调整日期按钮位置顺序 和 Dialog 统一 Bugfix 修复 Audio should not call load method at first render 修复 文档多余描述 修复 CRUD filter Combo模式不能清空查询条件 修复 初始状态 autoFill 不同步的问题 修复 文档样例错误 修复 Audio 组件 src 属性不符合预期的行为 修复 表单联合校验问题 修复 PopOver 宽度计算问题 修复 图片表单项 disabled 影响放大功能的问题 修复 Transfer selectTitle resultTitle 不生效的问题 修复 Tree 组件问题 修复 Fiule 组件错误提示样式问题 修复 Select 组件自定义菜单模式下无法全选问题 修复 Number 最大最小值校验问题 修复 sdk 中 dialog 里的编辑器弹窗被遮挡问题 修复

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值