使用 vue2-org-tree 封装新增、编辑、删除节点 组织结构组件

近项目要求动态组织架构,如图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/3bdd1eb6bcf54ee486f3eff38799bb1a.png

1、首先安装插件vue2-org-tree

npm i vue2-org-tree
npm i html2canvas

html2canvas 这个项目没有用只是安装引用了
2、安装less: 官方文档引入样式的时候是导入的 less 文件

npm install --save-dev less less-loader

3、全局main.js引入

import Vue2OrgTree from 'vue2-org-tree'

Vue.use(Vue2OrgTree)

4、在当前 vue 文件的 style 标签设置 lang=“less”,并且将 scoped 属性删除,否则样式不起作用,所以新建less文件

@colors:#8b8d8d;
.org-tree-container {
  display: inline-block;
  padding: 15px;
  background-color: #fff;
  width: 100%;
  /*定位*/
  // position: relative;  
  // top: 0;
  // left: 0;
}

.org-tree {
  display: inline-block;
  display: table;
  text-align: center;
  margin: auto;
  &:before, &:after {
    content: '';
    display: table;
  }

  &:after {
    clear: both;
  }
}

.org-tree-node,
.org-tree-node-children {
  position: relative;
  margin: 0;
  padding: 0;
  list-style-type: none;

  &:before, &:after {
    transition: all .35s;
  }
}
.org-tree-node-label {
  position: relative;
  display: inline-block;

  .org-tree-node-label-inner {
    padding: 10px 15px;
    text-align: center;
    border-radius: 3px;
    box-shadow: 0 1px 5px rgba(0, 0, 0, .15);
  }
}
.org-tree-node-btn {
  position: absolute;
  top: 100%;
  left: 50%;
  width: 20px;
  height: 20px;
  z-index: 10;
  margin-left: -11px;
  margin-top: 9px;
  background-color: #fff;
  border: 1px dashed @colors;
  border-radius: 50%;
  box-shadow: 0 0 2px rgba(0, 0, 0, .15);
  cursor: pointer;
  transition: all .35s ease;

  &:hover {
    background-color: #e7e8e9;
    transform: scale(1.15);
  }

  &:before, &:after {
    content: '';
    position: absolute;
  }

  &:before {
    top: 50%;
    left: 4px;
    right: 4px;
    height: 0;
    border-top: 1px dashed @colors;
  }

  &:after {
    top: 4px;
    left: 50%;
    bottom: 4px;
    width: 0;
    // border-left: 1px dashed @colors;
  }

  &.expanded:after {
    border: none;
  }
}
.org-tree-node {
  padding-top: 20px;
  display: table-cell;
  vertical-align: top;

  &.is-leaf, &.collapsed {
    padding-left: 10px;
    padding-right: 10px;
  }

  &:before, &:after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 50%;
    height: 19px;
  }

  &:after {
    left: 50%;
    border-left: 1px dashed @colors;
  }

  &:not(:first-child):before,
  &:not(:last-child):after {
    border-top: 1px dashed @colors;
  }

}
.collapsable .org-tree-node.collapsed {
  padding-bottom: 30px;

  .org-tree-node-label:after {
    content: '';
    position: absolute;
    top: 100%;
    left: 0;
    width: 50%;
    height: 20px;
    border-right: 1px dashed @colors;
  }
}
.org-tree > .org-tree-node {
  padding-top: 0;

  &:after {
    border-left: 0;
  }
}
.org-tree-node-children {
  padding-top: 20px;
  display: table;

  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 50%;
    height: 20px;
    border-right: 1px dashed @colors;
    border-left: none;
  }

  &:after {
    content: '';
    display: table;
    clear: both;
  }
}

