[英]How can I show an element depending on selected option in a dropdown
I have created 3 cards in Angular from an object array. 我从一个对象数组中创建了3个Angular卡片。 one of the properties of the objects is "price". 对象的一个属性是“价格”。 if I wanted to include a select element, with 2 options, one of them saying "lower than 2000" and the other saying "bigger that 2000", 如果我想要包含一个选择元素,有2个选项,其中一个说“低于2000”而另一个说“大于2000”,
What would be the best way to show only the elements with a higher price of ..for example, 2000? 只展示具有更高价格的元素的最佳方式是什么?例如,2000?
this is the HTML I've done: 这是我做过的HTML:
<div class="mat-card-wrapper">
<mat-card *ngFor="let person of dataArray; let i= index">
<mat-card-title-group>
<mat-card-subtitle>
<span class="boldy">BookingId:</span>
<span>
{{person.bookingId}}
</span>
<span class="boldy"> Cliente:</span>
<span>
{{person.locationId.tutenUser.firstName +" "+
person.locationId.tutenUser.lastName}}
</span>
<span class="boldy">Fecha de Creación</span>
<span>{{person.bookingTime}}</span>
<span class="boldy">Dirección</span>
<span>{{person.locationId.streetAddress}}</span>
<span class="boldy">Precio</span>
<span>{{person.bookingPrice}}</span>
</mat-card-subtitle>
</mat-card-title-group>
</mat-card>
</div>
This generates 3 cards, I can't come out with the right way of adding a simple <selec>
method that allow me to show only the elements with a price bigger than 2000 and the ones lower than 2000. 这会生成3张卡片,我不能用正确的方法添加一个简单的<selec>
方法,它允许我只显示价格大于2000的元素和低于2000的元素。
I know this is a very basic question but I'm stuck, can anyone help me with this? 我知道这是一个非常基本的问题,但是我被困住了,任何人都可以帮我这个吗?
Thanks. 谢谢。
You could add a pipe to your ngFor which would filter your data depending on what you selected (<2000 or >2000). 您可以在ngFor中添加一个管道,根据您选择的内容(<2000或> 2000)过滤您的数据。
<mat-card *ngFor="let person of dataArray | myfilterPipe:myCondition">
Your pipe would look like something like that: 你的烟斗看起来像这样:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'myfilterPipe',
})
export class MyFilterPipe implements PipeTransform {
transform(items: any[], filter: condition): any {
if (!items || !filter) {
return items;
}
if(condition === over2000) {
return items.filter(item => item.price > 2000);
}
if(condition === under2000) {
return items.filter(item => item.price < 2000);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.