ant-design/components/tree/demo/draggable.md

113 lines
2.6 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 2
title: 拖动示例
---
2016-01-07 19:05:55 +08:00
2016-01-28 15:13:17 +08:00
将节点拖拽到其他节点内部或前后。
2016-01-07 19:05:55 +08:00
````jsx
import { Tree } from 'antd';
const TreeNode = Tree.TreeNode;
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}`;
tns.push({ title: key, key });
2016-01-07 19:05:55 +08:00
if (i < y) {
children.push(key);
}
}
if (_level < 0) {
return tns;
}
const level = _level - 1;
2016-01-07 19:05:55 +08:00
children.forEach((key, index) => {
tns[index].children = [];
return generateData(level, key, tns[index].children);
2016-01-07 19:05:55 +08:00
});
};
generateData(z);
const Demo = React.createClass({
getInitialState() {
return {
2016-01-28 15:13:17 +08:00
gData,
2016-01-07 19:05:55 +08:00
expandedKeys: ['0-0', '0-0-0', '0-0-0-0'],
};
},
onDragEnter(info) {
2016-01-29 11:31:10 +08:00
console.log(info);
// expandedKeys 需要受控时设置
// this.setState({
// expandedKeys: info.expandedKeys,
// });
2016-01-07 19:05:55 +08:00
},
onDrop(info) {
2016-01-07 19:05:55 +08:00
console.log(info);
const dropKey = info.node.props.eventKey;
const dragKey = info.dragNode.props.eventKey;
// const dragNodesKeys = info.dragNodesKeys;
const loop = (data, key, callback) => {
data.forEach((item, index, arr) => {
if (item.key === key) {
return callback(item, index, arr);
}
if (item.children) {
return loop(item.children, key, callback);
}
});
};
const data = [...this.state.gData];
let dragObj;
loop(data, dragKey, (item, index, arr) => {
arr.splice(index, 1);
dragObj = item;
});
if (info.dropToGap) {
let ar;
let i;
loop(data, dropKey, (item, index, arr) => {
ar = arr;
i = index;
});
ar.splice(i, 0, dragObj);
} else {
loop(data, dropKey, (item) => {
item.children = item.children || [];
// where to insert 示例添加到尾部,可以是随意位置
item.children.push(dragObj);
});
}
this.setState({
gData: data,
});
},
render() {
2016-01-28 15:13:17 +08:00
const loop = data => data.map((item) => {
if (item.children) {
return <TreeNode key={item.key} title={item.key}>{loop(item.children)}</TreeNode>;
}
return <TreeNode key={item.key} title={item.key} />;
});
return (
<Tree defaultExpandedKeys={this.state.expandedKeys} openAnimation={{}} draggable
onDragEnter={this.onDragEnter}
onDrop={this.onDrop}>
{loop(this.state.gData)}
</Tree>
);
2016-01-07 19:05:55 +08:00
},
});
ReactDOM.render(<Demo />, mountNode);
````