【问题标题】:Exclude or include the particular row from the mat-table view从 mat-table 视图中排除或包含特定行
【发布时间】:2018-09-30 19:32:33
【问题描述】:

我有一个像idtitledescription这样的材料数据表
我的mat-table的数据源用dataSource$: Observable<Thing[]>表示

<mat-table #table [dataSource]="dataSource$ | async">
...
<mat-table>

基于下拉列表,我希望能够显示所有数据(目前的工作方式),但也可以隐藏 description 为空的项目,并隐藏具有 description 不的项目空。

我想我必须通过自定义管道来做到这一点?或者在我的 observable 上使用 .filter()
任何想法如何最好地接近它?

更新:

我正在尝试使用.filter(),但遇到问题,没有显示数据:

dataSource$ = originalDataSource$.filter((item: any) => item.description == null)

想法?

【问题讨论】:

  • 如你所说,filter你的dataSource$
  • @Vega 我有 3 个条件,show allshow only with descriptionshow only without descirption,所以这可能行不通。
  • @Vega 在ngif 的情况下,我必须在我的mat-table 的每个&lt;ng-container&gt; 中复制ngif 语句,这并不酷。
  • @Vega 有关如何在这里接近ng-template 的更多详细信息?

标签: angular filter pipe


【解决方案1】:

为此,您必须像originalDataSource$ 一样单独维护原始dataSource$。在(change)="onChange($event.target.value)" 之类的下拉列表中写入更改事件,并在该函数中过滤您的数据,例如

onChange(value){  
 dataSource$ = originalDataSource$.filter(m =>{ your logic/conditions })
}

【讨论】:

  • 我正在尝试这种方法但有一个问题,你能看到更新的部分吗?
  • 大声笑。您正在过滤空数据。你的情况应该是filter((item: any) =&gt; item.description !== null)
  • 如何在过滤器中添加 3 个条件 1) 跳过过滤器,2) 如果描述为空,则过滤 3) 如果描述不为空,则过滤?
  • 你可以制作函数并放置多个条件。参考这个 - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
猜你喜欢
  • 2019-10-10
  • 2018-06-23
  • 2016-10-17
  • 2019-01-06
  • 2016-04-21
  • 2015-07-25
  • 1970-01-01
  • 2022-10-26
  • 2021-04-04
相关资源
最近更新 更多