简体   繁体   English

角材料表自定义过滤器

[英]Angular material table custom filter

I am using mat-table.我正在使用垫表。 I know about the build in filter.我知道内置过滤器。 And the filterPredicate .filterPredicate But filter predicate only accepts filterValue: string .但过滤谓词只接受filterValue: string I want to be able to filter the table with slide toggle and show only objects that have for example isActive flag true or isActive flag false.我希望能够通过滑动切换过滤表格并仅显示具有例如isActive标志为 true 或isActive标志为 false 的对象。 But default filter doesn't offer such functionality.但是默认过滤器不提供这样的功能。 I figured out some workarounds myself but I hope there is a better solution.我自己想出了一些解决方法,但我希望有更好的解决方案。
1) I can filter the array myself but in that way I need to keep the initial array and pass the filtered array to the table. 1)我可以自己过滤数组,但这样我需要保留初始数组并将过滤后的数组传递给表。 And I don't like the idea of using two arrays for the same information.而且我不喜欢将两个数组用于相同信息的想法。
2) I can pass special value to the filter when slide toggle is changed for example &@changedToggle-true and check for that value in the filter predicate and filter only objects with the right isActive flag. 2)我可以在更改滑动切换时将特殊值传递给过滤器,例如&@changedToggle-true并检查过滤器谓词中的该值并仅过滤具有正确 isActive 标志的对象。 But that still doesn't look like good solution to me.但这对我来说仍然不是很好的解决方案。
Is there any good way for achieving what I want.有什么好的方法可以实现我想要的。

I do not know if I understand your problem, but you can extends standard datasource with something like this:我不知道我是否理解您的问题,但是您可以使用以下内容扩展标准数据源:

YourCustomDatasource extends Datasource<YourObject> {
 private objects: Observable<Obj[]>;

  filterCustom() {
   return objects.filter(elem => elem.isActive)
   .map(elem => elem)
   .subscribe(
    err => console.error(err),
  );
}

I am using mat-table.我正在使用垫子桌子。 I know about the build in filter.我知道内置过滤器。 And the filterPredicate .filterPredicate But filter predicate only accepts filterValue: string .但是filter谓词仅接受filterValue: string I want to be able to filter the table with slide toggle and show only objects that have for example isActive flag true or isActive flag false.我希望能够使用幻灯片切换功能过滤表格,并仅显示具有isActive标志为true或isActive标志为false的对象。 But default filter doesn't offer such functionality.但是默认过滤器不提供此类功能。 I figured out some workarounds myself but I hope there is a better solution.我自己想出了一些解决方法,但我希望有更好的解决方案。
1) I can filter the array myself but in that way I need to keep the initial array and pass the filtered array to the table. 1)我可以自己过滤该数组,但是以这种方式,我需要保留初始数组并将经过过滤的数组传递给表。 And I don't like the idea of using two arrays for the same information.而且我不喜欢将两个数组用于相同的信息的想法。
2) I can pass special value to the filter when slide toggle is changed for example &@changedToggle-true and check for that value in the filter predicate and filter only objects with the right isActive flag. 2)当滑动切换更改为&@changedToggle-true ,我可以将特殊值传递给过滤器,并在过滤器谓词中检查该值,并仅过滤具有正确的isActive标志的对象。 But that still doesn't look like good solution to me.但这对我来说仍然不是很好的解决方案。
Is there any good way for achieving what I want.有什么好的方法可以实现我想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM