Angular Primeng:p-table自定义排序功能?

发布于 2025-02-10 04:34:20 字数 2206 浏览 1 评论 0原文

我正在使用Primeng的P-Table。因为默认排序功能在上升和降序中,因此

假设我们在这样的列上有一个数据 -

aaa
bbb
aaa
ccc
bbb
ccc

当我们以升序顺序进行排序时,它将显示为这样 -

aaa
aaa
bbb
bbb
ccc
ccc

以及当我们将按降序施加分类时如下所示 -

ccc
ccc
bbb
bbb
aaa
aaa

,但是在这里我想要一个不同的排序功能,例如我们按顺序排序,应该像这样 -

aaa
aaa
ccc
ccc
bbb
bbb

在这里,CCC应该在中间,而BBB应该终于

当我们以降序进行分类时,应该像这样 -

bbb
bbb
ccc
ccc
aaa
aaa

在这里,BBB首先应该出现,CCC应该在

此中间是我的html-

<p-table #orderTable [value]="orderData"
       [rowHover]="true"
       [rows]="5"
       [scrollable]="true"
       [filterDelay]="0"
       [globalFilterFields]="['status']"
       class="borderless"
       (sortFunction)="customSort($event)" 
       [customSort]="true">

<ng-template pTemplate="colgroup" let-columns>
  <colgroup>
    <col [style.min-width]="'100px'" [style.width]="'100px'">
  </colgroup>
</ng-template>

<ng-template pTemplate="header">
  <tr>
    <th pSortableColumn="status">Status<p-sortIcon field="status"></p-sortIcon></th>
  </tr>
</ng-template>
<ng-template pTemplate="body" let-order>
  <tr>
    <td>
      <div class="hide-overflow-text">
        <button class=pill>{{order.status}}</button>
      </div>
    </td>
  </tr>
</ng-template>

这是自定义分类功能 -

customSort(event: SortEvent) {
 event.data.sort((data1, data2) => {
  let value1 = data1[event.field];
  let value2 = data2[event.field];
  let result = null;

  if (value1 == null && value2 != null)
    result = -1;
  else if (value1 != null && value2 == null)
    result = 1;
  else if (value1 == null && value2 == null)
    result = 0;
  else if (typeof value1 === 'string' && typeof value2 === 'string') {
    result = value1.localeCompare(value2);
  }
  else
    result = (value1 < value2) ? -1 : (value1 > value2) ? 1 : 0;

  return (event.order * result);
});

}

任何想法应该什么逻辑我用于自定义排序而不是使用上升,降序的顺序?

I am using PrimeNG's p-table. in that the default sorting functionality is in ascending and descending order, like this

suppose we have a data on a column like this -

aaa
bbb
aaa
ccc
bbb
ccc

when we will apply sorting in ascending order it is showing like this -

aaa
aaa
bbb
bbb
ccc
ccc

and when we will apply sorting in descending order it is showing like this -

ccc
ccc
bbb
bbb
aaa
aaa

but here I want a different sorting functionality like if we are sorting in ascending order it should come like this -

aaa
aaa
ccc
ccc
bbb
bbb

here the ccc should come in middle and bbb should come at last

and when we will apply sorting in descending order it should come like this -

bbb
bbb
ccc
ccc
aaa
aaa

here the bbb should come at first and ccc should come in middle

here is my html -

<p-table #orderTable [value]="orderData"
       [rowHover]="true"
       [rows]="5"
       [scrollable]="true"
       [filterDelay]="0"
       [globalFilterFields]="['status']"
       class="borderless"
       (sortFunction)="customSort($event)" 
       [customSort]="true">

<ng-template pTemplate="colgroup" let-columns>
  <colgroup>
    <col [style.min-width]="'100px'" [style.width]="'100px'">
  </colgroup>
</ng-template>

<ng-template pTemplate="header">
  <tr>
    <th pSortableColumn="status">Status<p-sortIcon field="status"></p-sortIcon></th>
  </tr>
</ng-template>
<ng-template pTemplate="body" let-order>
  <tr>
    <td>
      <div class="hide-overflow-text">
        <button class=pill>{{order.status}}</button>
      </div>
    </td>
  </tr>
</ng-template>

here is the custom sorting function -

customSort(event: SortEvent) {
 event.data.sort((data1, data2) => {
  let value1 = data1[event.field];
  let value2 = data2[event.field];
  let result = null;

  if (value1 == null && value2 != null)
    result = -1;
  else if (value1 != null && value2 == null)
    result = 1;
  else if (value1 == null && value2 == null)
    result = 0;
  else if (typeof value1 === 'string' && typeof value2 === 'string') {
    result = value1.localeCompare(value2);
  }
  else
    result = (value1 < value2) ? -1 : (value1 > value2) ? 1 : 0;

  return (event.order * result);
});

}

Any idea what logic should I use for the custom sorting instead of using the ascending, descending order?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文