I'm using mat-tree angular material component. It's a nice component with some very useful features like, multi-select, expand all/collapse all. I was not able to find any tree filtering feature in any of their APIs. Has anyone came across this feature or done any work around to get mat-tree filter?
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- How to instantiate Http service in main.ts manuall
- Highlight parent path to the root
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- How can you get current positional information abo
- Angular material table not showing data
I solved the problem by creating new datasource(filtered).
stackblitz sample
If I will explain the example of sharing the link, I filtered data by filter(filterText: string) in ChecklistDatabase and trigger dataChange event and then datasource.data changed by handled event in TreeChecklistExample. Thus datasource has been modified.
First add an input as the filter in the view. Bind keyup event to rxjs Subject
Then query your backend to filter the tree node with keyword
I am able to filter a tree by using simple recursion. Below are the code snippets:
The
filter()
function is called on(keyup)
ofinput type="text"
.cloneDeep
function is imported from lodashimport * as cloneDeep from 'lodash/cloneDeep';
this.searchString
is the string value for the filter text.The tree structure is defined by the interface
The actual filtering is done by the function
recursiveNodeEliminator
After I have spent several days on the same task here are some tips i can give: I am using input event to follow the user input:
On this filter i have attached a subject so i can subscribe to it:
To make it smooth for the user usually we want to delay the search execution, which you can do with debounceTime.
To perform the search I hide and show the nodes using a css class. This is done directly on the presentation collection which is flat and very easy to filter.
First I hide all and then show only these that match the criteria. Finally i want to show their parents but this is specific for my tree structure.
Finally here is the clear filter
Don't make the same mistake as me and try to filter the input collection (this.dataSource.data in my case) because you will lose you selection or you have to map it back to the presentation. Here is my initial data.