简体   繁体   English

带有复选框的Angular 6过滤器表

[英]Angular 6 filter table with checkboxes

I have this working snippet . 我有这个工作片段 I want to filter a table using checkboxes. 我想使用复选框过滤表。 The problem is when I check a for example, it gives me the filtered array. 问题是当我检查a例如,它给了我过滤后的数组。 But when I uncheck it, I want to return me the original array. 但是当我取消选中它时,我想将原始数组返回给我。

.html 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>&nbsp;
</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 .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
  }

How can I modify this code in order to obtain what I want? 如何修改此代码以获得所需的信息?

I tried something with a condition like this in changeGroup() 我在changeGroup()尝试了这样的条件

if (this.groupValue.length == 0) {
   this.transform(this.usersList, '', this.groupValue)
}

Also, is this a good way to filter a table? 另外,这是筛选表格的好方法吗? I tried avoiding pipe filters . 我尝试避免使用pipe filters Thank you for your time! 感谢您的时间!

UPDATED!! 更新!!

You are deleting items from the original Array therefore you no longer have the data. 您正在从原始数组中删除项目,因此不再有数据。

You can use an immutable approach so using a spread operator create a new version of that array which you then splice and then when the checkbox is unchecked you reset the array back to the original value. 您可以使用不可变的方法,因此使用散布运算符可以创建该数组的新版本,然后进行拼接,然后取消选中该复选框,即可将数组重置为原始值。

originalArray = [1,2,3,4]
newArray = [...array]

I would then do an ngFor using the newArray variable and not the original variable, this means your table will react to the modified array but you will always have the originalArray as a reference. 然后,我将使用newArray变量而不是原始变量来执行ngFor,这意味着您的表将对修改后的数组做出反应,但是您始终将originalArray作为参考。

Below is an updated snippet of the transform method. 下面是transform方法的更新片段。

transform(items: any[], field: string, value: string[]): any[] {

    if (!items) {
      return [];
    }

    if (!field || !value || value.length <= 0) {
      return this.newArray = [...this.usersList.slice()];
    }

    this.newArray = items.filter(singleItem => {
      return (singleItem != null && singleItem[field] != null && singleItem[field] != undefined && value.indexOf(singleItem[field]) >= 0);
    });

    return this.newArray
  }
}

try changing like this, this should work. 尝试像这样进行更改,这应该可行。

...
if (this.groupValue.length == 0) {
  this.transform(this.usersList, '', this.groupValue)
}
...

...
if (!field || !value || value.length <= 0) {
    this.newArray = this.usersList.slice()
    return this.newArray
}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM