按布尔值过滤
我试图按用户状态进行过滤,“Ativo”表示在线,“Inativo”表示离线,但是按字符串过滤让我有些头痛,因为当我输入“ativo”时,所有离线用户仍然会出现,因为“inativo”中存在“ativo”一词。所以我想为“inativo”附加一个错误值,为“ativo”附加一个真实值。
关于如何做到这一点有什么建议吗?我仍然什么过滤器显示“Ativo”和“Inativo”作为数据列表中的选项,但是当选择它时,它应该向过滤器函数发送一个 true 或 false 值
这是我创建的 ts 上的 html 和函数
<input type="text" list="status" placeholder="Status" (keyup)="updateFilter($event)" id="stat">
<datalist id="status">
<option>Ativo</option>
<option>Inativo</option>
</datalist>
updateFilter(event: Event) {
this.filterValue = (event.target as HTMLInputElement).value;
this.dataSource.filter = this.filterValue.trim().toLowerCase();
console.log(this.dataSource.filter);
if (this.dataSource.paginator) {
this.dataSource.paginator.firstPage();
}
}
im trying to make a filter by the status of the user, "Ativo" for online and "Inativo" for offline, but filtering by string is giving me some headache because when i type "ativo" all the offline users still show up because the word "ativo" exists in "inativo". So i wanted to attach a false value to 'inativo' and a true to 'ativo'.
Any tips on how do i do that? I still what the filter to show "Ativo" and "Inativo" as the options in the datalist, but when selected it should send a true or false value to the filter function
This are the html and the function on ts i created
<input type="text" list="status" placeholder="Status" (keyup)="updateFilter($event)" id="stat">
<datalist id="status">
<option>Ativo</option>
<option>Inativo</option>
</datalist>
updateFilter(event: Event) {
this.filterValue = (event.target as HTMLInputElement).value;
this.dataSource.filter = this.filterValue.trim().toLowerCase();
console.log(this.dataSource.filter);
if (this.dataSource.paginator) {
this.dataSource.paginator.firstPage();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Angular Material 提供了一种方法来说明如何通过 filterPredicate 方法将过滤器应用于数据: https://material.angular.io/components/table/api#MatTableDataSource
为您的数据源提供一个箭头函数,表示值应该严格相等而不是相似应该可以解决您的问题。
Angular Material provides a way to say how the filter should be applied to the data via the filterPredicate method: https://material.angular.io/components/table/api#MatTableDataSource
Providing an arrow function to your datasource that says values should strictly be equal instead of alike should solve you problem.
首先,如果您的过滤器具有严格的值,则不应使用 datalist,而应使用
select
。它的工作原理相同,但不允许用户输入输入。现在,您没有理由遇到此类问题。只需检查
(event.target as HTMLInputElement).value === 'ativo'
是否就足够了。我建议创建一个枚举以避免代码中出现魔术字符串,因此您将拥有
UserStatus.ativo
而不是'ativo'
。First, if your filter has strict value, you shouldn't use datalist but
select
. It works the same but doesn't allow the user to type in the input.Now, there is no reason you encounter this kind of problem. Just checking if
(event.target as HTMLInputElement).value === 'ativo'
should be enough.I recommand creating an enum to avoid magic string in your code, so instead of
'ativo'
you will haveUserStatus.ativo
.您可以使用正则表达式来做到这一点:
You can use regex to do that :