Comments (3)
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.
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.
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)
- Migration guide for new version package is required HOT 11
- `TreeController.collapseCascading()` has a funky behavior when paired with `SliverAnimatedTree`
- `TreeView` and `AnimatedTreeView` does not refresh on `treeController` change HOT 1
- How can I add nodes and rebuild HOT 3
- Best Way to Scroll Horizontally and Vertically HOT 2
- Adding a vertical scroll bar to the tree HOT 6
- How to change solid line to dashed line HOT 13
- When adding a child node to a node, setState() does not take effect, and only when expanding the node again can the newly added node be displayed. HOT 2
- How to use provider (riverpod) in the childrenProvider HOT 10
- Is it possible to have no interaction ? HOT 1
- Both horizontal and vertical scroll? HOT 9
- Selected node not highlighted HOT 3
- How to dynamically load tree for selected root? HOT 3
- how make search field to filter tree data HOT 2
- how to collapse all the children if parent collapse HOT 2
- [Help] DataTable inside tree, when node !hasChildren HOT 2
- how to fetch and load node data on tap HOT 4
- Add children on tap for given node HOT 1
- How to have tree fully expanded at start and on refresh? HOT 2
- Re-ordering and Moving Nodes HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_tree_view.