使用管道选择Angular 4中的Option Filter

时间:2017-09-15 06:22:19

标签: json angular typescript

我正在尝试创建一个带有selectbox下拉列表的管道,用于过滤json数据中的列表。我用selectbox管道创建了一个管道。我无法让我的过滤器在管道中工作。请帮忙。这是我的代码 -

选择框 -

<select class="form-control" [(ngModel)]="sel" name="sel">
   <option selected disabled>Select</option>
    <option *ngFor="let positionOpt of positionSelect" [value] = "sel">{{positionOpt.name}}</option>
</select>

SelectBox选项字段的数据 -

positionSelect:any[] = [
{
  name: "Social Media Manager",
  position: "Social Media Manager"
},
{
  name: "Product Manager",
  position: "Product Manager"
}
]

选择框的管道 -

import { Pipe, PipeTransform } from '@angular/core';
import { JobsService } from '../services/jobs.service';

@Pipe({
  name: 'selectbox'
})
export class SelectboxPipe implements PipeTransform {

  transform(opt: any, sel?: any): any {
    return (opt || opt === '0')
        ? opt.filter(sal => { return sal.position == sel})
        : opt;
}

}

工作清单数据 -

<ul class="jobs-list">
    <li *ngFor="let joblists of jobList | selectbox: sel">
         {{joblists.position}}
    </li>
</ul>

Json中的jobList数据来自服务。我应该在jobList的select选项字段中使用* ngFor,还是来自不同的json数据。请帮助选择框过滤器。

2 个答案:

答案 0 :(得分:4)

试试这个:

import { Pipe, PipeTransform } from '@angular/core';
import { JobsService } from '../services/jobs.service';

@Pipe({
  name: 'selectbox'
})
export class SelectboxPipe implements PipeTransform {

    transform(items: any, sel?: any): any {
        return sel ? items.filter(sal => sal.position === sel) : items;
    }
}
<select class="form-control" [(ngModel)]="sel" name="sel">
    <option *ngFor="let positionOpt of positionSelect" [ngValue]="positionOpt.position">{{positionOpt.name}}</option>
</select>

<ul class="jobs-list">
    <li *ngFor="let joblists of jobList | selectbox: sel">
         {{joblists.position}}
    </li>
</ul>

答案 1 :(得分:3)

试试这个: 我得到了你在下拉列表中选择的对象的sel值。该值显示在selectbox.pipe.ts console.log

<强> app.component.html

<select class="form-control" name="sel" id="sel" [(ngModel)]="sel">
    <option [ngValue]="undefined" disabled  selected>Select...</option>
    <option *ngFor="let positionOpt of positionSelect" [ngValue]="positionOpt">{{positionOpt.name}}</option>
</select>

<ul class="jobs-list">
    {{sel}}
    <li *ngFor="let joblists of jobList | selectbox: sel">
         {{joblists.position}}
    </li>
</ul>

<强> app.component.ts

export class AppComponent {
    private sel: any;
    private jobList: any[];
    private positionSelect: any[] = [{
        name: "Social Media Manager",
        position: "Social Media Manager"
    },
    {
        name: "Product Manager",
        position: "Product Manager"
    }]
}

<强> selectbox.pipe.ts

import { Component, NgModule, Pipe, PipeTransform } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'

@Pipe({
    name: 'selectbox'
})

export class SelectboxPipe implements PipeTransform {
    transform(opt: any, sel?: any): any {
        console.log('sel', sel);
        return (opt || opt === '0') ? opt.filter(sal => { return sal.position == sel }) : opt;
    }
}

<强> app.module.ts

import { SelectboxPipe } from './selectbox.pipe';

@NgModule({
    declarations: [
        SelectboxPipe
    ],
    exports: [   
        SelectboxPipe
    ]
})

enter image description here