How to make a filter that is picking data from JSON in ionic 2 rc3

时间:2016-12-02 04:47:50

标签: angular ionic2 typescript2.0

In my app I have JSON data with date. I want to show the data in day vice i.e., Today, yesterday etc. and I have a status called completed and pending. I wrote the ion-segment, in that segment I have completed and pending. I want to show the completed data in completed segment and pending data in pending segment.

image

Json Data Example with what my app will work.

`[{
    "day":"Today",
    "name":"Sandra Adams",
    "phno":8452514521,
    "tests": "Ecma, Hcbc...",
    "status":"pending"
}, {
    "day":"Yesterday",
    "name":"Nathan Jones",
    "phno":8452457845,
    "tests": "Ecma, Hcbc...",
    "status":"completed"
}]`

1 个答案:

答案 0 :(得分:0)

您应该使用Pipe

@Pipe({
  name: 'anyPipe'
})
class AnyPipe implements PipeTransform {
  public transform(values: any[], day: string) {
    if (!values || !values.length) return [];
    if (!day) return values;
    return values.filter(v => v.day === day);
  }
}

用法:

<list-item *ngFor="let item of (items | anyPipe:'Yesterday')"></list-item>