简体   繁体   English

如何根据下拉列表中的选定选项显示元素

[英]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.

相关问题 如何显示特定于所选下拉选项的项目? - How can I show items specific to a selected dropdown option? 如何根据下拉列表中当前选择的选项显示特定数据? - how can i show specific data based on currently selected option in a dropdown list? 根据下拉列表中的选定选项显示数据库中的数据 - Show data from database depending on selected option in a dropdown list 如何根据在“下拉列表 A”上选择的选项禁用“下拉列表 B”的某些选项? - How do I disable certain options of 'Dropdown B' depending on an option selected on 'Dropdown A'? 如何使用 mongoDB 在下拉菜单中显示所选选项? - How to show the selected option in a dropdown menu, with the mongoDB? 如何根据下拉选择显示/隐藏div? - How can I show/hide divs depending on dropdown selection? 如何将选定的语义UI下拉选项存储在状态中? - How can I store the selected semantic UI dropdown option in state? 如何使用getElementsByClassName确定所选下拉选项的值? - How can I determine the value of the selected dropdown option using getElementsByClassName? 如何使用选择元素中的选项来根据所选选项运行功能 - How to use an option in a selection element to run a function depending on the selected option 如何根据选定的单选按钮显示消息? - How can I show messages depending on selected radio buttons?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM