根据角度4中的下拉列表进行过滤 [英] Filtering based on dropdown list in angular 4

查看:120
本文介绍了根据角度4中的下拉列表进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含金额,日期,发票编号,状态等的发票清单表.我创建了一个包含状态的下拉列表.如果我在下拉列表中选择了状态,则需要过滤表并仅显示包含所选状态的行.

I have an invoice list table with amount, date, invoice number, status etc. I created a drop down list that contains status in it. If I selected the status in the drop down list I need the table to be filtered and show only the row which contains the selected status.

我只是在研究角度4.我尝试了以下代码,但无法正常工作.

I am just studying the angular 4. I tried the following code but it is not working.

在HTML页面中:

<select [(ngModel)]="selected" name="status" placeholder="select" (ngModelChange)="onOptionsSelected($event)">
        <option *ngFor="let sta of status" [ngValue]="sta">{{sta}}</option>
    </select>

在Component.ts页面中:

In Component.ts Page:

   selected:any;

    stat = [
        { value: "All", id: "123" },
        { value: "Unpaid and sent", id:"12" },

        { value: "Unpaid and sent",id:"23" },
        { value: "Unpaid and not sent" ,id:"45"},
        { value: "Unpaid with due date",id:"56" },
        { value: "Paid",id:"57" },
        { value: "Open",id:"78" },
        { value: "Overdue" ,id:"45"}];

    status = ['Select Status', 'All', 'Unpaid and sent', 'Unpaid with due date', 'Paid', 'Open', 'Overdue'];

constructor() {
        this.selected = this.stat;
        }

        onOptionsSelected(event) {
        let value = event.target.value;
         console.log(this.selected);

    }

有人可以帮助我吗?谢谢

Can anyone please help me? Thanks

推荐答案

上面的问题是您将数组设置为选中状态,只需在构造函数中删除该行,

The issue above is that you are setting array as selected, just remove that line inside the constructor,

constructor() {
   this.selected = this.stat; //Not necessary
}

而且,您可以对ngModel selected

And, You can use array.filter with the ngModel selected

onOptionsSelected() {
      console.log(this.selected); 
      this.filtered = this.stat.filter(t=>t.value ==this.selected);
}

STACKBLITZ DEMO

STACKBLITZ DEMO

这篇关于根据角度4中的下拉列表进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