【问题标题】:How to use this pipe with another one in this code for filtering the data in Angular?如何在此代码中将此管道与另一个管道一起使用以过滤 Angular 中的数据?
【发布时间】:2018-11-24 00:21:25
【问题描述】:

我已添加到表单输入中,该输入将按年龄范围过滤用户。为此创建了管道。现在我正在尝试在工作示例中实现这一点,在该示例中,使用其他管道基于其他形式过滤结果。如何在此工作示例中使用年龄范围管道并使这两个管道一起工作?代码如下:

年龄段管道

     transform(value: any, args?: any): any
 { if(!args) return value;
 return value.filter( item => item.age > args[0] && item => item.age < args[1])
     );
     }

args[0] 是最小值,args[1] 是最大值。

工作搜索示例管道

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'filter',
})
export class FilterPipe implements PipeTransform {
    transform(items: any[], value: string, prop: string): any[] {
        if (!items) return [];
        if (!value) return items;

        return items.filter(singleItem =>
            singleItem[prop].toLowerCase().includes(value.toLowerCase())
        );

    }
}

工作示例的 TypeScript

form: FormGroup;

  @Output() autoSearch: EventEmitter<string> = new EventEmitter<string>();
  @Output() groupFilters: EventEmitter<any>  = new EventEmitter<any>();
  searchText: string = '';
  constructor(private fb: FormBuilder,
              private userService: UserService) {}

  ngOnInit(): void {
    this.buildForm();
  }

  buildForm(): void {
    this.form = this.fb.group({
      name: new FormControl(''),
      prefix: new FormControl(''),
      position: new FormControl(''),
      gender: new FormControl(''),
      agefrom: new FormControl(''),
      ageto: new FormControl('')
    });
  }

  search(filters: any): void {
    Object.keys(filters).forEach(key => filters[key] === '' ? delete filters[key] : key);
    this.groupFilters.emit(filters);
  }

}

HTML

<form novalidate
      [formGroup]="form">

  <h3>Group Filter</h3>
  <div class="row">
    <div class="col-md-3">
  <input type="text" 
  formControlName="name"
         class="form-control"
         placeholder="name"
         #searchName
        />
    </div>
        <div class="col-md-3">
  <input type="text" 
  formControlName="agefrom"
         class="form-control"
         placeholder="age from"
         #searchName
        />
    </div>
        <div class="col-md-3">
  <input type="text" 
  formControlName="nageto"
         class="form-control"
         placeholder="age to"
         #searchName
        />
    </div>
    <div class="col-md-3 col-sm-3">
      <select class="form-control"
              formControlName="prefix">
        <option value="">Prefix</option>
        <option value="MR">MR</option>
        <option value="MS">MS</option>
      </select>
    </div>

    <div class="col-md-3 col-sm-3">
      <select class="form-control"
              formControlName="position">
        <option value="">Position</option>
        <option value="admin">admin</option>
        <option value="student">student</option>
      </select>
    </div>

    <div class="col-md-3 col-sm-3">
      <select class="form-control"
              formControlName="gender">
        <option value="">Gender</option>
        <option value="M">male</option>
        <option value="F">female</option>
      </select>
    </div>

    <div class="col-md-3 col-sm-3">
      <button class="btn btn-primary"
              (click)="search(form.value)">Search</button>
    </div>
  </div>

</form><br/>

您可以在此处查看完整的代码示例: https://stackblitz.com/edit/ng6-multiple-search-values-smz1cb-solved-jx6kgc?file=src%2Fapp%2Fuser%2Ffilter.pipe.ts

如何在代码中实现该年龄范围管道并使其成为此工作示例的一部分?

【问题讨论】:

  • 您可以通过使用两个单独的过滤器来实现这一点,否则您需要使用通用过滤器。让我知道你想要达到的目标。喜欢复杂性与简单性。
  • 我需要任何简单的方法来让这个年龄段的输入过滤器和其他过滤器一样好用。唯一的挑战是在上述工作示例中实现年龄范围输入过滤器并使其工作。

标签: angular angular6


【解决方案1】:

我解决了您的问题,但不使用管道,我在 (user-list.component.ts) 中创建的过滤器函数中创建了逻辑,您可以在此处查看:

https://stackblitz.com/edit/ng6-multiple-search-values-smz1cb-solved-age-range?file=src%2Fapp%2Fuser%2Fuser-list%2Fuser-list.component.html

希望我的回答对你有帮助

【讨论】:

  • 非常感谢!它完美地工作。就一个问题。当我为姓名或年龄输入删除 和 时,搜索仍然有效,但是当我为那些与选择过滤器一起使用的数据删除 和 时,它不再过滤了吗?那些有什么区别?我问它是因为在真正的应用程序中,并非所有来自后端的日期都应该显示在屏幕上,但搜索应该可以工作。这是一个非常有趣的案例,只发生在 select 中。
  • 我认为是因为当执行过滤器函数时,由于空字段而在检查用户 ['age'] 时会出现错误,但我现在在代码中添加了安全类型检查和在未发送年龄且过滤器再次起作用的情况下制作样本!
  • 不。我的意思是,当 和 不存在时,年龄和姓名可以完美地工作。问题在于性别、前缀和另一个选择而不是输入。
  • 我知道问题是因为在键过滤器中我没有对它们进行安全检查,而且如果它们不在 obj 中,所以我们将 false 附加到结果数组我用带有符合您情况的示例的新解决方案
  • 看来我解释的不够清楚。事实并非如此 :D 您刚刚在 HTML 部分中添加了条件。这正是我想要解释的内容:stackblitz.com/edit/… 我完全删除了名称和前缀。但是,当您按名称搜索时,它是正确的。当您从前缀中选择时,它不起作用。对于名称,它正确地从后端获取数据。但是对于选择的前缀,它不会。
猜你喜欢
相关资源
最近更新 更多
热门标签