天蓝云朗 2023-08-15 10:52 采纳率: 33.3%
浏览 1

and design 5.x版本中的tree组件拖拽功能

链接:https://4x.ant.design/components/tree-cn/#components-tree-demo-draggable

请问,这段代码什么情况下会被执行?

if (
    ((info.node as any).props.children || []).length > 0 && // Has children
    (info.node as any).props.expanded && // Is expanded
    dropPosition === 1 // On the bottom gap
  ) {
      loop(data, dropKey, item => {
        item.children = item.children || [];
        // where to insert 示例添加到头部,可以是随意位置
        item.children.unshift(dragObj);
        // in previous version, we use item.children.push(dragObj) to insert the
        // item to the tail of the children
      });
    }

完整代码:

import { Tree } from 'antd';
import type { DataNode, TreeProps } from 'antd/es/tree';
import React, { useState } from 'react';

const x = 3;
const y = 2;
const z = 1;
const defaultData: DataNode[] = [];

const generateData = (_level: number, _preKey?: React.Key, _tns?: DataNode[]) => {
  const preKey = _preKey || '0';
  const tns = _tns || defaultData;

  const children = [];
  for (let i = 0; i < x; i++) {
    const key = `${preKey}-${i}`;
    tns.push({ title: key, key });
    if (i < y) {
      children.push(key);
    }
  }
  if (_level < 0) {
    return tns;
  }
  const level = _level - 1;
  children.forEach((key, index) => {
    tns[index].children = [];
    return generateData(level, key, tns[index].children);
  });
};
generateData(z);

const App: React.FC = () => {
  const [gData, setGData] = useState(defaultData);
  const [expandedKeys] = useState(['0-0', '0-0-0', '0-0-0-0']);

  const onDragEnter: TreeProps['onDragEnter'] = info => {
    console.log(info);
    // expandedKeys 需要受控时设置
    // setExpandedKeys(info.expandedKeys)
  };

  const onDrop: TreeProps['onDrop'] = info => {
    console.log(info);
    const dropKey = info.node.key;
    const dragKey = info.dragNode.key;
    const dropPos = info.node.pos.split('-');
    const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);

    const loop = (
      data: DataNode[],
      key: React.Key,
      callback: (node: DataNode, i: number, data: DataNode[]) => void,
    ) => {
      for (let i = 0; i < data.length; i++) {
        if (data[i].key === key) {
          return callback(data[i], i, data);
        }
        if (data[i].children) {
          loop(data[i].children!, key, callback);
        }
      }
    };
    const data = [...gData];

    // Find dragObject
    let dragObj: DataNode;
    loop(data, dragKey, (item, index, arr) => {
      arr.splice(index, 1);
      dragObj = item;
    });

    if (!info.dropToGap) {
      // Drop on the content
      loop(data, dropKey, item => {
        item.children = item.children || [];
        // where to insert 示例添加到头部,可以是随意位置
        item.children.unshift(dragObj);
      });
    } else if (
      ((info.node as any).props.children || []).length > 0 && // Has children
      (info.node as any).props.expanded && // Is expanded
      dropPosition === 1 // On the bottom gap
    ) {
      loop(data, dropKey, item => {
        item.children = item.children || [];
        // where to insert 示例添加到头部,可以是随意位置
        item.children.unshift(dragObj);
        // in previous version, we use item.children.push(dragObj) to insert the
        // item to the tail of the children
      });
    } else {
      let ar: DataNode[] = [];
      let i: number;
      loop(data, dropKey, (_item, index, arr) => {
        ar = arr;
        i = index;
      });
      if (dropPosition === -1) {
        ar.splice(i!, 0, dragObj!);
      } else {
        ar.splice(i! + 1, 0, dragObj!);
      }
    }
    setGData(data);
  };

  return (
    <Tree
      className="draggable-tree"
      defaultExpandedKeys={expandedKeys}
      draggable
      blockNode
      onDragEnter={onDragEnter}
      onDrop={onDrop}
      treeData={gData}
    />
  );
};

