react输入框检索树形(tree)结构

xml 复制代码
input搜索框搜索树形子级内容

 1. input框输入搜索内容
 2. 获取tree结构数据
 3. 与tree匹配输入的内容,tree是多维数组,一级一级的对比输入的内容是否匹配,用forEach循环遍历数据,匹配不到在往下找,直到找到为null ,返回


typescript 复制代码
//tree子级
const childSerch = (res: any, val: any) => {
    res?.map((itemChilf: { meterName: any }, index) => {
      if (itemChilf?.meterName === val) {
        dataName.push(itemChilf);
        return dataName;
      } else {
        itemChilf?.children?.map((itemChilf1: { meterName: any }) => {
          if (itemChilf1.meterName === val) {
            dataName.push(itemChilf);
            return dataName;
          }
          return childSerch(itemChilf1?.children, val);
        });
        return dataName;
      }
    });
    return dataName;
  };

const fetchDeptList = async (val: any) => {
    try {
      dataName = [];
      //获取tree列表
      await getDeptTree().then((res: any) => {
        //判断是输入的那个值
        if (val?.formType.energyMediumId != undefined) {
          console.log(123456);
          let data: any[] = [];
          //循环tree
          res.forEach((element: any) => {
            if (element.id === val.formType.energyMediumId) {
              data.push(element);
              if (val?.formType?.meterName) {
                data = childSerch(element.children, val?.formType.meterName);
              }
            }
          });
          console.log(data);
          setTreeData(data);
          exKeys.push(data[0].children[0].id);
          setExpandedKeys(exKeys);
          props.onSelect(data[0].children[0]);
          return;
        } else if (val?.formType.meterName != undefined) {
          let data = [];
          data = childSerch(res, val?.formType.meterName);

          setTreeData(data);
          exKeys.push(data[0].children[0].id);
          setExpandedKeys(exKeys);
          props.onSelect(data[0].children[0]);
          return;
        }

        // console.log(val.formType.energyMediumId)
        setTreeData(res);
        // renderTreeNodes(res[0]);
        exKeys.push(res[0].children[0].id);
        setExpandedKeys(exKeys);
        props.onSelect(res[0].children[0]);
      });
      // hide();
      return true;
    } catch (error) {
      // hide();
      return false;
    }
  };
相关推荐
San301 小时前
深度解析 React 组件化开发:从 Props 通信到样式管理的进阶指南
前端·javascript·react.js
AAA阿giao1 小时前
深度解析 React 项目架构:从文件结构到核心 API 的全面拆解
前端·javascript·react.js
昨晚我输给了一辆AE861 小时前
关于 react-hook-form 的 isValid 在有些场景下的值总是 false 问题
前端·react.js
Mintopia4 小时前
🏗️ B端架构中的用户归因与埋点最佳实践
前端·react.js·架构
Blossom.1184 小时前
大模型AI Agent实战:ReAct框架从零实现与金融研报分析系统
人工智能·学习·react.js·stable diffusion·金融·aigc·知识图谱
南山安4 小时前
React 学习:父传子的单项数据流——props
javascript·react.js·前端框架
soda_yo5 小时前
React哲学:保持组件纯粹 哈气就要哈得纯粹
前端·react.js·设计
Bigger5 小时前
Tauri (22)——让 `Esc` 快捷键一层层退出的分层关闭方案
前端·react.js·app
T___T5 小时前
从入门到实践:React Hooks 之 useState 与 useEffect 核心解析
前端·react.js·面试
小肥宅仙女5 小时前
React + ECharts 多图表联动实战:从零实现 Tooltip 同步与锁定功能
前端·react.js·echarts