简体   繁体   English

通过Angular 6中的嵌套值过滤HTTP JSON响应

[英]Filter HTTP JSON response by nested value in Angular 6

I want to fetch some JSON data from an external source and display it in a <select> - & <option> -Tag in my view. 我想从外部来源获取一些JSON数据,并在视图中的<select> -& <option> -Tag中显示它们。 So far so good. 到现在为止还挺好。

Some JSON entries have a private: false value. 一些JSON条目具有一个private: false值。 And this is where my question comes in. My destination is it, to only show the user the entries containing the private value set to false. 这就是我的问题所在。我的目的地是它,仅向用户显示包含设为false的private值的条目。

I already looked for JSON filter and found out that I could set a filter in my view in ngFor ( let appointmentType of appointmentTypes | filter: { private: false } ), but I get an error message telling me that The pipe 'filter' could not be found . 我已经在寻找JSON过滤器,发现可以在ngFor的视图中设置过滤器( let appointmentType of appointmentTypes | filter: { private: false } ),但是我收到一条错误消息,告诉我The pipe 'filter' could not be found

This is the URL I had my solution from: ng-repeat :filter by single field 这是我从以下解决方案获得的网址: ng-repeat:按单个字段过滤

This is the JSON response: 这是JSON响应:

[
  {
    "id": 5780379,
    "name": "Appointment Type 1",
    "description": "",
    "duration": 15,
    "price": "290.00",
    "category": "REGULAR",
    "color": "#3177CA",
    "private": false
  },
  {
    "id": 5780481,
    "name": "Appointment Type 2",
    "description": "",
    "duration": 15,
    "price": "39.00",
    "category": "SERVICE",
    "color": "#D8394F",
    "private": true
  }
]

This is my HTML 这是我的HTML

<select type="appointmentType" [(ngModel)]="appointmentTypeId">
  <option *ngFor="let appointmentType of appointmentTypes | filter: { private: false }" [ngValue]="appointmentType.id">{{appointmentType.name}}</option>
</select>
{{appointmentTypeId}}

This is my Component.TS File: 这是我的Component.TS文件:

import { Appointment } from './../../appointments/appointment';
import { Component, OnInit } from '@angular/core';
import { APIService } from './../../../api.service';

@Component({
  selector: 'app-booking',
  templateUrl: './booking.component.html',
  styleUrls: ['./booking.component.scss']
})
export class BookingComponent implements OnInit {
  private appointmentTypes: Array<object> = [];
  appointmentTypeId: any;

  constructor(private apiService: APIService) {}

  ngOnInit() {
    this.getData();
    console.log(this.appointmentTypeId);
  }

  public getData() {
    this.apiService.getAppointmentTypes().subscribe((data: Array<object>) => {
      this.appointmentTypes = data;
      console.log(data);
    });
  }
}

And don't know if it's necessary, but here's my api.service.ts: 而且不知道是否有必要, 但这是我的api.service.ts:

getAppointmentTypes() {
  return this.httpClient.get(`${this.API_URL}/appointment-types/`);
}

As I said, I managed it do display the JSON Response entries in the select option, but I only want to provide the ones where private is set to false. 就像我说的那样,我管理它确实在select选项中显示JSON Response条目,但是我只想提供private设置为false的条目。

The filter in angularjs is different from pipe in angular . angularjs中的过滤器与angular中的管道不同。 you need to implement own custom pipe to make your logic work. 您需要实现自己的自定义管道以使逻辑工作。

your pipe should be like this, 你的烟斗应该是这样的

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'filter' })
export class filterPipe implements PipeTransform {
  transform(categories: any, searchText: any): any {
     let result = categories.filter(t=>t.private == searchText);
     return result;
  }
}

STACKBLITZ DEMO

You are going to have to create your own custom pipe to do the filtering of the users list. 您将必须创建自己的自定义管道来过滤用户列表。

Angular doesn't provide pipes for filtering or sorting lists. Angular不提供用于过滤或排序列表的管道。 Developers familiar with AngularJS know these as filter and orderBy. 熟悉AngularJS的开发人员将其称为filter和orderBy。 There are no equivalents in Angular. Angular中没有等效项。

https://angular.io/guide/pipes#appendix-no-filterpipe-or-orderbypipe https://angular.io/guide/pipes#appendix-no-filterpipe-or-orderbypipe

This decision was made because of he inheret performance implications of allowing these types of native pipes. 之所以做出此决定,是因为他允许使用这些类型的本机管道会对性能产生影响。

I have put together a stackblitz with the solution here: https://stackblitz.com/edit/angular-pqeky2 我在这里将解决方案放到了stackblitz上: https ://stackblitz.com/edit/angular-pqeky2

As you can see the third user who has privacy: false does not appear in the select list because it is filtered out by the custom pipe. 如您所见,第三个具有隐私权的用户:false不会出现在选择列表中,因为它已被自定义管道过滤掉。

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

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