Reputation: 73
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
Upvotes: 3
Views: 4525
Reputation: 222720
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);
}
Upvotes: 1