export default App;
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-08-15 14:46
    关注

    【以下回答由 GPT 生成】

    我觉得代码中有一个小错误,当在loop(data, dropKey, (item) => {...});中调用loop函数时,参数名应该为dragKey而不是dropKey。下面是更正过的代码:

    import { Tree } from 'antd';
    import type { DataNode, TreeProps } from 'antd/es/tree';
    import React, { useState } from 'react';
    
    const x = 3;
    const y = 2;
    const z = 1;
    const defaultData: DataNode[] = [];
    
    const generateData = (_level: number, _preKey?: React.Key, _tns?: DataNode[]) => {
      const preKey = _preKey || '0';
      const tns = _tns || defaultData;
    
      const children = [];
      for (let i = 0; i < x; i++) {
        const key = `${preKey}-${i}`;
        tns.push({ title: key, key });
        if (i < y) {
          children.push(key);
        }
      }
      if (_level < 0) {
        return tns;
      }
      const loop = (data: DataNode[], prePos: React.Key[], siblingIndex?: number) => {
        const len = data.length;
        data.forEach((item, index) => {
          const pos = [...prePos, index];
          item.pos = pos.join('-');
          item.level = pos.length - 1;
          if (item.children && item.children.length > 0) {
            loop(item.children, pos);
          }
          if (siblingIndex !== undefined) {
            item.siblingPosition = siblingIndex;
          }
          if (index === 0) {
            item.isStart = true;
            item.isLeaf = index === len - 1;
            item.isEnd = index === 0 && item.isLeaf;
          } else if (index === len - 1) {
            item.isEnd = true;
            item.isLeaf = false;
          } else {
            item.isLeaf = false;
          }
        });
      };
      loop(tns, []);
      return tns;
    };
    
    const DraggableTree: React.FC<TreeProps> = () => {
      const [treeData, setTreeData] = useState(() => {
        const tns: DataNode[] = [];
        generateData(z, undefined, tns);
        return tns;
      });
    
      const loop = (data: DataNode[], callback: (item: DataNode, index: number, arr: DataNode[]) => void) => {
        data.forEach((item, index, arr) => {
          callback(item, index, arr);
          if (item.children && item.children.length > 0) {
            loop(item.children, callback);
          }
        });
      };
    
      const onDrop = (info: any) => {
        const dropKey = info.node.key;
        const dragKey = info.dragNode.key;
        const dropPos = info.node.pos.split('-');
        const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
    
        if (
          ((info.node as any).props.children || []).length > 0 &&
          (info.node as any).props.expanded &&
          dropPosition === 1
        ) {
          loop(treeData, dragKey, (item) => {
            item.children = item.children || [];
            // where to insert 示例添加到头部,可以是随意位置
            item.children.unshift(dragObj);
            // in previous version, we use item.children.push(dragObj) to insert the
            // item to the tail of the children
          });
        }
      };
    
      return (
        <Tree
          draggable
          blockNode
          treeData={treeData}
          onDrop={onDrop}
        />
      );
    };
    
    export default DraggableTree;
    

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 8月15日

悬赏问题

  • ¥15 matlab数据降噪处理,提高数据的可信度,确保峰值信号的不损失?
  • ¥15 怎么看我在bios每次修改的日志
  • ¥15 python+mysql图书管理系统
  • ¥15 Questasim Error: (vcom-13)
  • ¥15 船舶旋回实验matlab
  • ¥30 SQL 数组,游标,递归覆盖原值
  • ¥15 为什么我的数据接收的那么慢呀有没有完整的 hal 库并 代码呀有的话能不能发我一份并且我用 printf 函数显示处理之后的数据,用 debug 就不能运行了呢
  • ¥20 gitlab 中文路径,无法下载
  • ¥15 用动态规划算法均分纸牌
  • ¥30 udp socket,bind 0.0.0.0 ,如何自动选取用户访问的服务器IP来回复数据