Skip to content

Show until specific level in tree #244

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No 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? No Sign in to your account

Closed
elcirao opened this issue Dec 21, 2020 · 4 comments
Closed

Show until specific level in tree #244

elcirao opened this issue Dec 21, 2020 · 4 comments
Milestone

Comments

@elcirao
Copy link

elcirao commented Dec 21, 2020

Please include a clear and concise description of the feature or enhancement you would like to have added to the project.
Hi, is there a way to show until specific leven in the tree? For example, i have an array with four levels from the API, but i just want to show until the third level. Is that possible?

@jakezatecky
Copy link
Owner

To be clear, are you looking for something to expand all of the nodes until the third level? This is a reasonable enough request, but it can be done through the expanded property, by iterating recursively through nodes until the desired depth is reached.

I can potentially add a utility function to help set that initial state.

@elcirao
Copy link
Author

elcirao commented Dec 21, 2020

yes, i want expand all of the nodes until the third level :)

@jakezatecky
Copy link
Owner

jakezatecky commented Dec 21, 2020

For the interim, you can do something like the following. Note that the first level starts with 0:

function expandNodesToLevel(nodes, targetLevel, currentLevel = 0) {
  if (currentLevel > targetLevel) {
    return [];
  }

  let expanded = [];
  nodes.forEach((node) => {
    expanded.push(node.value);
    if (node.children) {
    	expanded = [...expanded, ...expandNodesToLevel(node.children, targetLevel, currentLevel + 1)];
    }
  });
  return expanded;
}

For the initial state, you would do something like expanded = expandNodesToLevel(nodes, 2);.

See the following example that expands the tree to the second level.

@elcirao
Copy link
Author

elcirao commented Dec 21, 2020

it works perfect!

jakezatecky added a commit that referenced this issue Jan 20, 2021
@jakezatecky jakezatecky added this to the v1.7.0 milestone Jan 20, 2021
No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants