React-sortable-tree: path in removeNode

Created on 10 Feb 2017  路  5Comments  路  Source: frontend-collective/react-sortable-tree

I try remove my Node, how I can get path in my case?

export default class TreeVera extends Component {
    constructor(props) {
        super(props);
        this.updateTreeData = this.updateTreeData.bind(this);
        this.removeNode = this.removeNode.bind(this);
        this.state = {
            treeData: [{
                title: (<div>
                        <input />
                        <button onClick={this.removeNode}>remove</button>
                    </div>),
            }]
        };
    }
    removeNode(e){
        this.setState({
               treeData: removeNodeAtPath({
                   treeData: this.state.treeData,
                   path: ????,
                   getNodeKey: ({node: TreeNode, treeIndex: number}) => {
                       console.log(number);
                       return number;
                   },
                   ignoreCollapsed: false,
               })
        })
    }
    updateTreeData(treeData) {
        this.setState({ treeData });
    }
    render() {
        return (
            <div style={{ height: 600 }}>
                <SortableTree
                    treeData={this.state.treeData}
                    onChange={this.updateTreeData}
                />
            </div>
        );
    }
}

Most helpful comment

Try to use generateNodeProps to create delete button.

<SortableTree
     treeData={this.state.treeData}
     onChange={treeData => this.onChange(treeData)}
     generateNodeProps={rowInfo => ({
         buttons: [
                   <Button label='Delete'
                            onClick={(event) => this.removeNode(rowInfo)}/>,
                ],
      })}

removeNode(rowInfo) {
    let {node, treeIndex, path} = rowInfo;
    removeNodeAtPath({
                   treeData: this.state.treeData,
                   path: path,   // You can use path from here
                   getNodeKey: ({node: TreeNode, treeIndex: number}) => {
                       console.log(number);
                       return number;
                   },
              ignoreCollapsed: false,
           })
}

I am using the same way but some how getting below exception in browser console
Uncaught (in promise) Error: No node found at the given path.
Let me know if its working for you

@fritz-c can you please check am I doing something wrong.

Thanks,

All 5 comments

Try to use generateNodeProps to create delete button.

<SortableTree
     treeData={this.state.treeData}
     onChange={treeData => this.onChange(treeData)}
     generateNodeProps={rowInfo => ({
         buttons: [
                   <Button label='Delete'
                            onClick={(event) => this.removeNode(rowInfo)}/>,
                ],
      })}

removeNode(rowInfo) {
    let {node, treeIndex, path} = rowInfo;
    removeNodeAtPath({
                   treeData: this.state.treeData,
                   path: path,   // You can use path from here
                   getNodeKey: ({node: TreeNode, treeIndex: number}) => {
                       console.log(number);
                       return number;
                   },
              ignoreCollapsed: false,
           })
}

I am using the same way but some how getting below exception in browser console
Uncaught (in promise) Error: No node found at the given path.
Let me know if its working for you

@fritz-c can you please check am I doing something wrong.

Thanks,

Thanks, your code works perfect!

@anil1712 Thank you for adding your code. I've been busy outside of work lately, so it helps when members in the community help out.

Regarding the node error, could you give me the stack trace and steps for reproduction?

Your code looks like it would work except for one part - the result of removeNodeAtPath needs to be used to update this.state.treeData in your component.

Thanks for reply @fritz-c
Now its also working for me.

Hello, Can anyone please help with this and how to remove any node in the tree. Why because means I was trying the above same code in the functional component and got the error like this

Error: No node found at the given path.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

justduy picture justduy  路  5Comments

jorgecuesta picture jorgecuesta  路  4Comments

LogicMonsters picture LogicMonsters  路  5Comments

mcolburn picture mcolburn  路  4Comments

vaheqelyan picture vaheqelyan  路  4Comments