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>
相关推荐
huangdong_7 小时前
淘宝商品SKU图自动分类技术深度解析:从DOM解析到智能归档
开发语言·javascript·ecmascript
xiaofeichaichai10 小时前
ES 新特性九年速览:从 ES2016 到 ES2024
前端·javascript·es6
放下华子我只抽RuiKe511 小时前
FastAPI 全栈后端(四):认证与授权
开发语言·前端·javascript·python·深度学习·react.js·fastapi
如果超人不会飞11 小时前
WebMCP:当浏览器学会和 AI「说人话」,你的网页就成了智能体的游乐场
javascript
_codeOH11 小时前
Vue 3 vs React 19:框架还在卷,核心原理就这些
前端·vue.js
整点可乐11 小时前
cesium实现全景图加载
javascript·cesium
dualven_in_csdn12 小时前
一键起飞调用示例
android·java·javascript
英勇无比的消炎药12 小时前
新手必看玩转TinyRobot一定要避开这些坑
前端·vue.js
meilindehuzi_a12 小时前
通俗易懂掌握树与二叉树:定义、核心概念与JS实现遍历
javascript·ecmascript
胡志辉13 小时前
深入浅出理解浏览器事件循环:从一道输出题讲到 Chrome 源码
前端·javascript·面试