vue-org-tree搜索到对应项高亮展开

news/2024/7/8 6:14:04 标签: vue.js, 前端, javascript

效果图:

代码:

<template>
  <div class="AllTree">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="formInline.user" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <div ref="treeContainer" class="tree-container">
      <org-tree :data="data" :horizontal="true" name="test" :label-class-name="labelClassName" collapsable
        @on-expand="onExpand" @on-node-click="clickTree" ref="orgTree" :label-width="150"
        :line-style="{ stroke: '#ccc', strokeWidth: 2 }">
      </org-tree>
    </div>
    <!-- 俱乐部信息弹窗 -->
    <el-drawer title="俱乐部信息" :visible.sync="drawer" :before-close="handleClose">
      <span>我来啦!</span>
    </el-drawer>
  </div>
</template>

 初始化定义数据:

javascript">import OrgTree from 'vue2-org-tree';
import 'vue2-org-tree/dist/style.css';

export default {
  components: {
    orgTree: OrgTree
  },
  data() {
    return {
      formInline: {
        user: ''
      },
      id: null,
      drawer: false,
      data:{},
      data1: {
        id: 0,
        label: "XX俱乐部",
        children: [
          {
            id: 2,
            label: "xx合伙人",
            children: [
              {
                id: 5,
                label: "研发-前端",
                children: [
                  {
                    id: 55,
                    label: "前端1",
                    children: [
                      {
                        id: 555,
                        label: "前端1111",
                      },
                      {
                        id: 556,
                        label: "前端2222",
                      },
                      {
                        id: 557,
                        label: "前端3333",
                        children: [{
                          id: 5557,
                          label: "前端11111",
                        },
                        {
                          id: 5558,
                          label: "前端22222",
                        },
                        {
                          id: 5559,
                          label: "前端33333",
                        },
                        {
                          id: 5560,
                          label: "前端44444",
                        },
                        {
                          id: 5561,
                          label: "前端55555",
                        }]
                      },
                      {
                        id: 558,
                        label: "前端3333",
                      }
                    ]
                  },
                  {
                    id: 56,
                    label: "前端2"
                  },
                  {
                    id: 57,
                    label: "前端3"
                  },
                  {
                    id: 58,
                    label: "前端4"
                  }
                ]
              },
              {
                id: 6,
                label: "研发-后端"
              },
              {
                id: 9,
                label: "UI设计"
              },
              {
                id: 10,
                label: "产品经理"
              }
            ]
          },
          {
            id: 3,
            label: "销售部",
            children: [
              {
                id: 7,
                label: "销售一部",
                children: [
                  {
                    id: 78,
                    label: "销售一部组长",
                    children: [{
                      id: 788,
                      label: "销售一部A",
                    },
                    {
                      id: 789,
                      label: "销售一部B",
                    },
                    {
                      id: 790,
                      label: "销售一部C",
                    },
                    {
                      id: 791,
                      label: "销售一部D",
                    }]
                  }
                ]
              },
              {
                id: 8,
                label: "销售二部"
              }
            ]
          },
          {
            id: 4,
            label: "财务部"
          },
          {
            id: 9,
            label: "HR人事"
          }
        ]
      },
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      zoom: 1,
      BasicSwich: false,
      collapsable: true,
      labelClassName: "org-bg-white",
      clickTimeout: null
    };
  },

 搜索事件定义方法:

javascript"> created() {
    this.getTreeData();
  },
  methods: {
    //获取节点数据
    getTreeData(){
      this.data=this.data1
      this.expandChange();
    },
    renderContent(h, data) {
      return data.label;
    },
    // 树状结构折叠打开
    onExpand(e, data) {
      if ("expand" in data) {
        data.expand = !data.expand;
        if (!data.expand && data.children) {
          this.collapse(data.children);
        }
      } else {
        this.$set(data, "expand", true);
      }
    },
    collapse(list) {
      var _this = this;
      list.forEach(function (child) {
        if (child.expand) {
          child.expand = false;
        }
        child.children && _this.collapse(child.children);
      });
    },
    expandChange() {
      this.toggleExpand(this.data, true);
    },
    toggleExpand(data, val) {
      var _this = this;
      if (Array.isArray(data)) {
        data.forEach(function (item) {
          _this.$set(item, "expand", val);
          if (item.children) {
            _this.toggleExpand(item.children, val);
          }
        });
      } else {
        this.$set(data, "expand", val);
        if (data.children) {
          _this.toggleExpand(data.children, val);
        }
      }
    },
    // 搜索事件
    handleSearch() {
      const isLabelFound = this.findLabel(this.data, this.formInline.user.trim());
      console.log('isLabelFound',isLabelFound)

    },
    findLabel(node, targetLabel) {
       // 清除先前设置的背景色
      this.clearHighlight();
      const searchInChildren = (node) => {
        if (node.id == 0) {
          node.expand = true          
        }
        //检查当前节点
        if (node.label === targetLabel) {
          this.id = node.id;
          node.expand = true;
          this.highlightLabel(targetLabel, "#46a6ff"); // 设置背景色为 pink
          return true;//找到目标节点
        }
        //递归搜索子节点
        if (node.children) {
          for (let child of node.children) {
            if (searchInChildren(child)) {
              child.expand = true
              return true;//如果在子节点找到目标节点,直接返回true
            }
          }
        }
        return false;//当前节点及其子节点都未找到目标节点
      }
      const found = searchInChildren(node)
      if (!found) {
        this.$message.warning('未找到该项')
      }
      return found ? this.id : false
    },
    highlightLabel(targetLabel, color) {
      // 异步更新,等待 DOM 渲染完成后再操作
      setTimeout(() => {
        let orgTreeList = document.getElementsByClassName('org-tree-node-label-inner');
        for (let i = 0; i < orgTreeList.length; i++) {
          if (orgTreeList[i].innerText === targetLabel) {
            orgTreeList[i].style.backgroundColor = color;
            break; // 找到第一个匹配的节点后即可退出循环
          }
        }
      }, 0);
    },

    clearHighlight() {
      let orgTreeList = document.getElementsByClassName('org-tree-node-label-inner');
      for (let i = 0; i < orgTreeList.length; i++) {
        orgTreeList[i].style.backgroundColor = ""; // 清除背景色
      }},
    //点击事件
    clickTree(e, data) {
      if (this.clickTimeout) {
        // 如果存在单击事件的计时器,则视为双击事件
        clearTimeout(this.clickTimeout);
        this.$router.push({
          path: "/dataVisualization/recommendedLinksDetail",
          query: {
            id: data.id,
          },
        });
      } else {
        // 否则,启动单击事件计时器
        this.clickTimeout = setTimeout(() => {
          this.drawer = true
          this.clickTimeout = null; // 清除计时器
        }, 500); // 200毫秒内判断是否双击
      }
      // const depth = this.getNodeDepth(data);
      // if (depth === 1) {
      //   // 第一层节点,显示弹窗或其他操作
      //   this.drawer = true
      // } else {
      //   // this.$router.push({
      //   //   path: "/dataVisualization/recommendedLinksDetail",
      //   //   query: {
      //   //     id: data.id,
      //   //   },
      //   // });
      //   console.log(111)
      // }

    },
    getNodeDepth(node, depth = 0) {
      // 递归计算节点的深度(层级)
      console.log(node)
      console.log(node.parent)
      if (node.parent) {
        return this.getNodeDepth(node.parent, depth + 1);
      }
      return depth;
    },
    handleClose() {
      this.drawer = false
    }
  }
}


