0

This is how I've declared the p-dropdown:

<p-dropdown name="taxOptions" [options]="taxOptions" [(ngModel)]="purchaseInvoiceDetail.tax"></p-dropdown>

The taxOptions property is populated like this:

this.taxOptions = [
      { label: 'Tax Exclusive', value: '0' },
      { label: 'Tax Inclusive', value: '1' }
];

This is the PurchaseInvoiceDetail interface:

export interface PurchaseInvoiceDetail {
  id: number,
  product: Product,
  quantity: number,
  unitPrice: number,
  discount: number,
  tax: string,
  purchaseInvoice: PurchaseInvoice
}

The table is populated using *ngFor on an array of PurchaseInvoiceDetail i.e. PurchaseInvoiceDetail[].

So a separate p-dropdown is present in each row of the table. The problem is that when I change the value of the dropdown and add another product the table refreshes and the selected option of the previous dropdown resets but not from the purchaseInvoiceDetail.tax. It only fails to fetch the value from purchaseInvoiceDetail.tax and show it as the selected value in the dropdown. Why is this happening?

1 Answers1

0
dropdowns: Array<SelectItem[]>;


<div *ngFor="let dropdown of dropdowns">
  <div *ngFor="let taxOption of dropdown">
     <p-dropdown name="taxOption" [options]="taxOption" 
     [(ngModel)]="purchaseInvoiceDetail.tax"></p-dropdown>
  </div>
</div>

SelectItem Interface

export interface SelectItem {
  label: string;
  value: number;
}

this is the pattern you can follow, just adapt it to your rows.

Patricio Vargas
  • 5,236
  • 11
  • 49
  • 100
  • I've the `taxOptions` declared as `taxOptions: SelectItem[];` It doesn't let me declared an array of type `taxOptions` like this: `dropdowns: taxOptions[];` –  Oct 19 '18 at 08:30
  • Cannot find name `taxOptions`. I think its because we cannot use a variable as a type. –  Oct 19 '18 at 16:50