绘制组织架构 vue2-org-tree
dearweb 发布:2022-03-02 16:49:56阅读:使用vue2-org-tree插件实现组织架构绘制
1.下载
npm i vue2-org-tree npm install --save-dev less less-loader
下载less防止样式报错
2.引用
main.js中:
import Vue2OrgTree from 'vue2-org-tree' import 'vue2-org-tree/dist/style.css' Vue.use(Vue2OrgTree)
在使用的vue文件中
<vue2-org-tree :data="data" label-class-name="orglabel" /> data: { id: 0, label: 'XXX科技有限公司', children: [ { id: 2, label: '产品研发部', children: [ { id: 5, label: '研发-前端', }, { id: 6, label: '研发-后端', }, { id: 9, label: 'UI设计', }, { id: 10, label: '产品经理', }, ], }, { id: 3, label: '销售部', children: [ { id: 7, label: '销售一部', }, { id: 8, label: '销售二部', }, ], }, { id: 4, label: '财务部', }, ], },
4.自定义样式:
上部分html中使用了orglabel 这个类名修改组织架构图样式
需要注意的点:样式不能在当前组件中style scoped设置下生效
另外:需要引入less文件确定整个树的样式,否则不会出现组织架构的树形样式
在使用到vue2-org-tree的vue文件引入@import ‘@/assets/css/org-tree.less’;
.org-tree-container { display: inline-block; padding: 15px; text-align: center; } .org-tree { // display: inline-block; display: table; text-align: center; &: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 { 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 solid #ccc; 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 solid #ccc; } &:after { top: 4px; left: 50%; bottom: 4px; width: 0; border-left: 1px solid #ccc; } &.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 solid #ddd; } &:not(:first-child):before, &:not(:last-child):after { border-top: 1px solid #ddd; } } .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 solid #ddd; } } .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: 50%; width: 0; height: 20px; border-left: 1px solid #ddd; } &: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 solid #ddd; } &:not(:first-child):before, &:not(:last-child):after { border-top: 0; border-left: 1px solid #ddd; } &:not(:only-child):after { border-top: 1px solid #ddd; } .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 solid #ddd; } } .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 solid #ddd; } &:after { display: none; } & > .org-tree-node { display: block; } } }
以上可以根据实际的情况进行修改,本样式比较符合上图的组织架构显示图片。
另外本插件还有默认展开,点击节点等相关配置。
另附上vue2-org-tree的git地址 https://www.npmjs.com/package/vue2-org-tree
小礼物走一波,支持作者
赏还没有人赞赏,支持一波吧