Skip to content
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 update nodes after onChange #5

Open
amitaibu opened this issue Oct 12, 2023 · 4 comments
Open

How to update nodes after onChange #5

amitaibu opened this issue Oct 12, 2023 · 4 comments

Comments

@amitaibu
Copy link

I've created a demo repo using IHP (Haskell), so thanks for this handy package.

Over here I'm updating the tree on the backend, and I return a JSON with new nodes. I'm rendering the nodes differently based on their position.

How can I update the tree based on the new nodes we fetched?

I tried to do this.nodes = ... from within the onChange, but I didn't see a change.

@amitaibu
Copy link
Author

As a workaround, I'm cleaning the tree, and re-render it from the onChange. Wondering if there's a better way.

@marcantondahmen
Copy link
Owner

Hi! Yeah, I think re-rendering the tree is the most straight forward way to do it. You can provide a stateId in order to persist the folding state across re-renders and also page refreshes. That way the user shouldn't notice the rendering.

@amitaibu
Copy link
Author

You can provide a stateId in order to persist the folding state across re-renders and also page refreshes.

Is there an example to that?

@marcantondahmen
Copy link
Owner

Sure, you can find it in all examples on the demo page. The stateId can be any string you want:

import { nodes } from './nodes';
import SortableTree from 'sortable-tree';

new SortableTree({
  nodes,
  element: document.getElementById('tree'),
  stateId: 'tree-state',
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants