简体   繁体   English

Angular 4复选框管道过滤器

[英]Angular 4 checkbox pipe filter

Hello I am trying to make a custom pipe filter which filters the data when checkbox is checked. 您好,我正在尝试制作一个自定义管道过滤器,以在选中复选框时过滤数据。 I have a list of job locations which I want to filter when the certain job location is checked. 我有一个工作位置列表,当选中某个工作位置时,我想对其进行过滤。 I am providing the code which is related to the issue/problem. 我正在提供与问题/问题相关的代码。

Pipe code - 管道代码-

@Pipe({
  name: 'checkcity'
})
export class CheckcityPipe implements PipeTransform {

  transform(check: any, checked: any): any {
    let [loc] = checked;
    console.log('checked',checked);
    return checked
            ? check.filter(city => {return city.location }) 
            : check;
  }

}

Input Checkbox - 输入复选框-

<input type="checkbox" value="" [(ngModel)]="checked" name="checked"> Bangalore

Data to filter - 要过滤的数据-

<div *ngFor="let joblist of jobList | checkcity: checked">
    {{joblist.location}}
</div>

You should rename local var as joblist and the source as jobLists they should not be same. 您应该重命名局部变量作为joblist和源jobLists他们不应该是一样的。 Can you try with the below code 您可以尝试以下代码吗

@Pipe({
  name: 'checkcity'
})
export class CheckcityPipe implements PipeTransform {

  transform(check: any, checked: any): any {
    //let [loc] = checked;
    console.log('checked',checked);
    return checked
            ? check.filter(city =>  city.location == checked) 
            : check;
  }

}

<input type="checkbox" value="Bangalore" [(ngModel)]="checked" name="city"> Bangalore

<div *ngFor="let joblist of jobLists | checkcity: checked">
    {{joblist.location}}
</div>

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

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