【问题标题】:Filters without pipe Angular 6: overriding problem没有管道 Angular 6 的过滤器:最重要的问题
【发布时间】:2019-05-07 21:54:26
【问题描述】:

我有 2 类过滤器:groupname。我该如何修改这段代码,因为它们不是管道,它们是独立的,name 过滤器覆盖 group 过滤器,并且向后。

.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)

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

 changeUser(event) {
   const user = event.target.value;
   const index = this.userValue.indexOf(user);

   if (index > -1) {
     this.userValue.splice(index, 1);
   } else {
     this.userValue.push(user);
   }

   this.transform(this.usersList, 'name', this.userValue)

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

 transform(items: any[], field: string, value: string[]): any[] {
   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
 }

.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>

    <br>

    <ng-container *ngFor="let user of users">
        <label class="btn btn-filter" id="bttns">
        <input type="checkbox" name="customersUserFilter" autoComplete="off" [value]="user" (change)="changeUser($event)">
        {{ user }}
      </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>

Here is a working snippet

感谢您的宝贵时间!

【问题讨论】:

  • 首先,您甚至没有将功能用作pipe。其次,我没有看到它们压倒任何东西,它们都有效。
  • 这就是我问的原因,我不知道如何让它们像管道一样工作。其次,它们确实会覆盖。如果您检查tim 然后aa 将立即覆盖tim,我期待[] 结果,因为tim 不属于组a。不能回答的不要无礼,谢谢。
  • @Tenzolinho 你为什么说管道?你有什么想法?如果您想知道如何创建管道,请查看文档:angular.io/guide/pipes#custom-pipes 您必须创建一个由pipe 装饰的类,实现PipeTransform 并覆盖transform 方法。但我不明白你为什么需要管道。
  • 庆幸你没有像this kind of pipes are discouraged那样制作管道。在不阅读您的代码的情况下,我建议您保持这种方式(即在您的组件中),并且不要用它制作管道。
  • 我以前有管道,但我读到它们不好,所以我更改并移动了组件内的transform 函数。我的问题是我做错了什么,如何修改此代码以作为管道工作。这甚至可能吗?

标签: javascript html angular filter angular6


【解决方案1】:

请检查下面的app.component.ts 不需要额外的转换登录。只需在此文件中添加一个 get 属性读取器“filteredArray”,您可以使用以下方法而不使用管道。

import { Component } from '@angular/core';

@Component({
    selector: 'my-app',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    name: string;

    groups = ['a', 'b', 'c']
    users = ['john', 'mike', 'doe', 'tim']

    usersList = [
        { 'name': 'john', 'group': 'a' },
        { 'name': 'mike', 'group': 'a' },
        { 'name': 'doe', 'group': 'b' },
        { 'name': 'tim', 'group': 'c' }]
    groupValue: string[] = []
    userValue: string[] = []

    newArray = []

    ngOnInit() {
        this.newArray = this.usersList.slice()
    }

    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);
        }
    }

    changeUser(event) {
        const user = event.target.value;
        const index = this.userValue.indexOf(user);

        if (index > -1) {
            this.userValue.splice(index, 1);
        } else {
            this.userValue.push(user);
        }
    }

    get filteredArray() {
        let arrayData = [];
        if (this.groupValue.length === 0 && this.userValue.length === 0) {
            return this.usersList;
        }

        return this.usersList.filter((user) => {

            return (this.userValue.length > 0 ? this.userValue.indexOf(user.name) !== -1 : true)
                && (this.groupValue.length > 0 ? this.groupValue.indexOf(user.group) !== -1 : true);
        })
    }
}

对应的app.component.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>

<br>

<ng-container *ngFor="let user of users">
    <label class="btn btn-filter" id="bttns">
        <input type="checkbox" name="customersUserFilter" autoComplete="off" [value]="user" (change)="changeUser($event)">
        {{ user }}
    </label>&nbsp;
</ng-container>

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Group</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let user of filteredArray">
            <td>{{user.name}}</td>
            <td>{{user.group}}</td>
        </tr>
    </tbody>
</table>

为参考添加了一个内联屏幕截图。

【讨论】:

  • 好主意。谢谢你一百万次!
  • 最欢迎@Tenzolinho
【解决方案2】:

您正在使用groupusers 创建new array

所以我通过在两个事件上添加两个数组条件对您的代码进行了一些更改。

https://stackblitz.com/edit/angular-regyev?file=src/app/app.component.ts

this.transform(this.usersList, 'group', this.groupValue);
this.transform(this.newArray, 'name', this.userValue);

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

【讨论】:

  • @Florian,你检查了什么?
  • 勾选“a”并取消勾选。看 RANJIT 的回答,他的代码很不错。
  • 检查和取消检查是我的错,我的代码很糟糕。但是您按照我的要求做了,所以我赞成您的回答。 Ranjit 修复了这个检查/取消检查错误,所以我接受了他的回答。无论如何,谢谢!
  • 感谢 Tenzolinho,@Florian,无论如何更新了代码并对其进行了优化。
猜你喜欢
  • 2019-06-05
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 2019-07-02
  • 1970-01-01
  • 1970-01-01
  • 2019-05-02
相关资源
最近更新 更多