2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 2
|
2017-02-19 21:23:38 +08:00
|
|
|
title:
|
2016-07-21 09:52:39 +08:00
|
|
|
zh-CN: 拖动示例
|
|
|
|
en-US: draggable
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2016-01-07 19:05:55 +08:00
|
|
|
|
2016-07-21 09:52:39 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2016-01-28 15:13:17 +08:00
|
|
|
将节点拖拽到其他节点内部或前后。
|
2016-01-07 19:05:55 +08:00
|
|
|
|
2016-07-21 09:52:39 +08:00
|
|
|
## en-US
|
|
|
|
|
|
|
|
Drag treeNode to insert after the other treeNode or insert into the other parent TreeNode.
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
2016-01-07 19:05:55 +08:00
|
|
|
import { Tree } from 'antd';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2016-01-07 19:05:55 +08:00
|
|
|
const x = 3;
|
|
|
|
const y = 2;
|
|
|
|
const z = 1;
|
|
|
|
const gData = [];
|
|
|
|
|
|
|
|
const generateData = (_level, _preKey, _tns) => {
|
|
|
|
const preKey = _preKey || '0';
|
|
|
|
const tns = _tns || gData;
|
|
|
|
|
|
|
|
const children = [];
|
|
|
|
for (let i = 0; i < x; i++) {
|
|
|
|
const key = `${preKey}-${i}`;
|
2016-02-17 15:57:33 +08:00
|
|
|
tns.push({ title: key, key });
|
2016-01-07 19:05:55 +08:00
|
|
|
if (i < y) {
|
|
|
|
children.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_level < 0) {
|
|
|
|
return tns;
|
|
|
|
}
|
2016-04-29 12:13:27 +08:00
|
|
|
const level = _level - 1;
|
2016-01-07 19:05:55 +08:00
|
|
|
children.forEach((key, index) => {
|
|
|
|
tns[index].children = [];
|
2016-04-29 12:13:27 +08:00
|
|
|
return generateData(level, key, tns[index].children);
|
2016-01-07 19:05:55 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
generateData(z);
|
|
|
|
|
2017-02-19 21:23:38 +08:00
|
|
|
class Demo extends React.Component {
|
|
|
|
state = {
|
|
|
|
gData,
|
|
|
|
expandedKeys: ['0-0', '0-0-0', '0-0-0-0'],
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
onDragEnter = info => {
|
2016-01-29 11:31:10 +08:00
|
|
|
console.log(info);
|
|
|
|
// expandedKeys 需要受控时设置
|
|
|
|
// this.setState({
|
|
|
|
// expandedKeys: info.expandedKeys,
|
|
|
|
// });
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
onDrop = info => {
|
2016-01-07 19:05:55 +08:00
|
|
|
console.log(info);
|
2021-01-08 10:14:01 +08:00
|
|
|
const dropKey = info.node.key;
|
|
|
|
const dragKey = info.dragNode.key;
|
|
|
|
const dropPos = info.node.pos.split('-');
|
2017-05-02 16:45:32 +08:00
|
|
|
const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
|
2018-10-25 11:19:17 +08:00
|
|
|
|
2016-01-07 19:05:55 +08:00
|
|
|
const loop = (data, key, callback) => {
|
2020-04-08 23:33:42 +08:00
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
|
|
if (data[i].key === key) {
|
|
|
|
return callback(data[i], i, data);
|
2016-01-07 19:05:55 +08:00
|
|
|
}
|
2020-04-08 23:33:42 +08:00
|
|
|
if (data[i].children) {
|
2020-10-21 11:06:07 +08:00
|
|
|
loop(data[i].children, key, callback);
|
2016-01-07 19:05:55 +08:00
|
|
|
}
|
2020-04-08 23:33:42 +08:00
|
|
|
}
|
2016-01-07 19:05:55 +08:00
|
|
|
};
|
|
|
|
const data = [...this.state.gData];
|
2018-10-25 11:19:17 +08:00
|
|
|
|
|
|
|
// Find dragObject
|
2016-01-07 19:05:55 +08:00
|
|
|
let dragObj;
|
|
|
|
loop(data, dragKey, (item, index, arr) => {
|
|
|
|
arr.splice(index, 1);
|
|
|
|
dragObj = item;
|
|
|
|
});
|
2018-10-25 11:19:17 +08:00
|
|
|
|
|
|
|
if (!info.dropToGap) {
|
|
|
|
// Drop on the content
|
2019-05-07 14:57:32 +08:00
|
|
|
loop(data, dropKey, item => {
|
2018-10-25 11:19:17 +08:00
|
|
|
item.children = item.children || [];
|
2020-11-29 14:15:08 +08:00
|
|
|
// where to insert 示例添加到头部,可以是随意位置
|
|
|
|
item.children.unshift(dragObj);
|
2018-10-25 11:19:17 +08:00
|
|
|
});
|
|
|
|
} else if (
|
2019-05-07 14:57:32 +08:00
|
|
|
(info.node.props.children || []).length > 0 && // Has children
|
|
|
|
info.node.props.expanded && // Is expanded
|
|
|
|
dropPosition === 1 // On the bottom gap
|
2018-10-25 11:19:17 +08:00
|
|
|
) {
|
2019-05-07 14:57:32 +08:00
|
|
|
loop(data, dropKey, item => {
|
2018-10-25 11:19:17 +08:00
|
|
|
item.children = item.children || [];
|
2019-12-26 14:32:14 +08:00
|
|
|
// where to insert 示例添加到头部,可以是随意位置
|
2018-10-25 11:19:17 +08:00
|
|
|
item.children.unshift(dragObj);
|
2020-11-29 14:15:08 +08:00
|
|
|
// in previous version, we use item.children.push(dragObj) to insert the
|
|
|
|
// item to the tail of the children
|
2018-10-25 11:19:17 +08:00
|
|
|
});
|
|
|
|
} else {
|
2016-01-07 19:05:55 +08:00
|
|
|
let ar;
|
|
|
|
let i;
|
|
|
|
loop(data, dropKey, (item, index, arr) => {
|
|
|
|
ar = arr;
|
|
|
|
i = index;
|
|
|
|
});
|
2017-05-02 16:45:32 +08:00
|
|
|
if (dropPosition === -1) {
|
|
|
|
ar.splice(i, 0, dragObj);
|
|
|
|
} else {
|
2018-01-26 13:30:44 +08:00
|
|
|
ar.splice(i + 1, 0, dragObj);
|
2017-05-02 16:45:32 +08:00
|
|
|
}
|
2016-01-07 19:05:55 +08:00
|
|
|
}
|
2018-10-25 11:19:17 +08:00
|
|
|
|
2016-01-07 19:05:55 +08:00
|
|
|
this.setState({
|
|
|
|
gData: data,
|
|
|
|
});
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2016-01-07 19:05:55 +08:00
|
|
|
render() {
|
2016-01-28 15:13:17 +08:00
|
|
|
return (
|
2016-07-13 16:08:10 +08:00
|
|
|
<Tree
|
2017-03-02 17:08:57 +08:00
|
|
|
className="draggable-tree"
|
2016-07-13 16:08:10 +08:00
|
|
|
defaultExpandedKeys={this.state.expandedKeys}
|
|
|
|
draggable
|
2019-03-08 12:36:46 +08:00
|
|
|
blockNode
|
2016-01-28 15:13:17 +08:00
|
|
|
onDragEnter={this.onDragEnter}
|
2016-06-06 13:54:10 +08:00
|
|
|
onDrop={this.onDrop}
|
2019-08-12 13:22:36 +08:00
|
|
|
treeData={this.state.gData}
|
|
|
|
/>
|
2016-01-28 15:13:17 +08:00
|
|
|
);
|
2017-02-19 21:23:38 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-07 19:05:55 +08:00
|
|
|
|
2022-04-15 16:20:56 +08:00
|
|
|
export default Demo;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|