el-tree 更换图标懒加载

433 阅读1分钟
<template>
  <el-tree
    :props="props"
    :load="loadNode"
    :node-key="nodeKey"
    @node-click="nodeClick"
    :expand-on-click-node="false"
    ref="refCom"
    lazy>
  </el-tree>
</template>
<script>
  export default {
    props: {
      // 取哪一个字段展示
      treeName: {
        type: String,
        default: 'name'
      },
      nodeKey: {
        type: String,
        default: 'id'
      },
      nodeLeaf: {
        type: String,
        default: 'leaf'
      },
    },
    data() {
      return {
        props: {
          label: this.treeName,
          isLeaf: this.nodeLeaf
        },
        currentKey: '', //当前点击获取的key值
      }
    },
    methods: {
      loadNode (node, resolve) {
        this.$emit('treeBack', node, resolve)
      },
      nodeClick(data, node) {
        if (this.currentKey === this.$refs.refCom.getCurrentKey()) {
          this.$refs.refCom.setCurrentKey(data[this.nodeKey])
          this.currentKey = ''
        } else {
          this.currentKey = data[this.nodeKey]
        }
        this.$emit('nodeClick', data, node)
      },
      refreshNode(type='childNode', data) {
        let currentData = this.$refs.refCom.getCurrentNode()
        if(!currentData) currentData = data
        let node = this.$refs.refCom.getNode(currentData[this.nodeKey])
        if(type === 'childNode') {
          /** 第一种方法,获取了这个点击的node节点,然后修改这个节点下面的数据 可以使用下面这种 */
          node.loaded = false
          node.expand()
          // node.loadData() // 这个方法也是可以的
        } else {
          /** 删除或者更新当前选中的节点时, 更新选中的父级节点 */
          node.parent.loaded = false
          node.parent.expand()
          // node.parent.loadData()
        }
      }
    }
  };
</script>
<style lang="scss" scoped>
  // 树样式
  .el-tree{
    .el-tree-node__content{
      background-color: #fff;
      padding-left: 0 !important;
    }
    .el-tree-node__content:hover{
      background-color: lightpink;
    }
    .el-tree-node.is-current>.el-tree-node__content {
      background-color: lightpink !important;
    }
    .el-tree-node__children{
      padding: 0;
      margin: 0;
      width: 100%;
      overflow: inherit;
      .el-tree-node{
        height: auto !important;
        padding-left: 15px;
        border-left: 1px dashed #d7d7d7;
        margin-left: 12px;
        position: relative;
        background-color: #fff;
        &__label {
          overflow: hidden;
          text-overflow: ellipsis;
        }
        &::before{
          content: '';
          position: absolute;
          width: 15px;
          height: 15px;
          left: 0px;
          top: -3px;
          border-bottom: 1px dashed #d7d7d7;
          z-index: inherit;
        }
        &:last-child{
          border-left: 0px;
          position: relative;
          &::before{
            content: '';
            position: absolute;
            width: 15px;
            height: 15px;
            left: 0px;
            top: -3px;
            border-left: 1px dashed #d7d7d7;
            border-bottom: 1px dashed #d7d7d7;
            z-index: inherit;
          }
        }
      }
    }
    /deep/ .el-tree-node__expand-icon {
      content: url("../Assets/noleaf.png") !important;
      font-size: 16px;
    }
    /deep/ .el-tree-node__expand-icon.is-leaf {
      content: url("../Assets/leaf.png") !important;
      font-size: 16px;
    }
    /deep/ .el-tree-node__expand-icon.expanded {
      content: url("../Assets/leaf.png") !important;
      font-size: 16px;
      transform: rotate(0deg) !important;
    }
  }
</style>