【发布时间】:2019-07-02 14:39:40
【问题描述】:
我正在尝试在不使用任何管道的情况下在表内构建搜索。这就是我现在所拥有的:
.ts
get filteredArray() {
if (this.searchValue.length === 0) {
return this.usersList
}
return this.usersList.filter((user) => {
return (this.searchValue.length > 0 ? this.searchValue.indexOf(user.name) !== -1 : true) ||
(this.searchValue.length > 0 ? this.searchValue.indexOf(user.group) !== -1 : true) ||
(this.searchValue.length > 0 ? this.searchValue.indexOf(user.age) !== -1 : true)
})
}
inputClick(searchText) {
if (searchText != "") {
this.searchValue.push(searchText)
} else {
this.searchValue.splice(0, 1)
}
}
.html
<input type="text" [(ngModel)]="searchText" (keyup.enter)="inputClick(searchText)">
<table>
<thead>
<tr>
<td><strong>Name</strong></td>
<td><strong>Group</strong></td>
<td><strong>Age</strong></td>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of filteredArray">
<td>{{ user.name }}</td>
<td>{{ user.group }}</td>
<td>{{ user.age }}</td>
</tr>
</tbody>
</table>
这很好用(如果您在input 中输入一些内容并按回车键,它将出现,如果您删除并按回车键,它将恢复到初始数组)
如您所见,为此我过滤了列表的每个字段:
(this.searchValue.length > 0 ? this.searchValue.indexOf(user.name) !== -1 : true) ||
(this.searchValue.length > 0 ? this.searchValue.indexOf(user.group) !== -1 : true) ||
(this.searchValue.length > 0 ? this.searchValue.indexOf(user.age) !== -1 : true)
我的问题是:如何避免在我的退货声明中填写所有字段?因为在我的数据库中,我有 30 多个字段,并且很难写出 30 个不同的||。
另外,如果我写joh而不是john,我该如何修改我的代码,以仍然找到条目?
【问题讨论】:
标签: html angular typescript angular6