如何根据可观察值设置 primeng table 的默认排序顺序

How to set the default sort order of primeng table depending on observable value

我有 p-table 和 sortable 列。但是,最初我希望 table 按特定列排序,为此我使用 sortFiled。但是这个专栏直到 运行 时间才为人所知。 这是 table:

的片段
<p-table [value]="offers" sortField="totalPrice">
    <ng-template pTemplate="header">
       <tr>
          <th pSortableColumn="shopName">Name
              <p-sortIcon field="shopName"></p-sortIcon>
          </th>
          <th pSortableColumn="unitPrice">Unit price
              <p-sortIcon field="unitPrice"></p-sortIcon>
          </th>
          <th pSortableColumn="totalPrice">Total price
              <p-sortIcon field="totalPrice"></p-sortIcon>
          </th>
       </tr>
   </ng-template>
   <ng-template pTemplate="body"> .... </ng-template>
</p-table>

我需要根据 observable 的值指定列,如下所示,但似乎不起作用。

 <p-table [value]="offers" sortField="(isRelventPrice$ | async)? totalPrice : unitPrice">

如有任何帮助,我们将不胜感激。

sortField 是当前选择的过滤列,如果值发生变化,将会更新。

你可以将它设置为简单的字符串

<p-table [value]="offers" sortField="totalPrice">...<p-table>

在数据绑定的情况下你需要使用curly brackets []

<p-table [value]="offers" [sortField]="totalPriceValue">...<p-table>

对于你的情况 isRelventPrice$ 这将起作用

 <p-table 
    [value]="offers" [sortField]="(isRelventPrice$ | async)? 'totalPrice' : 'unitPrice'">
....
</p-table>

检查这个demo

angular binding syntax