angular - Ionic 3如何过滤嵌套在2 *ngFor中的数组?

标签 angular ionic3

我想根据搜索输入过滤 day.children name 属性。嵌套 *ngFor 遇到困难,因为它依赖于第二个。 dayOverViewByGroup 来自 API。 我尝试将 day.children 包装在 filterIt(day.children) 这样的函数中,但出现了无限循环。谢谢。

我有以下(简化的)html 结构

<ion-searchbar placeholder="Suchen..." (ionInput)="searchChildren($event)" padding></ion-searchbar>
<ion-grid *ngFor="let day of dayOverViewByGroup; let i = index" padding>
  <ion-card *ngFor="let child of day.children; let j = index">
  <h1>{{child.name}}</h1>
</ion-grid>

来自 API 的 dayOverview 变量示例数据

// dayOverview variable example data from API

[
  {
    "name": "Kindergarden 1",
    "presence_id": 25,
    "totalChildren": 3,
    "totalCheckinChildren": 1,
    "children": [
      {
        "name": "John Doe",
        "daycareComment": null,
        "parentComment": null,
        "id": "10633",
        "away": null,
        "checkin": [],
        "additionalDay": false,
        "remarks": "",
        "awayClass": "",
        "reason": "",
        "addtionalClass": "",
        "disableToggle": false,
        "disabled": false,
        "checkout": "Nicht anwesend",
        "class": "notcheckin"
      },
      {
        "name": "Jane Doe",
        "daycareComment": null,
        "parentComment": null,
        "id": "8322",
        "away": null,
        "checkin": [],
        "additionalDay": false,
        "remarks": "",
        "awayClass": "",
        "reason": "",
        "addtionalClass": "",
        "disableToggle": false,
        "disabled": false,
        "checkout": "Nicht anwesend",
        "class": "notcheckin"
      },
      {
        "name": "Bastian Paper",
        "daycareComment": null,
        "parentComment": null,
        "id": "86999",
        "away": null,
        "checkin": [],
        "additionalDay": false,
        "remarks": "",
        "awayClass": "",
        "reason": "",
        "addtionalClass": "",
        "disableToggle": false,
        "disabled": true,
        "class": "checkin",
        "checkout": "Anwesend"
      }
    ]
  },
  {
    "name": "Kindergarden 2",
    "presence_id": 26,
    "totalChildren": 1,
    "totalCheckinChildren": 0,
    "children": [
      {
        "name": "Thomas Mueller",
        "daycareComment": null,
        "parentComment": null,
        "id": "86900",
        "away": null,
        "checkin": [],
        "additionalDay": false,
        "remarks": "",
        "awayClass": "",
        "reason": "",
        "addtionalClass": "",
        "disableToggle": false,
        "disabled": false,
        "checkout": "Nicht anwesend",
        "class": "notcheckin"
      }
    ]
  },
  {
    "name": "Kindergarden 3",
    "presence_id": 27,
    "totalChildren": 1,
    "totalCheckinChildren": 0,
    "children": [

    ]
  }
]

最佳答案

添加一个 Observable filteredChildren$ ,将过滤后的子项发送到您的 day 对象。

要创建filteredChildren$,您需要一个过滤器值的可观察对象。将 FormControl 添加到搜索栏,以便您可以监听其值的变化。 (您必须导入ReactiveFormsModule)

Demo (Angular 8, Ionic 4, RxJS 6)

Demo (Angular 5, Ionic 3, RxJS 5.5)

<ion-searchbar placeholder="Suchen..." [formControl]="filterControl" padding></ion-searchbar>
<ion-grid *ngFor="let day of dayOverViewByGroup$ | async; let i = index" padding>       
  <ion-card *ngFor="let child of day.filteredChildren$ | async; let j = index">
    <h1>{{child.name}}</h1>
  </ion-card>
</ion-grid>
export class Component {
  filterControl = new FormControl();
  dayOverViewByGroup$: Observable<any>

  ngOnInit() {
    const filter$: Observable<string> = this.filterControl.valueChanges.pipe(
      startWith(''),
      debounceTime(100), 
      distinctUntilChanged(),
      share() // we use filter$ multiple times so we share a single subscription to the source
    );
    this.dayOverViewByGroup$ = this.fetchData().pipe(
      map(days => days.map(day => (
        {
          ...day,
          // add an Observable that emits an array of filtered children
          filteredChildren$: filter$.pipe(this.filterFrom(day.children))
        }
      )))
    );
  }

  fetchData(): Observable<any[]> {
    // fetch data from Api
    return of(data);
  }

  filterFrom(children: { name: string }[]) {
    return (filter$: Observable<string>) => filter$.pipe(
      // the filter logic (add your own here)
      map(f => children.filter(child => child.name.toLowerCase().indexOf(f.toLowerCase()) > -1))
    );
  }
}

关于angular - Ionic 3如何过滤嵌套在2 *ngFor中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59627540/

相关文章:

angular - 如何在 Ionic 3 中禁用模态动画?

css - 在 Ionic2 中设置 ionic 行高度大小

angular - 如何在 ionic 2 中正确使用(点击)?

html - Angular 4.x : How to bind table checkbox with button in Angular 4. x?

json - 在 Angular 7 项目中导入 JSON

javascript - ionic v3 : Group list by date/day

angular - ionic2 点击与点击

typescript - Angular 2 - 通过组件更改全局服务中的接口(interface)并更新另一个组件中更改的接口(interface)

javascript - 在Angular 2中YouTube iFrame事件发生后更新 View

angular - 为什么用于生成解析器的 cli 命令会抛出异常?