链接: https://4x.ant.design/components/tree-cn/#components-tree-demo-draggable
请问,这段代码什么情况下会被执行?
((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 (
className="draggable-tree"
defaultExpandedKeys={expandedKeys}
draggable
blockNode
onDragEnter={onDragEnter}
onDrop={onDrop}
treeData={gData}
export default App;
0 提建议
微信扫一扫
点击复制链接
分享 邀请回答
编辑 收藏 删除 结题 收藏 举报 追加酬金 (90%的用户在追加酬金后获得了解决方案) 
当前问题酬金
¥ 0
(可追加 ¥500)
支付方式
扫码支付
加载中...
1
条回答
默认
最新
查看更多回答(-1条)
报告相同问题?
-
-
-
-
前端页面仔的博客
Ant
Design
Vue 树形控件
Tree
实现
拖
拽
功能
-
没有解决我的问题,
去提问
悬赏问题
-
¥20
Gradle构建MCmod时出现问题
有java微服务重构经验的看过来
为什么图斑相同但shape_area不一样?!
关于整形函数和数组传递
nextcloud+onlyoffice文件被莫名其妙还原
请教一个弱电网络的原理和配置
laycascader级联选择器的懒加载问题
labelme_to_coco(多个json文件转换成一个)