angular - Angular Material autocomplete 的 mat-option 有两个可搜索的值
问题描述
我是 Angular 的新手,我有一个 mat-autocomplete 作为搜索字段,我希望我的每个 mat-option 都可以使用 2 个值进行搜索,例如使用名称和 id:
options = [{name: 'x', id: 123},{name: 'y', id:142}]
任何人都知道我该怎么做?
解决方案
如果您查看 Angular Material Autocomplete示例,尤其是称为过滤器自动完成的示例,您将看到过滤是如何完成的,并且您可以轻松地将其扩展为过滤多个值。
从示例中(请记住,这里的options
数组只是一个字符串数组,因此不会访问/过滤任何属性):
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();
return this.options.filter(option => option.toLowerCase().includes(filterValue));
}
我们可以将其重写为以下内容以过滤附加id
属性(假设options
是具有属性的对象数组name
和id
):
private _filter(value: string): Option[] {
const filterValue = value.toLowerCase();
return this.options.filter(option => String(option.id).toLowerCase().indexOf(filterValue ) > -1 ||
option.name.toLowerCase().indexOf(filterValue ) > -1);
}
这 是一个显示上述过滤的 stackblitz。
推荐阅读
- python - 为什么我无法使用正则表达式从该日志数据中提取数据?
- python - Google Classroom Python 也制作返还课程材料
- c++ - cygwin.s 文件缺少 C++ Visual Studio 代码
- verilog - verilog 错误 (13069) 和 (13205)。使用 `define 时发生
- c# - WPF 应用程序在 HttpClient PostAsync 方法处无响应或异常退出
- r - 在 R 中使用 ggplot2 绘制卡方分布
- go - 如何根据elasticsearch中的输入字段获取字段的总和值(输入字段和总和输出字段不同)
- c# - MongoDB find 查询需要 15 分钟,失败,然后立即成功
- html - 为什么 ul 列表超出 div class="col"
- amazon-web-services - 安全组 sg-0da667222da8a6eb2 似乎不属于与输入子网相同的 VPC