Filtering based on dropdown list in angular 4

2019-07-16 10:01发布

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.

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

In HTML Page:

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

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

标签: html angular
1条回答
Melony?
2楼-- · 2019-07-16 10:14

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
}

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

查看更多
登录 后发表回答