antd tree 拖拽 功能

Ⅰ- 壹 - 功能展示和使用需求

需求描述

同级拖拽,不能不同父级的进行拖拽

功能展示

在这里插入图片描述

Ⅱ - 贰 - 封装代码

import React, { useState } from "react";
import { Tree } from "antd";
import { cloneDeep } from "lodash";

const dataA = [
  {
    title: "Branch 1",
    key: "1",
    parentId: '0',
    leve: 0,
    children: [
      {
        title: "Branch 1.1",
        key: "1.1",
        parentId: '1',
        leve: 1,
      },
      {
        title: "Branch 1.2",
        key: "1.2",
        parentId: '1',
        leve: 1,
      },
      {
        title: "Branch 1.3",
        key: "1.3",
        parentId: '1',
        leve: 1,
      },
    ],
  },
  {
    title: "Branch 2",
    key: "2",
    parentId: '0',
    leve: 0,
    children: [
      {
        title: "Branch 2.1",
        key: "2.1",
        parentId: '2',
        leve: 1,
      },
      {
        title: "Branch 2.2",
        key: "2.2",
        parentId: '2',
        leve: 1,
      },
    ],
  },
  {
    title: "Branch 3",
    key: "3",
    parentId: '0',
    leve: 0,
    children: [
      {
        title: "Branch 3.1",
        key: "3.1",
        parentId: '3',
        leve: 1,
      },
      {
        title: "Branch 3.2",
        key: "3.2",
        parentId: '3',
        leve: 1,
      },
      {
        title: "Branch 3.3",
        key: "3.3",
        parentId: '3',
        leve: 1,
      },
    ],
  },
];

export default function Demo() {
  const [treeData, setTreeData] = useState(dataA);
  const onDrop = (info) => {
    console.log("info", info);
    const dragNode = info.dragNode;
    const dropNode = info.node;
    // const dropKey = info.node.props.eventKey;
    // const dragKey = info.dragNode.props.eventKey;
    // const dropPos = info.node.props.pos.split('-');
    // const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
    // console.log(info, "info");
    // console.log(dropKey, "dropKey", "放置位置同级的上一个元素,当最顶的时候是下一个元素");
    // console.log(dragKey, "dragKey", "拖拽对象");
    // console.log(dropPos, "dropPos","应该没什么用,拿来计算dropPosition = -1的参数而已");
    // console.log(dropPosition, "dropPosition", "-1的时候是最顶的最顶,");
    // console.log(info.dropToGap, "dropToGap", "最外一层的第一个是true,移动到其他树枝的第一个时是false,移动到不是第一个的位置是true");
    // ————————————————
    // 版权声明:本文为CSDN博主「没事下辈子小心点」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    // 原文链接:https://blog.csdn.net/weixin_42357617/article/details/120422339

    if (dropNode.parentId != dragNode.parentId && dropNode.key != dragNode.parentId) {
      console.log('提示---不可以不同父级进行拖拽');
      return
    }

    let clodata = cloneDeep(treeData)
    const loopSplice = (data) => {
      let cIndex = data.findIndex((o: any) => o.key == dragNode.key)
      let cItem = data.find((o: any) => o.key == dragNode.key)
      console.log(cIndex, "===cIndex==");
      console.log(info.dropPosition, "===info.dropPosition==");
      console.log(cIndex >= info.dropPosition ? info.dropPosition : info.dropPosition - 1, "===info.dropPosition  tow==");
      data.splice(cIndex, 1);
      if (!info.dropToGap || info.dropPosition == -1) {
        data.splice(0, 0, cItem);
      } else {
        data.splice(cIndex >= info.dropPosition ? info.dropPosition : info.dropPosition - 1, 0, cItem);
      }
    }
    const loop = (data) => {
      if (dragNode.parentId == 0) {
        loopSplice(data)
        return
      }
      for (const iterator of data) {
        if (iterator.key == dragNode.parentId) {
          loopSplice(iterator.children)
          return
        }
        if (iterator?.children) loop(iterator?.children)
      }
    };
    loop(clodata)
    setTreeData(() => clodata)
  }
  return (
    <div>
      <Tree defaultExpandAll draggable treeData={treeData} onDrop={onDrop} />
    </div>
  );
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
antd Tree控件提供了搜索功能,可以通过设置onSearch属性来实现。 首先,在Tree组件中添加一个Search组件,如下所示: ```jsx import { Tree, Input } from 'antd'; const { Search } = Input; class MyTree extends React.Component { state = { expandedKeys: [], searchValue: '', autoExpandParent: true, }; onExpand = expandedKeys => { this.setState({ expandedKeys, autoExpandParent: false, }); }; onChange = e => { const { value } = e.target; const expandedKeys = dataList .map(item => { if (item.title.indexOf(value) > -1) { return getParentKey(item.key, this.props.treeData); } return null; }) .filter((item, i, self) => item && self.indexOf(item) === i); this.setState({ expandedKeys, searchValue: value, autoExpandParent: true, }); }; render() { const { treeData } = this.props; const { searchValue, expandedKeys, autoExpandParent } = this.state; const loop = data => data.map(item => { const index = item.title.indexOf(searchValue); const beforeStr = item.title.substr(0, index); const afterStr = item.title.substr(index + searchValue.length); const title = index > -1 ? ( <span> {beforeStr} <span style={{ color: '#f50' }}>{searchValue}</span> {afterStr} </span> ) : ( <span>{item.title}</span> ); if (item.children) { return { title, key: item.key, children: loop(item.children), }; } return { title, key: item.key, }; }); return ( <div> <Search style={{ marginBottom: 8 }} placeholder="Search" onChange={this.onChange} /> <Tree onExpand={this.onExpand} expandedKeys={expandedKeys} autoExpandParent={autoExpandParent} treeData={loop(treeData)} /> </div> ); } } ``` 这里的onSearch函数会在Search组件的值发生变化时被调用。其中,onChange函数会根据搜索值来过滤出需要展开的节点,然后再将搜索值传递给loop函数,该函数会返回一个新的树形结构,其中匹配搜索值的节点会被高亮显示。 最后,在render函数中返回更新后的树形结构即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值