javascript - antd's Tree component, does it support drag and drop restrictions?
ringa_lee
ringa_lee 2017-05-19 10:32:01
0
1
964

When dragging the Tree component, I want to restrict the parent node from being dragged to the level of the child node, which means that only the child node can be dragged upwards. Can this be supported by antd?

ringa_lee
ringa_lee

ringa_lee

reply all(1)
洪涛

The final rendering only depends on what the data looks like. The change of the data is under your own control. Whether it is allowed or not is just a matter of whether the data changes.

For example, the following code, 子节点 只能放到 第三个 下面,不能放到 第一个 below (because this is my logic).

import * as React from 'react';
import {BaseComponent} from '../base';
import Tree from 'antd/lib/tree';
const TreeNode = Tree.TreeNode;
import 'antd/lib/tree/style/index.css';


export interface HeaderProps { }
export interface HeaderState { data: any }

export class Header extends BaseComponent<HeaderProps, HeaderState> {
    state = {
        data: [
            {name: '第一个', key: 'a'},
            {name: '第二个', key: 'b',
                children: [
                    {name: '子节点', key: 'd'}
                ]},
            {name: '第三个', key: 'c'}
        ]
    };

    constructor(props:HeaderProps) {
        super(props);
    }

    onDragEnter(opt){
        console.log(opt, 'x');
    }

    onDrop(opt){
        const fromNode = opt.dragNode.props.eventKey;
        const toNode = opt.node.props.eventKey;
        if(toNode !== 'c'){ return }
        this.state.data[2]['children'] = this.state.data[1]['children'];
        this.state.data[1]['children'] = [];
        this.setState({});
    }

    loop(data){
        return data.map( d => {
            if(d.children && d.children.length){
                return <TreeNode key={d.key} title={d.name}>{this.loop(d.children)}</TreeNode>
            } else {
                return <TreeNode key={d.key} title={d.name}></TreeNode>
            }
        })
    }

    render() {
        return (<p>
            <Tree className="draggable-tree"
                draggable
                onDragEnter={this.onDragEnter.bind(this)}
                onDrop={this.onDrop.bind(this)}>
                {this.loop(this.state.data)}
            </Tree>
        </p>)
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template