ng-bootstrap 所有对象属性的预输入格式
ng-bootstrap typeahead format on all object properties
我正在开发一个 angular 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 和名称...
任何机构都可以提供帮助?
只需更改函数搜索以包含新条件
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是一个数字,所以你需要将term转换成数字(+term)
一个简单的例子可以在stackblitz
中看到
我正在开发一个 angular 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 和名称... 任何机构都可以提供帮助?
只需更改函数搜索以包含新条件
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是一个数字,所以你需要将term转换成数字(+term)
一个简单的例子可以在stackblitz
中看到