我有这个型号:
export class food_items {
constructor(
public itemname: string,
) {}
}
export class Foods {
constructor(
public name: string,
public items: food_items[]
) {}
}
和数据:
foods: Foods [];
getTable(){
this.foods=[
{
"name": "fruits"
"items":[
{"itemname": "apple"},
{"itemname": "banana"},
{"itemname": "orange"}
]},
{
"name": "vegetables"
"items": [
{"itemname": "carrot"},
{"itemname": "cucumber"},
{"itemname": "tomato"},
]}
}
并使用pipe按itemname过滤它们:
<div *ngFor="let food of foods>
{{food.name}}
<div *ngFor="let item of items | myfilter: filterargs">
{{item.itemname}}
管:
export class MyFilter implements PipeTransform {
temp = [];
transform (value, queryString) {
this.temp.length = 0;
let arr=value.filter((item)=> new
RegExp(queryString.toLowerCase()).test(item.itemname.toLowerCase()));
for(let i=0; i<arr.length; i++){
this.temp.push(arr[i]);
}
return this.temp;
}
}
如果在filterargs中放入胡萝卜,我将会这样:
fruits
vegetables
carrot
我的问题是如何删除水果元素或其他空母元素不包含filterargs?感谢。