Antd 树组件删除节点后选中同级节点

Antd 树组件删除节点后选中同级节点

在写业务组件的时候,遇到了一个需求。当树组件删除选中的节点或者目录后,要选中同级的节点或目录。主要逻辑如下:
当前选中的是节点,若节点是当前目录下的第一个,则选中后一个;若非第一个,则选中前一个。目录同理。
看起来较为简单,但是实际上还是需要用到层层递归去寻找。总的来说,完成这个功能主要用到的方法就是递归和深度搜索。

分析一下,要完成这个功能。首先要获取删除数据的同级数据,然后进行判断被删的数据处于同级数据中的第几个。如果是第一个则向后找,如果非第一个,先向前找,若没有找到则再向后找。查找完当前的数据都没有找到后,则向上一级查找。

拿图分析下,节点0-1-1-0-1被删除后,会选中0-1-1-0-2;0-1-1-0-2被删除后,会选中上一级目录0-1-0中的节点0-1-0-1

在这里插入图片描述

//判断删除后选中哪个数据
  const checkAfterDelete = (
    key: Key,
    type: string,
    dataList?: myTreeNode[],
  ) => {
   
    if (!key) {
   
      return;
    }
    //如果同级下没有或者只有被删的数据则递归向上一级找
    if (!dataList || dataList.length < 2) {
   
      findFatherNode(key, type);
    } else {
   
      if (type) {
   
        //找到删除的节点是当前同级中的第几个
        let index = -1;
        dataList.map((item, itemIndex) => {
   
          if (item.key === key) {
   
            index = itemIndex;
          }
        });
        //被删的是第一个就向后找
        if (index === 0) {
   
          !loopAfterBroTree(index + 1, dataList, type)
            ? findFatherNode(dataList[index].key, type)
            : '';
        } else if (index > 0) {
   
          //被删的是中间,先向上找,找不到再向下
          !loopBeforeBroTree(index - 1, dataList, type)
            ? !loopAfterBroTree(index + 1, dataList, type)
              ? findFatherNode(dataList[index].key, type)
              : ''
            : '';
        }
      }
    }
  };

  //递归查找上一级
  const findFatherNode = (key: Key, type: string) => {
   
    const parentKey = getParentKey(key, remainData!);
    const brotherNodes = getBrotherNode(data, parentKey);
    //被删的是数据是目录,且同级没有目录则选中父级目录
    if (type === 'catalogue') {
   
      let catalogueSelf = brotherNodes?.filter(
        (item) => item.key === parentKey,
      );
      findFirstNode(catalogueSelf!, type)
        ? ''
        : checkAfterDelete(parentKey, type, brotherNodes);
    } else {
   
      checkAfterDelete(parentKey, type, brotherNodes);
    }
  };
向前查找的方法
const loopBeforeBroTree = (
    index: number,
    brotherNode: myTreeNode[],
    type: string,
  ): boolean => {
   
    if (index >= 0) {
   
      if (findFirstNode([brotherNode[index]], type)) {
   
        return true;
      } else {
   
        let result = loopBeforeBroTree(index - 1, brotherNode, type);
        //找到则停止
        if (result) return result;
      }
    }
    return false;
  };
向后查找的方法
  const loopAfterBroTree = (
    index: number,
    brotherNode: myTreeNode[],
    type: string,
  ): boolean => {
   
    if (index < brotherNode.length) {
   
      if (findFirstNode([brotherNode[index]], type)) {
   
        return true;
      } else {
   
        let result = loopAfterBroTree(index + 1, brotherNode, type);
        //找到则停止
        if (result) return result;
      }
    }
    return false;
  };
完整代码
import React, {
    Key, useEffect, useState } from 'react';
import {
    Tree } from 'antd';
import 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值