我正在研究一个有角度的6 proj,我正在使用ng-bootstrap typeahead来搜索这个对象:
const options: SelectItem[] = [
{ name: 'home', id: 1 },
{ name: 'dashboard', id: 2 },
{ name: 'panel', id: 3 } ];
现在我只能按名称搜索:
search = (text$: Observable<string>) =>
text$.pipe(
debounceTime(200),
map(term => term === '' ? []
: options.filter(v => v.name.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
)
formatter = (result: string) => result.toUpperCase();
但是我想搜索我的搜索领域中的id和名字...任何身体都可以提供帮助吗?
只需更改功能搜索以包含新条件
search = (text$: Observable<string>) =>
text$.pipe(
debounceTime(200),
map(term => term === '' ? []
: options.filter(v =>
v.name.toLowerCase().indexOf(term.toLowerCase())>-1 ||
v.id==+term
).slice(0, 10))
)
看到,因为id是一个数字,所以你需要将术语转换为数字(+术语)
一个简单的例子可以在stackblitz中看到