question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get `parentKey` to add new node

See original GitHub issue

Hi, First of all I really like your implemented stuff, it saves my lot of time. So thanks for implementing this sortable tree, but it would be more better if you can make the documentation more descriptive. Because I am having issue while using the Data Helper Functions. I am trying to use the addNodeUnderParent(treeData, newNode, parentKey) but dont know how to get the parentKey.

Also I did not the use of getNodeKey parameter, so can you please explain these thing with some example.

Actually I am pretty new for React. So please help me to get out of it.

@fritz-c can you please look into it.

Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
vladimirsvsv77commented, Feb 12, 2017

try this code:

    addNode(rowInfo){
        let NEW_NODE = {title: 'Another Node', subtitle: 2};
        let {node, treeIndex, path} = rowInfo;
        path.pop();
        let parentNode = getNodeAtPath({
            treeData: this.state.treeData,
            path : path,
            getNodeKey: ({ treeIndex }) =>  treeIndex,
            ignoreCollapsed : true
        });
        let getNodeKey = ({ node: object, treeIndex: number }) => {
            return number;
        };
        let parentKey = getNodeKey(parentNode);
        if(parentKey == -1) {
            parentKey = null;
        }
        let newTree = addNodeUnderParent({
                treeData: this.state.treeData,
                newNode: NEW_NODE,
                expandParent: true,
                parentKey: parentKey,
                getNodeKey: ({ treeIndex }) =>  treeIndex
         });
         this.setState({treeData: newTree.treeData});
    }

It’s works fine for me.

1reaction
borisyordanovcommented, Nov 22, 2017

@everyonesdesign Thanks for taking the time to explain. I do have another question - if you need to get the parent of the node, why don’t you use the path? If your path = [2,3,1], then the parent should be treeData[2].children[3] ?

Edit: i guess what i posted above is true, but that info doesn’t necessarily make finding the parent easier. I ended up using a slightly modified version of your function. Thanks for the help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add the child node using parent key - Syncfusion
You can add the child node in parent node using parent key column in TreeViewAdv control by using Add() method. This can be...
Read more >
node.js - Adding parent key to this json output - Stack Overflow
I am using restify module on node.js to build a REST API server. I have this simple API using restify. var api_get_sample =...
Read more >
Find the parent of a node in the given binary tree
Given a tree and a node, the task is to find the parent of the given node in the tree. Print -1 if...
Read more >
How to get a parent node key in the InitNewNode event
NewNodeParentKey property to get the key value of a new node's parent. This answer was helpful 1.
Read more >
how to find parent node key in the child node determination ...
You get the key of your current root instance by following the "to_parent" association. Regards,. Martin. Add a Comment. Help to improve this ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found