vue3使用element-plus,树组件el-tree增加引导线

devtools/2024/11/14 0:35:45/

vue3使用element-plus,树组件el-tree增加引导线

vue3项目element-plus,树组件el-tree增加引导线

element-plus组件库的el-tree样式
在这里插入图片描述

因为element的样式不满足当前的的需求,UI图,所以对el-tree进行增加了引导线
修改样式如下,增加引导线,并且实现增删,输入框

在这里插入图片描述

代码实现

1、在vue3中组件里面template模板引入el-tree组件,需要设置:indent=“0”,不然每层树padding-left呈18px的倍数增加

javascript"><div class="mytree"><el-tree:indent="0":data="data":props="defaultProps":expand-on-click-node="false":highlight-current="false"node-key="id"@node-click="handleNodeClick":default-expanded-keys="expDtable"></el-tree>
</div>
  1. indet:设置每层叔padding-left为0
  2. data:绑定的展示数据
  3. props:配置选项,里面配置节点,子节点字段等,默认绑定子节点children,element-plus里面有详细的解说[Tree 树形控件 | Element Plus]
  4. expand-on-click-node:是否在点击节点的时候展开或者收缩节点, 默认值为 true,如果为 false,则只有点箭头图标的时候才会展开或者收缩节点。
  5. node-key:每个树节点用来作为唯一标识的属性,整棵树应该是唯一的
  6. node-click:当节点被点击的时候触发
  7. default-expanded-keys:默认展开节点

2.在setup中声明变量data,defaultProps,handleNodeClick,expDtable

javascript">import { computed, shallowRef,ref,onMounted } from 'vue';const expDtable = ref([])const data = ref([{name: '',type: '',describe: '',cekk: true,id: 1}
])const defaultProps = {children: 'children',label: 'label',
}
const handleNodeClick = data => {console.log(data)
}

3.在el-tree里面写入输入框已经增删,接着上面模板的代码 我们往里面继续写

javascript"> <el-tree:indent="0":data="data":props="defaultProps":expand-on-click-node="false":highlight-current="false"node-key="id"@node-click="handleNodeClick":default-expanded-keys="expDtable"><template #default="{ node, data }"><!-- {{node.level}} --><div class="custom-tree-node" style="display: flex; width: 100%"><div :style="{'width': node.level == 1 ? '120px' : node.level == 2 ? '100px' : '80px'}"><el-input v-model="data.name" placeholder="变量名" :disabled="data.id == 1" /></div><div style="width: 100px; margin-left: 10px"><el-select v-model="data.type" placeholder="Select" :disabled="data.id == 1"><el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" /></el-select></div><div style="width: 120px; margin-left: 10px"><el-inputv-model="data.describe"placeholder="请输入用户变量用途"type="number":disabled="data.id == 1"/></div><div style="width: 50px; text-align: center; margin-left: 20px"><el-checkbox type="checkbox" v-model="data.cekk" :disabled="data.id == 1" /></div><div style="width: 40px; text-align: right"><span style="cursor: pointer" v-if="data.type == '2' && data.boolean != 1" @click="childAdd(node,data)">+</span></div><div style="width: 40px; text-align: right"><span style="cursor: pointer" v-if="data.id != 1" @click="remove(node, data)">-</span></div></div></template></el-tree>

4.在setup里面加上我们的新增方法,删除方法,以及选择框绑定的数据

javascript">import { computed, shallowRef,ref,onMounted } from 'vue';const containerRef = shallowRef()
const options = [{value: '1',label: 'javaScript'},{value: '2',label: 'Array<object>'}
]const expDtable = ref([])const data = ref([{name: '',type: '',describe: '',cekk: true,id: 1}
])
// 新增
const addUser = () => {data.value.push({name: '',type: '',describe: '',cekk: true,children: [],id: getRandomSixDigitNumber()})
}const remove = (node, datas) => {const parent = node.parent;const children = parent.data.children || parent.data;const index = children.findIndex(d => d.id === datas.id);children.splice(index, 1)
}const getRandomSixDigitNumber = () => {// 生成一个介于0(包含)和1(不包含)之间的随机浮点数let randomFloat = Math.random()// 将这个浮点数放大到1000000(10^6)的范围内,并四舍五入到最近的整数let randomInt = Math.floor(randomFloat * 1000000)// 检查生成的数是否小于100000(六位数的最小值),如果是,则加上100000// 确保生成的数是一个六位数if (randomInt < 100000) {randomInt += 100000}return randomInt
}const childAdd = (node,value) => {if (node.level == 3) {return false}expDtable.value = [value.id]value.children.push({name: '',type: '',describe: '',cekk: true,id: getRandomSixDigitNumber(),children: []})data.value = data.value
}
const defaultProps = {children: 'children',label: 'label',
}
const handleNodeClick = data => {console.log(data)
}

