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
Comments
I currently use following code to do the trick. 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();
} |
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 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 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. |
Thanks, I have tried your code, copying node children solved the problem! |
For example, when I search
B 2 1 1
the tree remains nodesB
,B 2
,B 2 1
andB 2 1 1
, while search content is empty it shows original tree data.The text was updated successfully, but these errors were encountered: