element Tree 树形控件 高亮默认

需求:

  1. 进入页面,默认选中需要的节点,并展开+做出高亮效果,其他时候点击箭头图标才展开。

  2. input框搜索树节点

代码:

html 复制代码
        <el-aside
          class="aside flex-shrink-0 bg-white"
          style="width:300px;height: 100%;"
        >
          <div>
            <el-input
              v-model="filterText"
              class="filterInput"
              placeholder="请输入单位名称关键字"
              clearable
            />
            <div class="problem-orgTree">
              <el-tree
                ref="deptTree"
                :data="deptOptions"
                :props="defaultProps"
                accordion
                :filter-node-method="filterNode"
                highlight-current
                node-key="id"
                :expand-on-click-node="false"
                :default-expanded-keys="defaultShowKeys"
                @node-click="handleNodeClick"
              />
            </div>
          </div>
        </el-aside>
javascript 复制代码
<script>
export default {
  data() {
    return {
      deptOptions: [],
      defaultProps: {
        children: 'children',
        label: 'label',
      },
      defaultShowKeys: [],
      filterText: '', // 搜索文本
      belongdCompId: '', // 选中的树节点id
    }
  },
  watch: {
    filterText(val) {
      this.$refs.deptTree.filter(val);
    },
  },
  mounted() {
    if (this.$route.query.deptId) {
      this.belongdCompId = this.$route.query.deptId;
    }
  },
  methods: {
    getTreeselect() {
      // 调用接口获取树结构
      treeselectOnlyOrg().then((response) => {
        this.deptOptions = response.data;
        this.$nextTick(() => {
          // 高亮选中的节点
          this.$refs.deptTree.setCurrentKey(this.belongdCompId);
          // 默认展开
          this.defaultShowKeys.push(this.belongdCompId);
        });
      });
    },
    // 筛选
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.belongdCompId = data.id;
    },
  }
}
</script>
<style lang="scss">
.problem-orgTree {
  padding-right: 5px;
  padding-bottom: 10px;
  // 超出换行
  .el-tree {
    .el-tree-node {
      white-space: normal;
      outline: 0;
    }
    .el-tree-node__content {
      height: 100% !important;
      word-break: break-all !important;
      white-space: pre-wrap !important;
      word-wrap: break-word !important;
    }
    .el-tree-node__label {
      word-break: break-all !important;
      white-space: pre-wrap !important;
      word-wrap: break-word !important;
    }
  }
  // 高亮选中
  .el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
    color: #006569;
  }
}

</style>
相关推荐
酒鼎14 分钟前
学习笔记(12-02)事件循环 - 实战案例 —⭐
前端·javascript
小恰学逆向24 分钟前
【爬虫JS逆向之旅】某球网参数“md5__1038”逆向
javascript·爬虫
竹林81825 分钟前
从“连接失败”到丝滑登录:我用 ethers.js v6 搞定 MetaMask 钱包连接的全过程
前端·javascript
三原1 小时前
超级好用的三原后台管理v1.0.0发布🎉(Vue3 + Ant Design Vue + Java Spring Boot )附源码
java·vue.js·开源
前端那点事1 小时前
前端必看!JS高频实用案例(单行代码+实战场景+十大排序)
javascript
之歆1 小时前
RBAC权限模型设计与实现深度解析
vue.js
前端Hardy1 小时前
前端开发效率翻倍:15个超级实用的工具函数,直接复制进项目(建议收藏)
前端·javascript·面试
前端Hardy1 小时前
Vue 项目必备:10 个高频实用自定义指令,直接复制即用(Vue2 / Vue3 通用)
前端·javascript·vue.js
h_jQuery2 小时前
uniapp使用canvas实现逐字书写任意文字内容,后合成一张图片提交
前端·javascript·uni-app
懒大王95272 小时前
Vue 2 与 Vue 3 的区别
前端·javascript·vue.js