1.安装

	npm install --save-dev less less-loader
	npm install --save-dev vue2-org-tree

#因为需要引入less的样式 所以需要安装less
2.main.js引入

import Vue2OrgTree from 'vue2-org-tree';
Vue.use(Vue2OrgTree)

3.在页面中直接调用

   <vue2-org-tree :data="orginList" />
	//data绑定的数据
	数据格式
		 {
        label: "应急救援管理办公室",
        id: 1,
        children: [
          {
            label: "总指挥",
            id: 2,
            children: [
              {
                label: "副总指挥",
                id: 3,
                children: [
                  {
                    label: "应急救援组",
                    id: 5,
                  },
                  {
                    label: "通讯联络组",
                    id: 6,
                  },
                  {
                    label: "医疗救护组",
                    id: 7,
                  },
                  {
                    label: "车辆调度组",
                    id: 8,
                  },
                ],
              },
              {
                label: "副总指挥",
                id: 4,
                children: [
                  {
                    label: "疏导境界组",
                    id: 10,
                  },
                  {
                    label: "事故调查组",
                    id: 11,
                  },
                  {
                    label: "后勤保障组",
                    id: 12,
                  },
                  {
                    label: "善后处理组",
                    id: 13,
                  },
                ],
              },
            ],
          },
        ],
      },

tips:如果需要修改label名称

:render-content="renderContent" //这个属性 
renderContent(h, data) { return data.taskName },

4.在style中引入样式,必须引入 引入全局样式 lang的值是less 样式如下 可以根据实际项目需要进行修改

@colors:#1FAAEB;
.org-tree-container {
  display: inline-block;
  padding: 15px;
  // margin: 0 auto;
  
  position: absolute;
  width: 975px;
  // height: 500px;
    top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  margin-top: 50px;
}

.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 .5s;
  }
}
.org-tree-node-label {
  position: relative;
  display: inline-block;

  .org-tree-node-label-inner {
    cursor: pointer;
    padding: 30px 20px;
    text-align: center;
    border-radius: 3px;
    box-shadow: 0 1px 5px rgba(0, 0, 0, .7);
    font-size: 16px;
  }
}
.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 @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 solid @colors;
  }

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

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

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

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

  &:after {
    left: 50%;
    border-left: 7px solid @colors;
    // background: url('../image/jt.png') 0 0 no-repeat;
    // background-size: 100% 100%;
    // background-position-x: -25px;

  }

  &:not(:first-child):before,
  &:not(:last-child):after {
    border-top: 7px solid @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 solid @colors;
  }
}
.org-tree > .org-tree-node {
  padding-top: 0;

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

  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 50%;
    height: 50px;
    border-right: 7px solid @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 solid @colors;
    }

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

    &:not(:only-child):after {
      border-top: 1px solid @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 solid @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 solid @colors;
    }

    &:after {
      display: none;
    }

    & > .org-tree-node {
      display: block;
    }
  }
}


Logo

前往低代码交流专区

更多推荐