简体   繁体   English

Pipe 同时允许多个过滤器值 - Angular

[英]Pipe to allow multiple filter values simultaneously - Angular

Buttons are generated using *ngFor bringing back as many different types of values available to filter by.按钮是使用*ngFor生成的,可以带回尽可能多的不同类型的值来过滤。 I'm filtering on a key of 'location', therefore if there are locations of 'west' and 'england' then two buttons of 'west' and 'england' are available to filter by.我正在过滤“位置”键,因此如果有“西”和“英格兰”的位置,则可以使用“西”和“英格兰”两个按钮进行过滤。

What I want to be able to do is select more than one filter.我想要做的是 select 不止一个过滤器。 If I click 'england' all the results for 'england' come back, then if I click 'west' then 'west' comes back as well as 'england' still being "active".如果我点击 'england','england' 的所有结果都会回来,然后如果我点击 'west' 则 'west' 会回来,而 'england' 仍然是“活跃的”。 Currently, I can only click one filter at a time.目前,我一次只能点击一个过滤器。

I think I need to assign an active class to my button then this will push an array of whats active to send to my pipe to do the filtering... ?我想我需要为我的按钮分配一个活动的 class 然后这将推动一个活动数组发送到我的 pipe 进行过滤...?

My filter Button我的过滤器按钮

  <div ngDefaultControl [(ngModel)]="filteredLocation" name="locationFilter" id="locationFilter">
    <button value="All" class="m-2 btn btn-primary" type="button">All</button>
    <button [class.active]="selectedIndex === i" (click)="filteredLocation = entry.location" class="m-2 btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique; let i = index">{{entry.location}}</button>
  </div>

single filter on results结果的单一过滤器

<div class="timeline">
  <my-timeline-entry *ngFor="let entry of timeLine | filter:filteredLocation:'location'" timeEntryHeader={{entry.year}} timeEntryContent={{entry.detail}} timeEntryPlace={{entry.place}} timeEntryLocation={{entry.location}}></my-timeline-entry>
</div>

I have created a stackBlitz of what I've got - try clicking on a filter you will see only one filter can be applied at one time.我已经创建了一个我所拥有的 stackBlitz - 尝试单击一个过滤器,您会看到一次只能应用一个过滤器。 https://stackblitz.com/edit/timeline-angular-7-nve3zw https://stackblitz.com/edit/timeline-angular-7-nve3zw

Any help here would be awesome.这里的任何帮助都会很棒。 Thanks谢谢

There can be multiple ways to do that one would be to attach some property to determine if location is active for example isLocationActive有多种方法可以做到这一点,一种是附加一些属性来确定位置是否处于活动状态,例如isLocationActive

Then toggle this flag as per your need and to apply active class also you don't need filter pipe in that case然后根据您的需要切换此标志并应用活动 class 在这种情况下您也不需要过滤器 pipe

So your html will look like所以你的 html 看起来像

<div class="form-group row">
  <div ngDefaultControl [(ngModel)]="filteredLocation" name="locationFilter" id="locationFilter">
        <button value="All" class="m-2 btn btn-primary" (click)="activeAllEntries()" type="button">All</button>
        <button [class.active]="entry.isLocationActive" (click)="entry.isLocationActive = !entry.isLocationActive" class="m-2 btn btn-primary" type="button" *ngFor="let entry of timeLine | filterUnique; let i = index">{{entry.location}}</button>
      </div>
</div>

<div class="timeline">
  <ng-container *ngFor="let entry of timeLine">
<my-timeline-entry *ngIf="entry.isLocationActive" timeEntryHeader={{entry.year}} timeEntryContent={{entry.detail}} timeEntryPlace={{entry.place}} timeEntryLocation={{entry.location}}></my-timeline-entry>
  </ng-container>

</div>

TS function TS function

   activeAllEntries() {
      this.timeLine.forEach(t=> t.isLocationActive=!t.isLocationActive)
   }

working Demo工作演示

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

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