我有看起来像这样的数据
[
{id: 1234,
Name: 'John',
Tags: ['tag1', 'tag2']
},
{id: 1235,
Name: 'Mike',
Tags: ['tag1', 'tag3']
}
]
我希望能够在搜索栏中输入并过滤数据以搜索相关标签。在 angular 1 中有一个内置的过滤器管道,但看起来它已在 angular 2 中删除。我一直在研究自定义管道,但我能想到的唯一方法是使用嵌套循环来循环然后所有对象循环遍历标签。我在想这个错误吗。有没有更简单的方法来做到这一点,或者有一个内置的功能可以做到这一点?
您可以只使用普通的 javascript API 来获得该行为:
data = [
{id: 1234,
Name: 'John',
Tags: ['tag1', 'tag2']
},
{id: 1235,
Name: 'Mike',
Tags: ['tag1', 'tag3']
}
];
filterDataByTag(searchTerm: string) {
// filter the data array, based on some condition
return this.data.filter(item => {
// only include an item in the filtered results
// if that item's Tags property includes the searchTerm
// includes is a built in array method in ES2016
return item.Tags.includes(searchTerm);
});
}
在我的示例中,我正在努力编码data
,但您可以根据自己的情况进行调整。关键是该函数返回一个data
基于 searchTerm的过滤列表,因此您可以在每次要刷新过滤列表时调用此方法(例如在搜索字段的输入事件上)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句