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

Is there any better way to filter nodes? #18

Closed
lvyandev opened this issue Jan 11, 2022 · 3 comments
Closed

Is there any better way to filter nodes? #18

lvyandev opened this issue Jan 11, 2022 · 3 comments
Labels
question Further information is requested

Comments

@lvyandev
Copy link

kRootId: ['A', 'B', 'C', 'D', 'E', 'F'],
  'A': ['A 1', 'A 2'],
  'A 2': ['A 2 1'],
  'B': ['B 1', 'B 2', 'B 3'],
  'B 1': ['B 1 1'],
  'B 1 1': ['B 1 1 1', 'B 1 1 2'],
  'B 2': ['B 2 1'],
  'B 2 1': ['B 2 1 1'],
  'C': ['C 1', 'C 2', 'C 3', 'C 4'],
  'C 1': ['C 1 1'],
  'D': ['D 1'],
  'D 1': ['D 1 1'],
  'E': ['E 1'],
  'F': ['F 1', 'F 2'],

For example, when I search B 2 1 1 the tree remains nodes B,B 2,B 2 1 and B 2 1 1, while search content is empty it shows original tree data.

@lvyandev
Copy link
Author

I currently use following code to do the trick.
However, I found that rootNode children is modified during buildSearchData; Therefore, I had to request api and regenerate all tree when searchContent is empty.

  void onSearch() {
    if (searchContent.trim().isEmpty) {
      getData();
    } else {
      final TreeNode newRootNode = TreeNode(id: 'SearchRoot');

      final Set<TreeNode> searchData =
          buildSearchData(searchContent, Set<TreeNode>.from(rootNode.children));

      newRootNode.addChildren(searchData);

      treeController = TreeViewController(rootNode: newRootNode);
    }

    treeController.expandAll();

    setState(() {});
  }

  Set<TreeNode> buildSearchData(String content, Iterable<TreeNode> nodes) {
    return nodes
        .map(
          (TreeNode e) {
            final TreeNode treeNode =
                TreeNode(id: e.id, label: e.label, data: e.data);
            if (e.hasChildren) {
              if (e.label.contains(content)) {
                final Set<TreeNode> children = Set<TreeNode>.from(e.children);
                treeNode.addChildren(children);
                return treeNode;
              } else {
                final Iterable<TreeNode> searchData =
                    buildSearchData(content, e.children);
                if (searchData.isNotEmpty) {
                  searchData.forEach(treeNode.addChild);
                  return treeNode;
                }
              }
            } else if (e.label.contains(content)) {
              return treeNode;
            } else if (e.data is Map<String, dynamic>) {
              final Map<String, dynamic> data = e.data as Map<String, dynamic>;
              // tree data satisfies search content
              if (data.validateAny(
                (Map<String, dynamic> data) => <bool?>[
                  cast<String?>(data['standards'])?.contains(content),
                  cast<String?>(data['unit'])?.contains(content),
                ],
              )) {
                return treeNode;
              }
            }
          },
        )
        .whereNotNull()
        .toSet();
  }

@baumths
Copy link
Owner

baumths commented Jan 11, 2022

Hey @lvyandev! Thank you for opening this issue.

Could you please add a "expected/actual" result of your functions?

Make sure all your ids are unique, the TreeNode objects get their identity from its id, label and data.

I see you are copying the nodes into a new tree, but you are not copying in the following part:

if (e.hasChildren) {
  if (e.label.contains(content)) {
    // You should probably copy each child into a new TreeNode here, right?
    final Set<TreeNode> children = Set<TreeNode>.from(e.children);
    // Before adding them to the treeNode children as below
    treeNode.addChildren(children);
    return treeNode;
  } else {
    // ...
  }
}

The TreeNode.addChild removes the child from its old parent before adding it to the new one, so if you don't copy them over to the new tree (i.e. create new objects), when showing the old tree again, those nodes will be missing because they are now part of another tree (i.e. were detached from old root and attached to the new one).

Try the following:

if (e.hasChildren) {
  if (e.label.contains(content)) {
    for (final TreeNode child in e.children) {
      final TreeNode newChild = TreeNode(id: child.id, label: child.label, data: child.data);
      treeNode.addChild(newChild);
    }
    return treeNode;
  } else {
    // ...
  }
}

I haven't tried the code above, let me know if it works.

@baumths baumths added the question Further information is requested label Jan 11, 2022
@lvyandev
Copy link
Author

lvyandev commented Jan 12, 2022

Thanks, I have tried your code, copying node children solved the problem!
addChild will first remove node from tree and add it then, that's why the original rootNode was modified.

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

No branches or pull requests

2 participants