5.最后就是css,主要代码实现在css部分,该项目使用scss,deep深度选择器使用如下方式,css代码如下

<style>
/* 鼠标浮动过的背景颜色 */
.el-tree-node__content:hover {background: #eeeeee;
}
/* 鼠标点击时节点的背景颜色 */
.el-tree-node:focus>.el-tree-node__content {background-color: #eeeeee !important;color: node;
}
/* 鼠标失去焦点时节点背景的颜色 */
.el-tree--highlight-current .el-tree-node.is-current>.el-tree-node__content {background: #eeeeee;
}</style><style lang="scss" scoped>
::v-deep .el-input__wrapper {border-radius: 10px;
}
::v-deep .el-select__wrapper {border-radius: 10px;
}
.mytree {width: 110%;margin-left: -30px;
}
:deep(.mytree) {.el-tree-node {position: relative;padding-left: 10px;margin-top: 10px;// height:50px;background: none;// margin-bottom: 10px;}.el-tree-node__children {padding-left: 10px;// height:100px;// margin-top:10px;}.el-tree-node :last-child:before {height: 38px;}.el-tree > .el-tree-node:before {border-left: none;}.el-tree > .el-tree-node:after {border-top: none;}.el-tree {background: none;}.el-tree-node:before {content: '';left: -4px;position: absolute;right: auto;border-width: 1px;border-left: 1px dashed #1389bc;bottom: 0px;height: 100%;top: -26px;width: 1px;}.el-tree-node:after {content: '';left: -4px;position: absolute;right: auto;border-width: 1px;border-top: 1px dashed #1389bc;height: 20px;top: 12px;width: 18px;}// .el-tree .el-tree-node__expand-icon.expanded {//   -webkit-transform: rotate(0deg);//   transform: rotate(0deg);// }.el-tree .el-icon-caret-right:before {content: '\e723';font-size: 16px;color: #1389bc;position: absolute;left: -20px;top: -8px;}.el-tree .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {content: '\e722';font-size: 16px;color: #1389bc;position: absolute;left: -20px;top: -8px;}.el-tree-node__expand-icon.is-leaf:before {content: '\e722';font-size: 16px;color: #1389bc;position: absolute;left: -20px;top: -8px;}.el-tree-node__content > .el-tree-node__expand-icon {padding: 0;// padding:10px 0;}
}
.start {width: 100%;// height: 400px;background: #eeeeee;margin: 0 auto;border-radius: 5px;.start-input {font-weight: 700;margin: 0 20px;}.table {width: 90%;margin: 0 auto;.table-row {display: flex;justify-content: space-between;font-size: 12px;color: #ccc;}.table-row-one {width: 100%;display: flex;justify-content: space-between;align-items: center;}}.button {margin: 0 20px;}
}
</style>

完整代码

javascript"><template><div style="padding: 10px; width: 100%;"><div class="start" style=" overflow: auto"><div class="start-input">输入</div><div class="table"><div class="table-row"><div class="table-header" style="width: 18%">变量名</div><div class="table-header" style="width: 13%">变量类型</div><div class="table-header" style="width: 15%; text-align: center">描述</div><div class="table-header">是否必要</div><div class="table-header"></div><div class="table-header"></div></div><div><div class="table-row-one"><!-- <div style="width: 20%"><el-input v-model="input" placeholder="Please input" /></div><div style="width: 25%"><el-select v-model="value" placeholder="Select"><el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" /></el-select></div><div style="width: 25%"><el-input v-model="input1" placeholder="Please input" type="number" /></div><div style="width: 25%; margin-right: -70px"><el-checkbox type="checkbox" /></div><div><span style="cursor: pointer">+</span></div><div><span style="cursor: pointer">-</span></div> --><div class="mytree"><el-tree:indent="0":data="data":props="defaultProps":expand-on-click-node="false":highlight-current="false"node-key="id"@node-click="handleNodeClick":default-expanded-keys="expDtable"><template #default="{ node, data }"><!-- {{node.level}} --><div class="custom-tree-node" style="display: flex; width: 100%"><div :style="{'width': node.level == 1 ? '120px' : node.level == 2 ? '100px' : '80px'}"><el-input v-model="data.name" placeholder="变量名" :disabled="data.id == 1" /></div><div style="width: 100px; margin-left: 10px"><el-select v-model="data.type" placeholder="Select" :disabled="data.id == 1"><el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" /></el-select></div><div style="width: 120px; margin-left: 10px"><el-inputv-model="data.describe"placeholder="请输入用户变量用途"type="number":disabled="data.id == 1"/></div><div style="width: 50px; text-align: center; margin-left: 20px"><el-checkbox type="checkbox" v-model="data.cekk" :disabled="data.id == 1" /></div><div style="width: 40px; text-align: right"><span style="cursor: pointer" v-if="data.type == '2' && data.boolean != 1" @click="childAdd(node,data)">+</span></div><div style="width: 40px; text-align: right"><span style="cursor: pointer" v-if="data.id != 1" @click="remove(node, data)">-</span></div></div></template></el-tree></div></div></div></div><div class="button"><el-buttonv-for="button in buttons"@click="addUser()":key="button.text":type="button.type"textbgstyle="margin-top: 30px">{{ button.text }}</el-button></div></div></div>
</template><script setup>
import { computed, shallowRef,ref,onMounted } from 'vue';const buttons = [{ type: 'primary', text: '+新增' }]const input = ref('')
const input1 = ref('')const value = ref('')
const options = [{value: '1',label: 'javaScript'},{value: '2',label: 'Array<object>'}
]const expDtable = ref([])const data = ref([{name: '',type: '',describe: '',cekk: true,id: 1}
])
// 新增
const addUser = () => {data.value.push({name: '',type: '',describe: '',cekk: true,children: [],id: getRandomSixDigitNumber()})
}const remove = (node, datas) => {const parent = node.parent;const children = parent.data.children || parent.data;const index = children.findIndex(d => d.id === datas.id);children.splice(index, 1)// const parent = node.parent// const children = parent.data.children || parent.data// console.log(children)// const id = children.filter(d => d.id === datas.id)// if (id.length > 0) {//   console.log(id[0].id)//  const T = data.value.slice(id[0].id, 1)//   console.log(data.value)//   console.log(T)// data.value = [...data.value]// }}const getRandomSixDigitNumber = () => {// 生成一个介于0(包含)和1(不包含)之间的随机浮点数let randomFloat = Math.random()// 将这个浮点数放大到1000000(10^6)的范围内,并四舍五入到最近的整数let randomInt = Math.floor(randomFloat * 1000000)// 检查生成的数是否小于100000(六位数的最小值),如果是,则加上100000// 确保生成的数是一个六位数if (randomInt < 100000) {randomInt += 100000}return randomInt
}const childAdd = (node,value) => {if (node.level == 3) {return false}expDtable.value = [value.id]value.children.push({name: '',type: '',describe: '',cekk: true,id: getRandomSixDigitNumber(),children: []})data.value = data.value
}
const defaultProps = {children: 'children',label: 'label',
}
const handleNodeClick = data => {console.log(data)
}
</script><style>
/* 鼠标浮动过的背景颜色 */
.el-tree-node__content:hover {background: #eeeeee;
}
/* 鼠标点击时节点的背景颜色 */
.el-tree-node:focus>.el-tree-node__content {background-color: #eeeeee !important;color: node;
}
/* 鼠标失去焦点时节点背景的颜色 */
.el-tree--highlight-current .el-tree-node.is-current>.el-tree-node__content {background: #eeeeee;
}</style><style lang="scss" scoped>
::v-deep .el-input__wrapper {border-radius: 10px;
}
::v-deep .el-select__wrapper {border-radius: 10px;
}
.mytree {width: 110%;margin-left: -30px;
}
:deep(.mytree) {.el-tree-node {position: relative;padding-left: 10px;margin-top: 10px;// height:50px;background: none;// margin-bottom: 10px;}.el-tree-node__children {padding-left: 10px;// height:100px;// margin-top:10px;}.el-tree-node :last-child:before {height: 38px;}.el-tree > .el-tree-node:before {border-left: none;}.el-tree > .el-tree-node:after {border-top: none;}.el-tree {background: none;}.el-tree-node:before {content: '';left: -4px;position: absolute;right: auto;border-width: 1px;border-left: 1px dashed #1389bc;bottom: 0px;height: 100%;top: -26px;width: 1px;}.el-tree-node:after {content: '';left: -4px;position: absolute;right: auto;border-width: 1px;border-top: 1px dashed #1389bc;height: 20px;top: 12px;width: 18px;}// .el-tree .el-tree-node__expand-icon.expanded {//   -webkit-transform: rotate(0deg);//   transform: rotate(0deg);// }.el-tree .el-icon-caret-right:before {content: '\e723';font-size: 16px;color: #1389bc;position: absolute;left: -20px;top: -8px;}.el-tree .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {content: '\e722';font-size: 16px;color: #1389bc;position: absolute;left: -20px;top: -8px;}.el-tree-node__expand-icon.is-leaf:before {content: '\e722';font-size: 16px;color: #1389bc;position: absolute;left: -20px;top: -8px;}.el-tree-node__content > .el-tree-node__expand-icon {padding: 0;// padding:10px 0;}
}
.start {width: 100%;// height: 400px;background: #eeeeee;margin: 0 auto;border-radius: 5px;.start-input {font-weight: 700;margin: 0 20px;}.table {width: 90%;margin: 0 auto;.table-row {display: flex;justify-content: space-between;font-size: 12px;color: #ccc;}.table-row-one {width: 100%;display: flex;justify-content: space-between;align-items: center;}}.button {margin: 0 20px;}
}
</style>

http://www.ppmy.cn/devtools/133784.html

相关文章

Vagrant 没了 VirtualBox 的话可以配 Qemu

开源虚拟机软件 VirtualBox 从当初不可一世的 Sun 易手到 Oracle 之间&#xff0c;变得不那么被许多公司信任了。之前一直是用 Vagrant 搭配 VirtualBox 在 Mac 下使用 Linux 虚拟机&#xff0c;因为不需要用到 Linux 桌面&#xff0c;用 Vagrant 操作虚拟机非常方便。但现在不…

GitHub Org

运营一个GitHub Org&#xff08;组织&#xff09;是一个复杂但充满价值的过程&#xff0c;它涉及多个方面&#xff0c;包括项目管理、团队协作、代码审查、文档维护、社区建设等。以下是一篇关于如何运营GitHub Org的详细指南&#xff0c;旨在帮助组织者更好地管理和维护其GitH…

量化交易系统开发-实时行情自动化交易-3.4.2.1.Okex K线数据

19年创业做过一年的量化交易但没有成功&#xff0c;作为交易系统的开发人员积累了一些经验&#xff0c;最近想重新研究交易系统&#xff0c;一边整理一边写出来一些思考供大家参考&#xff0c;也希望跟做量化的朋友有更多的交流和合作。 接下来聊聊基于Okex交易所API获取K线数…

系统架构设计师论文:论基于构件的软件开发方法及其应用

从下列的4道试题(试题一至试题四) 中任选1道解答。请在答题纸上的指定位置处将所选择试题的题号框涂黑。若多涂或者未涂题号框,则对题号最小的一道试题进行评分。 试题一 论基于构件的软件开发方法及其应用 基于构作的软件开发 (Component-Based Software Development,CBSD…

第23天Linux下常用工具(二)

目录 第四章 GDB调试工具 4.1gdb的作用 4.2调试代码的流程 4.3gdb的安装 4.4 gdb的使用 第五章 makefile工程管理工具 5.1makefile的作用 5.2makefile的运行 5.3make的安装 5.4makefile的编写方法 5.5makefile的语法 5.6makefile使用示例 第四章 GDB调试工具 4.1g…

【爬虫分享】

爬虫分享 1、爬虫科普 视频发送于2024-10-27 14 _50.mp4 全屏预览下载附件 所以 爬虫 其实是非常 可“刑” 可“铐” 的。 2、逆向方法 算法还原 补环境 无头浏览器&#xff08;自动化&#xff09; rpc 参数生成速度&#xff1a;算法还原 > 补环境 > rpc > 无头…

基于yolov8、yolov5的番茄成熟度检测识别系统(含UI界面、训练好的模型、Python代码、数据集)

摘要&#xff1a;番茄成熟度检测在农业生产及质量控制中起着至关重要的作用&#xff0c;不仅能帮助农民及时采摘成熟的番茄&#xff0c;还为自动化农业监测提供了可靠的数据支撑。本文介绍了一款基于YOLOv8、YOLOv5等深度学习框架的番茄成熟度检测模型&#xff0c;该模型使用了…

王珊数据库系统概论第六版PDF+第五版课后答案+课件

为了保持科学性、先进性和实用性&#xff0c; 编者在第5版教材基础上对全书内容进行了修改、更新和充实。在科学性方面&#xff0c; 编者在系统篇中增加了第9章关系数据库存储管理&#xff0c; 讲解数据库的逻辑与物理组织方式及索引结构。增加这部分内容有助于学生更好地理解关…