search

Home  >  Q&A  >  body text

javascript - antd's Tree component, does it support drag and drop restrictions?

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_leeringa_lee2740 days ago934

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-19 10:34:01

    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>)
        }
    }
    

    reply
    0
  • Cancelreply