[英]How to create pipe to filter list in Angular2
I'm facing problem with creating the pipe to filter list from input.我在创建管道以从输入过滤列表时遇到问题。
What I would like to have is something like this: http://www.w3schools.com/howto/howto_js_filter_lists.asp我想要的是这样的: http : //www.w3schools.com/howto/howto_js_filter_lists.asp
Is there anybody who can help me with creating pipe for this?有没有人可以帮助我为此创建管道?
Update , I changed the code with one proposition and it is still not working with my part of code.更新,我用一个命题更改了代码,但它仍然不适用于我的代码部分。
Some parts from my code:我的代码中的一些部分:
component.html:组件.html:
<input id="desc" type="text" placeholder="Alarm name" [(ngModel)]="desc"> <ul> <li *ngFor="let name of names" [style.display]="(name | search : desc) ? 'block' : 'none'"> {{ name }} </li> </ul> <div class="alarm-list-item" *ngFor="let alarm of alarmsList" [style.display]="(alarm.Description | search : desc) ? 'block' : 'none'"> {{alarm.Description }} </div>
alarmList is an array: alarmList 是一个数组:
enter image description here在此处输入图片说明
search.pipe.ts also I had to change the pipe code as "contains" doesn't work and I change type to any: search.pipe.ts我也不得不更改管道代码,因为“包含”不起作用,我将类型更改为任何:
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'search' }) export class SearchPipe implements PipeTransform { transform(list: any, searchText: any): any { for(let i=0; i<list.length; i++){ if(list[i].includes(searchText)){ return true; } } return false; } }
You're not supposed to do that with a Pipe I believe.我相信你不应该用 Pipe 来做这件事。
What you want is to perform a pipe operation on your itemssource 'alarmlist' which angular 2 won't allow.您想要的是对 angular 2 不允许的 itemssource 'alarmlist' 执行管道操作。
What you could do though is something like this, which I'd not advise since it's ugly code:你可以做的是这样的事情,我不建议这样做,因为它是丑陋的代码:
https://plnkr.co/edit/3e6cFSBFIf0uYYIgKNZ8?p=preview https://plnkr.co/edit/3e6cFSBFIf0uYYIgKNZ8?p=preview
My advice would be to make another property: filteredAlarmList which you would edit in the (change)
event of the <input>
or the setter of your desc property and then rewrite your ngFor like *ngFor="let alarm of filteredAlarmList"
我的建议是创建另一个属性:filteredAlarmList,您可以在
<input>
的(change)
事件或 desc 属性的设置器中对其进行编辑,然后重写您的 ngFor,如*ngFor="let alarm of filteredAlarmList"
If you need this kind of filtering multiple times you could extract all of this to a seperate components.如果您需要多次进行这种过滤,您可以将所有这些提取到单独的组件中。 eg: FilterListComponent which will take 2 inputs: searchText and List (in your case AlarmList)
例如:FilterListComponent 将采用 2 个输入:searchText 和 List(在您的情况下为 AlarmList)
something like this could help这样的事情可能会有所帮助
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'search',
pure: true
})
export class SearchPipe implements PipeTransform {
transform(value: any, args:string=''): any {
console.log(value);
if(!value) return value;
else {
if(value.toLowerCase().indexOf(args.toLowerCase())!= -1) {
return value;
}
}
}
}
In your component在您的组件中
@Component({
selector: 'my-app',
template: `
<input type="text" [(ngModel)]="filterValue">
<ul>
<li *ngFor="let name of names">{{name | search:filterValue}}</li>
</ul>`,
})
export class AppComponent {
names:Array<string> = ['Agnes','Adele','Binny','Bob'];
constructor(){
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.