3

https://primefaces.org/primeng/showcase/#/table

First, i want the table to initialy do the order on my column "order" : it works.

Then, the user can change the sort manually by itself for another column (like 'name')...

When the user hit the "Edit" button, he can change the order of then items.
For a better user experience and a better logic, i want to force reset the column order to it's initial form : "order".

app.component.html

<p-table [value]="values" #table sortField="order" [sortOrder]="1" sortMode="single">
 <ng-template pTemplate="header">
  <tr>
   <th pSortableColumn="order" [pSortableColumnDisabled]="!readOnly">
    ORDER
    <p-sortIcon *ngIf="readOnly" field="order"></p-sortIcon>
   </th>
   ...
  </tr>
 </ng-template>
 <ng-template pTemplate="body">
  ...
 </ng-template>
</p-table>

app.component.ts

//@ViewChild('table') table: Table;
@ViewChild('table', { static: false }) table: Table;

onChangeMode() {
 this.table.sortMode = 'single';
 this.table.sortField = 'order';
 this.table.sortOrder = 1;
 this.table.reset();
}

But the "reset" function seems not doing it's job here... (nothing changes)
Did i forget something ?

Thank you for your help !

Arn.vrn7
  • 101
  • 1
  • 7

1 Answers1

0

I know this is an older question, but I recently had the same thing happen. I am using PrimeNG 15.2.0, and to follow your example, this is what I had to do:

@ViewChild('table', { static: false }) table: Table;

onChangeMode() {
 this.table.sortMode = 'single';
 this.table.reset();
 this.table.sortField = 'order';
 this.table.sortOrder = 1;
 this.table.sortSingle();
}

The reset() function resets certain pieces about the table's state it seems, including what field is sorted. But, just removing the state doesn't mean it will remove the actual sort. So after resetting, you have to tell the table what to sort by again.

Seth
  • 342
  • 1
  • 4
  • 14