我有一张发票清单表,其中包含金额,日期,发票编号,状态等。我创建了一个包含状态的下拉列表。如果我在下拉列表中选择了状态,我需要过滤表格并仅显示包含所选状态的行。
我正在研究角度4.我尝试了以下代码,但它无法正常工作。
在HTML页面中:
<select [(ngModel)]="selected" name="status" placeholder="select" (ngModelChange)="onOptionsSelected($event)">
<option *ngFor="let sta of status" [ngValue]="sta">{{sta}}</option>
</select>
在Component.ts页面中:
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);
}
任何人都可以帮助我吗?感谢
答案 0 :(得分:0)
上面的问题是你将数组设置为选中,只需在构造函数中删除该行,
constructor() {
this.selected = this.stat; //Not necessary
}
并且,您可以将 array.filter
与ngModel selected
onOptionsSelected() {
console.log(this.selected);
this.filtered = this.stat.filter(t=>t.value ==this.selected);
}
<强> STACKBLITZ DEMO 强>