.horizontal {
  .org-tree-node {
    // display: flex;
    // flex-direction: row;
    // justify-content: flex-start;
    // align-items: center;
    display: table-cell;
    float: none;
    padding-top: 0;
    padding-left: 20px;

    &.is-leaf, &.collapsed {
      padding-top: 10px;
      padding-bottom: 10px;
    }

    &:before, &:after {
      width: 19px;
      height: 50%;
    }

    &:after {
      top: 50%;
      left: 0;
      border-left: 0;
    }

    &:only-child:before {
      top: 1px;
      border-bottom: 1px dashed @colors;
    }

    &:not(:first-child):before,
    &:not(:last-child):after {
      border-top: 0;
      border-left: 1px dashed @colors;
    }

    &:not(:only-child):after {
      border-top: 1px dashed @colors;
    }

    .org-tree-node-inner {
      display: table;
    }

  }

  .org-tree-node-label {
    display: table-cell;
    vertical-align: middle;
  }

  &.collapsable .org-tree-node.collapsed {
    padding-right: 30px;

    .org-tree-node-label:after {
      top: 0;
      left: 100%;
      width: 20px;
      height: 50%;
      border-right: 0;
      border-bottom: 1px dashed @colors;
    }
  }

  .org-tree-node-btn {
    top: 50%;
    left: 100%;
    margin-top: -11px;
    margin-left: 9px;
  }

  & > .org-tree-node:only-child:before {
    border-bottom: 0;
  }

  .org-tree-node-children {
    // display: flex;
    // flex-direction: column;
    // justify-content: center;
    // align-items: flex-start;
    display: table-cell;
    padding-top: 0;
    padding-left: 20px;

    &:before {
      top: 50%;
      left: 0;
      width: 20px;
      height: 0;
      border-left: 0;
      border-top: 1px dashed 
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
好的,我会尽力回答你的问题。 首先,Element Plus 是一个基于 Vue 3 的 UI 组件库,而 TypeScript 则是一种强类型的 JavaScript 扩展语言。在 Vue 3 中,我们可以通过 Composition API 来编写组件。 针对你的需求,我们可以封装一个 Tree 组件,代码如下: ```typescript <template> <el-tree :data="data" :props="props" :expand-on-click-node="false" @node-click="handleNodeClick"> <slot name="default" v-bind="{ node, data }" /> </el-tree> </template> <script lang="ts"> import { defineComponent, PropType } from 'vue' import { ElTree } from 'element-plus' interface TreeNode { label: string children?: TreeNode[] } interface Props { data: TreeNode[] props: Record<string, any> } export default defineComponent({ name: 'MyTree', components: { ElTree }, props: { data: { type: Array as PropType<TreeNode[]>, required: true }, props: { type: Object as PropType<Record<string, any>>, default: () => ({ label: 'label', children: 'children' }) }, }, setup(props: Props, { emit }) { const handleNodeClick = (data: any, node: any, instance: any) => { emit('node-click', data, node, instance) } return { handleNodeClick, } }, }) </script> ``` 在这个组件中,我们使用了 Element Plus 的 `el-tree` 组件,并使用了 `slot` 来插入自定义节点内容。通过 TypeScript 的类型定义,我们可以确保传入的 `data` 和 `props` 属性是正确的格式。同时,我们为 `node-click` 事件添加了一个自定义的处理函数。 使用这个组件的方式非常简单: ```vue <template> <my-tree :data="data" @node-click="handleNodeClick"> <template #default="{ node, data }"> <span>{{ node.label }}</span> </template> </my-tree> </template> <script lang="ts"> import { defineComponent } from 'vue' import MyTree from '@/components/MyTree.vue' interface TreeNode { label: string children?: TreeNode[] } export default defineComponent({ components: { MyTree }, setup() { const data: TreeNode[] = [ { label: '一级 1', children: [ { label: '二级 1-1', children: [ { label: '三级 1-1-1' }, { label: '三级 1-1-2' }, ], }, { label: '二级 1-2', children: [ { label: '三级 1-2-1' }, { label: '三级 1-2-2' }, ], }, ], }, { label: '一级 2', children: [ { label: '二级 2-1', children: [ { label: '三级 2-1-1' }, { label: '三级 2-1-2' }, ], }, { label: '二级 2-2', children: [ { label: '三级 2-2-1' }, { label: '三级 2-2-2' }, ], }, ], }, ] const handleNodeClick = (data: any, node: any, instance: any) => { console.log('node-click', data, node, instance) } return { data, handleNodeClick, } }, }) </script> ``` 在这个示例中,我们使用了自定义的 `MyTree` 组件,并在 `slot` 中渲染了节点的标签文本。 希望这个示例能够帮助到你。如果你还有其他问题,可以随时问我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

左手牵♪♡右手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值