Git Product home page Git Product logo

Comments (3)

lvyandev avatar lvyandev commented on May 12, 2024 1

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.

from flutter_tree_view.

lvyandev avatar lvyandev commented on May 12, 2024

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();
  }

from flutter_tree_view.

baumths avatar baumths commented on May 12, 2024

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.

from flutter_tree_view.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.