http://www.niftyadmin.cn/n/5536656.html

相关文章

http 状态码主要有哪些?【面试】

HTTP 协议在互联网上用得特别广&#xff0c;在浏览网页的时候经常会碰到它的状态码。这状态码其实就是服务器给客户端请求的一个回应&#xff0c;通过它我们就能知道请求处理得怎么样了。 一、HTTP 协议的状态码类别 &#x1f4f1; 1xx &#xff0c;这是信息性状态码&#xf…

Emacs有什么优点,用Emacs写程序真的比IDE更方便吗?

Emacs 是一个功能强大的文本编辑器&#xff0c;它在开发者和程序员中非常受欢迎&#xff0c;主要优点包括&#xff1a; 可定制性&#xff1a;Emacs 允许用户通过 Lisp 编程语言来自定义编辑器的行为和界面&#xff0c;几乎可以修改任何方面。扩展性&#xff1a;拥有大量的扩展…

ArcGIS Pro SDK (七)编辑 10 捕捉

ArcGIS Pro SDK &#xff08;七&#xff09;编辑 10 捕捉 文章目录 ArcGIS Pro SDK &#xff08;七&#xff09;编辑 10 捕捉1 配置捕捉 - 打开或关闭捕捉2 配置捕捉 - 应用程序捕捉模式3 配置捕捉 - 图层捕捉可捕捉性4 配置捕捉 - 图层捕捉模式5 配置捕捉 - 组合示例6 捕捉选项…

6月30日功能测试Day10

3.4.4拼团购测试点 功能位置&#xff1a;营销-----拼团购 后台优惠促销列表管理可以添加拼团&#xff0c;查看拼团活动&#xff0c;启动活动&#xff0c;编辑活动&#xff0c;删除活动。 可以查看拼团活动中已下单的订单以状态 需求分析 功能和添加拼团 商品拼团活动页 3…

无线麦克风哪个好?一文看懂无线领夹麦克风哪个品牌音质最好

如今无论是视频直播或者是个人Vlog都越来越受欢迎了&#xff0c;要想做好内容&#xff0c;除了画面之外&#xff0c;声音效果同样重要。声音的录制成功与否&#xff0c;也会直接影响整个作品的整体水平&#xff0c;要想录的声音清晰&#xff0c;有专业级录制效果&#xff0c;必…

Spring Boot 文件上传和下载指南:从基础到进阶

文章目录 引言1. 环境配置2. 文件上传2.1 配置文件上传路径2.2 创建上传服务2.3 创建上传控制器 3. 文件下载3.1 创建下载服务3.2 创建下载控制器 4. 前端页面4.1 文件上传页面4.2 文件下载页面 5. 技术分析结论 &#x1f389;欢迎来到SpringBoot框架学习专栏~ ☆* o(≧▽≦)o …

一周小计(1):实习初体验

实习的第一周&#xff0c;从最开始的配环境做好准备工作&#xff0c;到拉项目熟悉项目&#xff0c;然后自己去写需求&#xff0c;每一步都有很大收获&#xff0c;以下是个人这几天的记录与感想。&#xff08;这个其实是我写的周报&#xff0c;记录的也不太详细&#xff0c;有些…

10分钟完成微信JSAPI支付对接过程-JAVA后端接口

引入架包 <dependency><groupId>com.github.javen205</groupId><artifactId>IJPay-WxPay</artifactId><version>${ijapy.version}</version></dependency>配置类 package com.joolun.web.config;import org.springframework.b…