当前位置: 首页 > news >正文

vue3实现规则编辑器

组件用于创建和编辑复杂的条件规则,支持添加、删除条件和子条件,以及选择不同的条件类型。
可实现json数据和页面显示的转换。

在这里插入图片描述

在这里插入图片描述
代码实现 :
index.vue:

<template><div class="allany-container"><div class="control-bar"><el-select v-model="pipe.condition" style="width: 200px; margin-right: 16px;" :disabled="disabled"><el-option label="满足以下所有条件" value="all"/><el-option label="满足以下任一条件" value="any"/><el-option label="不包含以下条件" value="not"/></el-select><el-button v-if="!disabled" type="primary" @click="addCondition('condition')">添加条件</el-button><el-button v-if="!disabled" type="success" @click="addCondition('all_any')">添加子条件</el-button><el-button v-if=" !disabled" type="danger" :icon="Delete" circle @click="delSelf"/></div><div class="conditions-wrapper"><!--   侧边显示条   --><div :class="`conditions-wrapper-${pipe.condition}`"></div><div class="conditions-wrapper--conditions"><div v-for="(child, idx) in pipe.children" :key="idx" class="conditions-wrapper--condition"><biz-rule-all-any v-if="child.type === 'all_any'" :pipe="child" :attrOptions="attrOptions":disabled="disabled" @delRule="handleDelRule(idx,child)"/><biz-rule-condition v-else-if="child.type === 'condition'" :pipe="child" :attrOptions="attrOptions":disabled="disabled" @delRule="handleDelCondition(idx,child)"/></div></div></div></div>
</template><script setup>
import {ref, watch} from 'vue';
import BizRuleCondition from './BizRuleCondition.vue';
import {Delete} from "@element-plus/icons-vue";
import {deepClone} from "@/utils.js";defineOptions({name: 'BizRuleAllAny'
})const emit = defineEmits(['delRule']);const props = defineProps({pipe: {type: Object,default: () => ({type: 'all_any',condition: 'all',children: [],})},attrOptions: {type: Array,default: () => {return []}},disabled: {type: Boolean,default: false}
});
const pipe = ref(props.pipe);function addCondition(type) {if (type === 'all_any') {pipe.value.children.push({type: 'all_any',condition: 'all',children: [],});} else if (type === 'condition') {pipe.value.children.push({type: 'condition',name: '',operator: 'eq',value: '',val_type: 'string',});}
}function delSelf() {emit('delRule')
}// 删除条件组
const handleDelRule = (idx,child) => {pipe.value.children.splice(idx, 1);
}// 删除条件组中的子数据
const handleDelCondition = (idx,child) => {pipe.value.children.splice(idx, 1);
}</script><style scoped lang="scss">.allany-container {.control-bar {display: flex;flex-direction: row;}.conditions-wrapper {display: flex;flex-direction: row;}.conditions-wrapper-all {width: 4px;margin: 5px 20px 0;border-radius: 5px;transition: background-color 400ms;background-color: #67C23A;&:hover {background-color: #529b2e;}}.conditions-wrapper-any {width: 4px;margin: 5px 20px 0;border-radius: 5px;transition: background-color 400ms;background-color: #E6A23C;&:hover {background-color: #b88230;}}.conditions-wrapper-not {width: 4px;margin: 5px 20px 0;border-radius: 5px;transition: background-color 400ms;background-color: rgb(245, 245, 245);&:hover {background-color: rgb(144, 147, 153);}}.conditions-wrapper--conditions {display: flex;flex-direction: column;}.conditions-wrapper--condition {padding-top: 15px;}
}</style>

BizRuleCondition.vue:

<template><div class="bizrulecondition-container"><el-select v-model="pipe.name" placeholder="字段名称" style="width: 150px; margin-right: 16px;" clearable :disabled="disabled"><el-option v-for="item in attrOptions" :key="item.value" :label="item.label" :value="item.value"/></el-select><el-select v-model="pipe.operator" style="width: 90px; margin-right: 16px;" :disabled="disabled"><el-option label="==" value="eq"/><el-option label="!=" value="neq"/><el-option label="<" value="lt"/><el-option label=">" value="gt"/><el-option label="<=" value="lte"/><el-option label=">=" value="gte"/><el-option label="in" value="in"/><el-option label="not in" value="not_in"/></el-select><el-badge:value="pipe.val_type === 'string' ? '字符串' : '数字'":type="pipe.val_type === 'string' ? 'info' : 'success'"@click.native="switchVarType($event, pipe)"style="margin-right: 50px;":disabled="disabled"><el-inputv-model="pipe.value"placeholder="字段值"style="width: 150px;"clearable:disabled="disabled"/></el-badge><el-button type="danger" :icon="Delete" circle @click="delSelf" :disabled="disabled"/></div>
</template><script setup>
import {ref, defineProps, watch} from 'vue';
import {Delete} from "@element-plus/icons-vue";
import {deepClone} from "@/utils.js";const emit = defineEmits(['delRule']);const props = defineProps({pipe: Object,attrOptions:Array,disabled:Boolean
});
const pipe = ref({});watch(() => props.pipe, (newData) => {if (!newData) returnpipe.value = deepClone(newData)
}, {deep: true,immediate: true
})function switchVarType(e, kv) {if (String(e.target.tagName).toUpperCase() === 'SUP') {kv.val_type = kv.val_type === 'number' ? 'string' : 'number';}
}function delSelf() {emit('delRule')
}
</script><style lang="scss" scoped>
.bizrulecondition-container {display: flex;flex-direction: row;.el-badge__content {transition: 400ms;user-select: none;}.el-badge__content:hover {cursor: pointer;}
}
</style>

BizRuleAdapter.js:

<template><div class="bizrulecondition-container"><el-select v-model="pipe.name" placeholder="字段名称" style="width: 150px; margin-right: 16px;" clearable :disabled="disabled"><el-option v-for="item in attrOptions" :key="item.value" :label="item.label" :value="item.value"/></el-select><el-select v-model="pipe.operator" style="width: 90px; margin-right: 16px;" :disabled="disabled"><el-option label="==" value="eq"/><el-option label="!=" value="neq"/><el-option label="<" value="lt"/><el-option label=">" value="gt"/><el-option label="<=" value="lte"/><el-option label=">=" value="gte"/><el-option label="in" value="in"/><el-option label="not in" value="not_in"/></el-select><el-badge:value="pipe.val_type === 'string' ? '字符串' : '数字'":type="pipe.val_type === 'string' ? 'info' : 'success'"@click.native="switchVarType($event, pipe)"style="margin-right: 50px;":disabled="disabled"><el-inputv-model="pipe.value"placeholder="字段值"style="width: 150px;"clearable:disabled="disabled"/></el-badge><el-button type="danger" :icon="Delete" circle @click="delSelf" :disabled="disabled"/></div>
</template><script setup>
import {ref, defineProps, watch} from 'vue';
import {Delete} from "@element-plus/icons-vue";
import {deepClone} from "@/utils.js";const emit = defineEmits(['delRule']);const props = defineProps({pipe: Object,attrOptions:Array,disabled:Boolean
});
const pipe = ref({});watch(() => props.pipe, (newData) => {if (!newData) returnpipe.value = deepClone(newData)
}, {deep: true,immediate: true
})function switchVarType(e, kv) {if (String(e.target.tagName).toUpperCase() === 'SUP') {kv.val_type = kv.val_type === 'number' ? 'string' : 'number';}
}function delSelf() {emit('delRule')
}
</script><style lang="scss" scoped>
.bizrulecondition-container {display: flex;flex-direction: row;.el-badge__content {transition: 400ms;user-select: none;}.el-badge__content:hover {cursor: pointer;}
}
</style>
http://www.lryc.cn/news/473836.html

相关文章:

  • 【快速上手】pyspark 集群环境下的搭建(Standalone模式)
  • 中文NLP地址要素解析【阿里云:天池比赛】
  • 使用AddressSanitizer内存检测
  • 11月1日星期五今日早报简报微语报早读
  • 实用篇:Postman历史版本下载
  • 微服务实战系列之玩转Docker(十七)
  • 操作系统-实验报告单(1)
  • rom定制系列------小米8青春版定制安卓14批量线刷固件 原生系统
  • CATIA许可证常见问题解答
  • PySpark Standalone 集群部署教程
  • 【源码+文档】基于SpringBoot+Vue旅游网站系统【提供源码+答辩PPT+参考文档+项目部署】
  • 9.排队模型-M/M/1
  • 【GO学习笔记 go基础】编译器下载安装+Go设置代理加速+项目调试+基础语法+go.mod项目配置+接口(interface)
  • 从0开始学习shell脚本
  • 官方工具重装Windows 11当前版本 /绕过硬件检查/免U盘
  • JavaEE初阶---网络原理/UDP服务器客户端程序
  • 每天10个vue面试题(六)
  • Qt:信号和槽
  • 可以免费商用的字体下载
  • centos7之LVS-TUNNEL模式
  • Linux驱动开发(3):字符设备驱动
  • 刘艳兵-DBA023-控制文件是Oracle 数据库用来查找数据库文件,控制文件包含以下哪些信息:
  • Vue Scoped CSS深度解析:原理、误区与最佳实践
  • 744. 寻找比目标字母大的最小字母
  • 浅谈QT中Tab键的切换逻辑
  • 基于MoviNet检测视频中危险暴力行为
  • 《等保测评:抵御网络威胁的盾牌》
  • 前端必知必会-JavaScript 对象属性
  • 双11都有什么值得入手的好物?双十一最建议买的5样东西
  • Xcode 15.4 运行flutter项目,看不到报错信息详情?