Comments (14)
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.
from react-sortable-tree.
@fritz-c Can you please look into this?
from react-sortable-tree.
@borisyordanov
walk
is a utils method provided by react-sortable-tree.
It can be directly imported from it, e.g.
const { walk } = require('react-sortable-tree');
You can find other helpful functions in src/utils/tree-data-utils.js.
As for find
method, I use the method from Lodash library, but you can also use native Array.prototype.find (in case you don't support IE or polyfill the method).
if (node.children.find(child => getNodeKey(сhild) === getNodeKey(childNode))) {
parent = node;
}
from react-sortable-tree.
@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!
from react-sortable-tree.
Hi,
Can you provide example for AddNodeUnderParent and removeNodeAtPath i am trying to add and remove
node in generateNodeProps property.
@anil1712 : Please let me know if you found any work arround.
Thanks in advance.
from react-sortable-tree.
@yagneshmodh Right now I am just using addNodeUnderParent
. Here is the working code.
..................
import {addNodeUnderParent} from 'react-sortable-tree';
..................
let NEW_NODE = {title: 'Another Node', subtitle: 2}
let newTree = addNodeUnderParent({
treeData: this.state.treeData,
newNode: NEW_NODE,
expandParent: true,
parentKey: 0, // Still don't know where to get the parentKey
getNodeKey: ({node: TreeNode, treeIndex: number}) => {
return treeIndex;
},
});
this.setState({treeData: newTree.treeData});
Let me know if you got any solution to pass the parentKey
from react-sortable-tree.
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.
from react-sortable-tree.
Very very thanks @fritz-c Its working prefect.
from react-sortable-tree.
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.
from react-sortable-tree.
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 getNodeKey
function depending on your tree structure.
from react-sortable-tree.
@vladimirsvsv77 What is getNodeAtPath()
? Is that a system function? If you wrote it yourself can you post your code?
from react-sortable-tree.
@everyonesdesign I don't understand your function. How do you just call walk()
, isn't that a method of the treeData
? Is it a custom method you implemented?
What about find()
? Where did that come from?
from react-sortable-tree.
Late to the game, but @borisyordanov import { getNodeAtPath } from 'react-sortable-tree'
from react-sortable-tree.
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.
The parentKey comes from the property generateNodeProps
in your SortableTree
component , generateNodeProps
points to any function with arguments {node, path}
and expects to return an a object with fields like {title: YOUR_NODE_TITLE, buttons:[BUTTON1, BUTTON2 , ......]}
which is used for renderring each node . For example , your render code may look like this :
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 path
in the property generateNodeProps
is actually a list of tree index numbers for each node to represent a path from a root node to that node in current tree view , Note that the path
of each node also changes as soon as you expand or collapse any non-leaf node in the tree view, then Sortable
re-calculates path
and re-render the buttons for each present node in updated tree view .
To me, it may be good practice to preserve path
from generateNodeProps
to the buttons (in the example, the path
is serialized and store to dataset of the HTML element) , used at later time for any operation on the tree.
Hope that helps, since it is not clearly documented.
from react-sortable-tree.
Related Issues (20)
- How to use props with redux ? HOT 1
- React version dependency issue HOT 5
- how to make search nodes only starting from one node which we clicked ?
- Is it possible to make the react sortable tree ui less big with a setting : sm, md, bg ?
- React dnd upgrade HOT 2
- How to customize move handler? 🤔 HOT 2
- div is fadeout when drag
- Insert node at specific index under parent node.Iindex HOT 1
- It is possible to use a node as ReactRef() ? HOT 1
- Give Children another label/property name
- Failed to minify the code error HOT 2
- getTreeFromFlatData set rootKey as null
- Support multiple dragging
- Option to snap automatically to nearest possible level HOT 1
- Support React 18 HOT 1
- new child is added to the parent when collapsing the parent HOT 1
- Using the react-sortable-tree in react 18 HOT 4
- is it possible to display the tree as a vertical tree diagram?
- About maintenance of this library
- Could not resolve dependency: React 18 with [email protected] HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-sortable-tree.