简体   繁体   English

按数组过滤数组,具有真或假值 Angular

[英]Filter array by array with true or false value Angular

A have a client class I have a column with a sting column isSimple 'TAK' or 'NIE' (YES or NO in English) in my Class. A 有一个客户 class 我在我的 Class 中有一个带有 sting 列 isSimple 'TAK' 或 'NIE'(英文是或否)的列。 In this class a have an argument a Canal and this class I have an argument 'Procudent' or 'Handel'.在这个 class 中有一个参数是运河,而这个 class 我有一个参数“Procudent”或“Handel”。 In my Reactive Form a have a form where a may a filter a Producent, Handel and Simple and I send this information to my HomeController.在我的反应式表单中,有一个表单可以过滤 Producent、Handel 和 Simple,我将此信息发送到我的 HomeController。 Now I want to filter this clients where I clicked true in my input near Handel, Producent or Simple by this columns.现在,我想通过此列过滤这些客户,在这些客户中,我在 Handel、Producent 或 Simple 附近的输入中单击了 true。

My code is:我的代码是:

I recive this data in my HomeController array:我在 HomeController 数组中接收到这些数据:

 [
    { name: "Producent", checked: true },
    { name: "Handel", checked: true },
    { name: "Simple", checked: true }
  ];

My class client:我的 class 客户端:

export class Client {
    clientId: number;
    name: string ;
    district: string;
    province: string;
    zip: string;
    city: string;
    full_Address: string;
    latitude: number;
    longitude: number;
    segment: string;
    ph: string;
    bh: number;
    canal: string;
    isSimple: string;
}

I send through this class:我通过这个 class 发送:

import { Component, EventEmitter, OnInit, Output } from '@angular/core';
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
import { IBox } from 'src/app/Models/IBox';

@Component({
  selector: 'app-type-of-client',
  templateUrl: './type-of-client.component.html',
  styleUrls: ['./type-of-client.component.css']
})
export class TypeOfClientComponent implements OnInit {
  box_data: IBox[] = [
    { name: "Producent", checked: true },
    { name: "Handel", checked: true },
    { name: "Simple", checked: true }
  ];

  @Output() typeOfClient = new EventEmitter<{}>();


  form_model: FormGroup = this.fb.group({
    search_text: this.fb.control(""),
    boxes: this.fb.array(
      this.box_data.map((box: IBox) => {
         return this.fb.group({ checked: [box.checked],name:[box.name] });
      })
    )
  })

  constructor(private fb: FormBuilder) { }

  get boxes() {
    return this.form_model.get("boxes") as FormArray;
  }

  ngOnInit(): void {
          this.boxes.valueChanges.subscribe(
              (response) =>{

                let clients : IBox[] =[];
                for (let index = 0; index < response.length; index++) {
                  const element = response[index];
                  clients.push(element);
                }

                // let clients : IBox[] = this.box_data;
                this.typeOfClient.emit(clients);
              }
    );
  }
}

And now a want to try use method method include but it doesn't work.现在想尝试使用方法方法包括但它不起作用。 I filter like this my client throung another argument.我像这样过滤我的客户提出了另一个论点。

 filterClients() {
    console.log(this.checkedPH);
    console.log(this.checkedSegment);

    const typeClient = this.checkedTypeOfClient


    this.currentClientList = this.baseClientList;
    this.currentClientList = this.currentClientList.filter(client => {
       return this.checkedSegment.includes(client.segment) && this.checkedPH.includes(client.ph);

    });

  }

In general you has a parent component that received the value通常,您有一个父组件接收该值

<!--see that you received the output using "$event"-->
<app-type-of-client (typeOfClient)="filter($event)"></app-type-of-client>

if your parent component I mush suppose you has an array with all the clients, use an auxiliar variable filterClients如果您的父组件我必须假设您有一个包含所有客户端的数组,请使用辅助变量 filterClients

allClients:Clients[];
filterClients:Clients[]=[]

The problem is that you received an strange object.问题是您收到了一个奇怪的 object。 I'm going to transform this response before make the filter我将在制作过滤器之前转换此响应

So, create a function所以,创建一个 function

    filter(response:any[]){
        const filter={
           Producent:response.find(x=>x.name="Producent").checked
           Handel:response.find(x=>x.name="Handel").checked
           Simple:response.find(x=>x.name="Simple").checked
        }

        this.filterClients=this.allClients.filter(x=>(x.canal=="Producent" && filter.Producent) &&
                                                     (x.canal=="Handel" && filter.Handel) &&
            ((x.Simple=="TAK" && filter.Simple) || (x.Simple=="NIE" && !filter.Simple)))
    }

And iterate over filterClients.并迭代 filterClients。

NOTE: if you always received the three values and the values are in order.注意:如果您总是收到三个值并且这些值是按顺序排列的。 Some like有些喜欢

this.boxes.valueChanges.subscribe(res=>{
  this.typeOfClient.emit(res.map(x=>x.checked)) //<--this emit, e.g. [true,false,true]
})

You can filter like你可以过滤喜欢

    filter(response:any[]){
        this.filterClients=this.allClients.filter(x=>(x.canal=="Producent" && response[0]) &&
                                                     (x.canal=="Handel" && response[1]) &&
            ((x.Simple=="TAK" && response[2]) || (x.Simple=="NIE" && !response[2])))
    }

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

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