【发布时间】:2018-12-05 11:35:34
【问题描述】:
I have this working snippet。我想使用复选框过滤表格。问题是,例如,当我检查 a 时,它给了我过滤后的数组。但是当我取消选中它时,我想将原始数组返回给我。
.html
<ng-container *ngFor="let group of groups">
<label class="btn btn-filter" id="bttns">
<input type="checkbox" name="customersGroupFilter" autoComplete="off" [value]="group" (change)="changeGroup($event)">
{{ group }}
</label>
</ng-container>
<table>
<thead>
<tr>
<th>Name</th>
<th>Group</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of newArray">
<td>{{user.name}}</td>
<td>{{user.group}}</td>
</tr>
</tbody>
</table>
.ts
changeGroup(event) {
const group = event.target.value;
const index = this.groupValue.indexOf(group);
if (index > -1) {
this.groupValue.splice(index, 1);
} else {
this.groupValue.push(group);
}
this.transform(this.usersList, 'group', this.groupValue)
}
transform(items: any[], field: string, value: string[]): any[] {
console.log(value)
if (!items) {
return [];
}
if (!field || !value || value.length <= 0) {
return items;
}
this.newArray = items.filter(singleItem => {
return (singleItem != null && singleItem[field] != null && singleItem[field] != undefined && value.indexOf(singleItem[field]) >= 0);
});
return this.newArray
}
如何修改此代码以获得我想要的?
我在changeGroup()中尝试了类似这样的条件
if (this.groupValue.length == 0) {
this.transform(this.usersList, '', this.groupValue)
}
另外,这是过滤表格的好方法吗?我试着避开pipe filters。感谢您的宝贵时间!
【问题讨论】:
-
Java != Javascript ...
-
哎呀,我的错!我没注意 :D 修改了。
标签: javascript html angular angular6