美文网首页
vue+ElementUI动态表单(校验规则、计算条件和约束条件

vue+ElementUI动态表单(校验规则、计算条件和约束条件

作者: vikang | 来源:发表于2020-08-26 16:02 被阅读0次

    主要用于记录,若能帮到他人,也算是功德了,哈哈。
    在原有基础上新增了组的概念(某些字段属于一个组,用的elementUI中的el-collapse区分),
    不啰嗦了,直接上代码(可直接拷贝试用)

    <template>
        <div class="dynamicForm">
            <el-collapse>
                <el-collapse-item v-for="(collapse,index) in form.getData" :key="index">
                    <template slot="title">
                        {{collapse.groupName}}
                        <el-button type="primary" size="mini" @click.stop="groupChange(index,collapse)">复制组</el-button>
                        <el-button type="danger" size="mini" @click.stop="groupChange(index)">删除组</el-button>
                    </template>
                    {{collapse.computedRule}}
                    <el-form ref="getData" :model="collapse" label-width="80px">
                        <el-form-item v-for="(item,key) in collapse.groupList" :key="key" :label="item.label" v-show="!item.hide"
                            :prop="'groupList.' + key +'.value'"  :rules="[{required: true, message: 'Can\'t be empty'}]">
                            <el-input v-model="item.value" :disabled="!item.formula"></el-input>
                        </el-form-item>
                    </el-form>
                </el-collapse-item>
            </el-collapse>
        </div>
    </template>
    
    <script>
    
    export default {
        name: 'DynamicFormUpgrade',
        data () {
            return {
                form: {
                    getData: [],
                },
                //存储所有watcher,增删组时需全部清除
                formWatchers: [],
            }
        },
        created () {
            this.getDataFn();
        },
        mounted() {
            //首次添加计算与约束规则
            this.addComOrResRules();
        },
        methods: {
            //增删组
            groupChange(index,collapse){
                //清空所有watcher
                this.formWatchers.forEach(_ => _());
                !!collapse ?
                this.form.getData.splice(index,0,JSON.parse(JSON.stringify(collapse))) :
                this.form.getData.splice(index,1);
                this.addComOrResRules();
            },
            //
            getDataFn() {
                /*数据挡板--key1、key2等为表单字段唯一标识,通常格式固定,
                方法内需根据此格式制定正则以便提取。*/
                this.form.getData = [
                    {
                        computedRule: '计算规则:label1*label2-label3/label4=result1;空值按0计算',
                        groupKey: 'groupOne',
                        groupName: '组一test',
                        groupList: {
                            key1:{
                                formula: 'key1*key2-key3/key4=key5',
                                value: 5,
                                label: 'label1',
                                hide: false,
                                hideRule: null
                            },
                            key2:{
                                formula: 'key1*key2-key3/key4=key5',
                                value: 400,
                                label: 'label2',
                                hide: false,
                                hideRule: null
                            },
                            key3:{
                                formula: 'key1*key2-key3/key4=key5',
                                value: 30,
                                label: 'label3',
                                hide: false,
                                hideRule: null
                            },
                            key4:{
                                formula: 'key1*key2-key3/key4=key5',
                                value: 2,
                                label: 'label4',
                                hide: false,
                                hideRule: null
                            },
                            key5:{
                                formula: null,
                                value: null,
                                label: 'result1',
                                hide: false,
                                hideRule: null
                            },
                            key6:{
                                formula: null,
                                value: 'isShow: result1 >= 200',
                                label: 'visible1',
                                hide: false,
                                hideRule: 'key5 >= 200'
                            },
                            key7:{
                                formula: null,
                                value: 'isShow: label3-label1 >= 20',
                                label: 'visible2',
                                hide: false,
                                hideRule: 'key3-key1 >= 20'
                            },
                            key8:{
                                formula: null,
                                value: 'isShow: label3-label1 < label2',
                                label: 'visible3',
                                hide: false,
                                hideRule: 'key3-key1 < key2'
                            }
                        },
                    },
                    {
                        computedRule: '计算规则:label1+label2*label3-label4=result1;空值按0计算',
                        groupKey: 'groupTwo',
                        groupName: '组二test',
                        groupList: {
                            key1:{
                                formula: 'key1+key2*key3-key4=key5',
                                value: 150,
                                label: 'label1',
                                hide: false,
                                hideRule: null
                            },
                            key2:{
                                formula: 'key1+key2*key3-key4=key5',
                                value: 10,
                                label: 'label2',
                                hide: false,
                                hideRule: null
                            },
                            key3:{
                                formula: 'key1+key2*key3-key4=key5',
                                value: 300,
                                label: 'label3',
                                hide: false,
                                hideRule: null
                            },
                            key4:{
                                formula: 'key1+key2*key3-key4=key5',
                                value: 2,
                                label: 'label4',
                                hide: false,
                                hideRule: null
                            },
                            key5:{
                                formula: null,
                                value: null,
                                label: 'result1',
                                hide: false,
                                hideRule: null
                            },
                            key6:{
                                formula: null,
                                value: 'isShow: result1 >= 150',
                                label: 'visible1',
                                hide: false,
                                hideRule: 'key5 >= 150'
                            },
                            key7:{
                                formula: null,
                                value: 'isShow: label3-label1 < 200',
                                label: 'visible2',
                                hide: false,
                                hideRule: 'key3-key1 < 200'
                            },
                            key8:{
                                formula: null,
                                value: 'isShow: label3-label1 > label2',
                                label: 'visible3',
                                hide: false,
                                hideRule: 'key3-key1 > key2'
                            }
                        },
                    },
                ];
            },
            //添加计算与约束规则
            addComOrResRules(){
                //动态添加监听--数据结构与计算规则需前后端统一制定再进行代码调整
                let _this = this;
                this.form.getData.forEach((_, index) => {
                    Object.keys(_.groupList).forEach(__ => {
                        //计算
                        _.groupList[__].formula &&
                        _.groupList[__].formula.indexOf('=') > -1 &&
                        this.formWatchers.push(_this.$watch('form.getData.'+index + '.groupList.' + __, (n, o) => {
                            let res = n.formula.split('=')[0].replace(/[0-9A-Za-z]{4}/img, key => Number(_.groupList[key].value));
                            console.log('form.getData.'+ index + '.groupList.' + __, res);
                            _.groupList[n.formula.split('=')[1]].value = eval(res).toString().indexOf('Infinity') > -1 ? 'error' : eval(res);
                        },{deep: true,immediate: true}));
                        //约束
                        _.groupList[__].hideRule && 
                        _.groupList[__].hideRule.replace(/[0-9A-Za-z]{4}/img, key => {
                            this.formWatchers.push(_this.$watch('form.getData.'+ index + '.groupList.' + key, (n, o) => {
                                let res = _.groupList[__].hideRule.replace(/[0-9A-Za-z]{4}/img, item => Number(_.groupList[item].value));
                                _.groupList[__].hide = !eval(res);
                            },{deep: true,immediate: true}));
                        })
                    })
                })
            }
        }
    }
    
    </script>
    
    <style lang="scss" scoped>
    .dynamicForm{
        margin: 0 100px;
    }
    </style>
    

    相关文章

      网友评论

          本文标题:vue+ElementUI动态表单(校验规则、计算条件和约束条件

          本文链接:https://www.haomeiwen.com/subject/tcqfsktx.html