1

I'm using primeng 4.3

I've coded this primeng datatable:

<p-dataTable [value]="quotes" [editable]="true">
  <p-column *ngFor="let col of cols" [field]="col.field 
    [header]="col.header" [editable]="col.editable">
    <ng-template let-col let-quote="rowData" pTemplate="editor">
      <p-spinner size="30" [(ngModel)]="quote[col.field]" name="quote"</p-spinner>
    </ng-template>
  </p-column>
</p-dataTable>

The problem appears when this.quotes changes. I mean, when a new array is populated on this.quotes, datatable doesn't populates changes.

In order to make sure a new array is populated I'm using:

this.quotes = Object.assign([], calculatedQuotes);

So, I mean, each time this.quotes is assigned, it has a new reference to a new array.

Any ideas?

I'm using primeng 4.3

Jordi
  • 20,868
  • 39
  • 149
  • 333

1 Answers1

1

As PRIMENG doc says:

Change Detection Table may need to be aware of changes in its value in some cases such as reapplying sort. For the sake of performance, this is only done when the reference of the value changes meaning a setter is used instead of ngDoCheck/IterableDiffers which can reduce performance. So when you manipulate the value such as removing or adding an item, instead of using array methods such as push, splice create a new array reference using a spread operator or similar.

So your ways is correct to see changes in your table:

this.quotes = Object.assign([], calculatedQuotes);

To add new item to your grid, you can use spread operator:

this.quotes = [...this.quotes , newObject];
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • I've not specified any `ChangeDetectionStrategy`. As far I've been able to figure out, I'm already changing reference (`Object.assign([],...`). If references changes it should be populated, shouldn't? I've also tried to use `ChangeDetectorRef`, but I'm getting an `providerData is undefined`... – Jordi Aug 27 '19 at 10:53
  • @Jordi please, see my updated answer. I've read more thoroughly docs of primeng table. Now it is clear why it happens. – StepUp Aug 27 '19 at 11:23