所有对象属性上的ng-bootstrap typeahead格式

时间:2019-03-04 08:21:21

标签: angular ng-bootstrap typeahead

我正在研究角度为6的项目,并且正在使用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和名称... 任何机构都能帮忙吗?

1 个答案:

答案 0 :(得分:0)

只需更改功能搜索以包括新条件

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

中看到
相关问题