-
Notifications
You must be signed in to change notification settings - Fork 900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get parentKey
to add new node
#49
Comments
Hi, Can you provide example for AddNodeUnderParent and removeNodeAtPath i am trying to add and remove @anil1712 : Please let me know if you found any work arround. Thanks in advance. |
@yagneshmodh Right now I am just using
Let me know if you got any solution to pass the |
@fritz-c Can you please look into this? |
try this code:
It's works fine for me. |
Sorry for the delay. What action initiates the node addition? i.e., are you clicking on a button on the parent to add a child, are you adding nodes programmatically to parent nodes that fit certain conditions, etc? The way to do it will depend on that. |
Very very thanks @fritz-c Its working prefect. |
guys nobody has explained where to get parentKey to add new node? in my case I am triggering an action from inside custom node renderer. |
I wrote a small helper function to get the parent. I know it's not that efficient, because it walks every node of the tree even after it has found the parent, but anyway: function getNodeKey(node) {
return node.id;
}
function getParent(childNode, treeData) {
let parent = null;
walk({
treeData,
getNodeKey,
ignoreCollapsed: false,
callback: ({ node }) => {
if (find(node.children, { id: getNodeKey(childNode) })) {
parent = node;
}
},
});
return parent;
} You may want to alter |
@vladimirsvsv77 What is |
@everyonesdesign I don't understand your function. How do you just call What about |
@borisyordanov const { walk } = require('react-sortable-tree'); You can find other helpful functions in src/utils/tree-data-utils.js. if (node.children.find(child => getNodeKey(сhild) === getNodeKey(childNode))) {
parent = node;
} |
@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 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! |
Late to the game, but @borisyordanov |
The parentKey comes from the property render() {
return <SortableTree isVirtualized={ false }
treeData={this.state.treeData}
onChange={this.on_state_change.bind(this)}
generateNodeProps={
({node, path, treeidx}) => ({
title: node.title,
buttons: [
<button data-node_key_path={ path.join(',') }
onClick={ this.update_tree_status.bind(this) }>
edit
</button> ,
],
})
}
/> ;
} // end of render function The argument To me, it may be good practice to preserve Hope that helps, since it is not clearly documented. |
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 theaddNodeUnderParent(treeData, newNode, parentKey)
but dont know how to get theparentKey
.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
The text was updated successfully, but these errors were encountered: