校验该字段是否重复

        需求:新增 或 编辑 的时候校验同一类型下的字典代码不能重复

 注:(当前数据类型是树结构,需要判断根节点(字典选择)下的字典类型是否重复,树节点(性别)下的字典类型是否重复)

        思路:写 if 判断,当我选择的是根节点(字典选择)的时候,拿我输入的值去和字典选择里面的数据的字典代码去做判断,(有校验的接口,false 表示不存在,true 表示存在)看是否重复。重复则提示;选择树节点(性别)思路与前面相似

父组件:

<Drawer :visible.sync="visible" :dictcode="dictcode" :dictCheck="dictCheck" ref="dictDrawer" :selectKeys="selectKeys" :treeKeys="treeKeys">
</Drawer>

data(){
    retuen{
        visible: false, //遮罩层
        treeKeys: '789',
        dictcode: '' //编辑回显的code  
    }
}

//新增按钮
async addBtn() {
    //选择根节点或者不选任何节点,点击新增,只传字典代码,选择树节点,传字典代码和字典类型
    (this.treeKeys == '789' || this.treeKeys == undefined) ? this.dictCheck = {
        dictPcode: this.searchForm.pcode
    } : this.dictCheck = {
          dictType: this.searchForm.type,
          dictPcode: this.searchForm.pcode
        }
	    this.getTree();
	    this.visible = true;
	}

//编辑按钮
async handleEdit(record) {
    this.dictcode = record.dictCode
    this.$refs.dictDrawer.handleEdit(record);
    this.visible = true;
}

子组件 :

props:{
    visible: {
        type: Boolean,
        default: false,
    },
    dictCheck: {
        type: Object
    },
    treeKeys: {
        type: String
    },
    dictcode: {
        type: String
    },
},

async verifyCode (rule, value, callback) {
    // 选中树节点进行校验
    if(this.treeKeys !== '789'){
        if(rule.field == "dictCode" && (value !== this.dictcode)){
            const checkCode = {
              dictType: this.formData.dictType,
              dictCode: this.formData.dictCode
            }
            const { data } = await dictVerify(checkCode)
            data.entity ? callback(new Error("字典代码重复,请重新输入!")) : callback();
        }
     // 未选择节点或者选中根节点进行校验
    }else if (this.treeKeys == '789'){
        if(rule.field == "dictCode" && (value !== this.dictcode)){
            const checkCode = {
              dictType: this.formData.dictType1 = '',
              dictCode: this.formData.dictCode
            }
            const { data } = await dictVerify(checkCode)
            data.entity ? callback(new Error("字典代码重复,请重新输入!")) : callback();
          }
        }
	 }

效果: 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值