5

我想要做的是对已经按字母顺序或自定义顺序分组的数据进行排序。我使用了sortField指定组头顺序的属性,但我也需要对组内的数据进行排序。

在此处输入图像描述

4

3 回答 3

2

我有同样的问题。我添加了自定义排序来解决这个问题

添加自定义排序

<p-column   field="color" header="color"  sortable="custom" (sortFunction)="sortByColor($event)"></p-column>

在打字稿中创建一个 customSort

sortByColor(e) {
    this.cars.sort(function (a, b) {
      let aGroup = a.name.toLowerCase();
      let bGroup = b.name.toLowerCase();
      if (aGroup > bGroup) return 1;
      if (aGroup < bGroup) return -1;
      let aSort = a.color.toLowerCase();
      let bSort = b.color.toLowerCase();
      if (aSort > bSort) return 1;
      if (aSort < bSort) return -1;
      return 0
    });
  }
于 2017-06-23T08:02:06.217 回答
2

对于那些对 TurboTable 有问题的人<p-table>,这里是解决方案:

<p-table sortField="name" sortMode="single" (onSort)="onSort($event)" (sortFunction)="customSort($event)" [customSort]="true">

OnSort()执行:

onSort() {
   // function to properly work with turbotable and rowgroup, see: https://www.primefaces.org/primeng/#/table/rowgroup 
   this.updateRowGroupMetaData();
}

customSort()执行:

customSort(e) {
  this.budgets.sort((a, b) => {
    const aGroup = a.name.toLowerCase();
    const bGroup = b.name.toLowerCase();
    if (aGroup > bGroup) { return 1; }
    if (aGroup < bGroup) { return -1; }
    const aSort = a.color;
    const bSort = b.color;
    if (aSort > bSort) { return 1; }
    if (aSort < bSort) { return -1; }
    return 0;
  });
}
于 2018-08-31T18:41:16.893 回答
1

我遇到了同样的问题。我使用了自定义排序。下面是代码:

在模板中:

<p-column   field="color" header="color"  sortable="custom" (sortFunction)="sortByColor($event,sortOrder)"></p-column>

下面是 typescript 中的 sortByColor 函数:

  sortOrder = 1;//1 means ascending order, 2 means descending order
  sortByField(e, order) {   
    this.cars.Data.sort(function (a, b) {
      let aGroup = a.name.toLowerCase();
      let bGroup = b.name.toLowerCase();
      if (aGroup > bGroup) return 1;
      else if (aGroup < bGroup) return -1;
      let aSort = a.color.toLowerCase();
      let bSort = b.color.toLowerCase();
      if (aGroup == bGroup) {
        //ascending order
        if (order == 1 && aSort < bSort) {
          return -1;
        }
        //ascending order     
        else if (order == 1 && aSort > bSort) {
          return 1;
        }
        //descending order   
        else if (order == 2 && aSort > bSort) {
          return -1;
        }
        //descending order 
        else if (order == 2 && aSort < bSort) {
          return 1;
        }

      }
      return 1
    });   
    this.sortOrder = this.sortOrder == 1 ? 2 : 1;
  }

这对我有用。希望这也适用于您的情况。

于 2018-06-08T11:31:48.953 回答