How to sort data in a PrimeNG DataTable with Row Grouping
Asked Answered
U

3

6

What I want to do is to sort the data already grouped in alphabetical order or custom order. I used the sortField attribute which specify the groupheader order but I need to order the data inside the group too.

enter image description here

Upsetting answered 20/6, 2017 at 10:25 Comment(1)
Can you tell me how you add rowGroup with footers please. I am struggling with that now. Also is it possible to have multiple field grouping ?Marcos
S
3

I have the same issues. I have added customized sort to solve this issues

To add a customized sort

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

In the typescript create a 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
    });
  }
Sofiasofie answered 23/6, 2017 at 8:2 Comment(0)
C
2

For those who have the problem with TurboTable <p-table>, here is the solution:

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

OnSort() implementation:

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

customSort() implementation:

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;
  });
}
Chromolithography answered 31/8, 2018 at 18:41 Comment(0)
Y
1

I was facing the same issue.I have used custom sorting.Below is the code:

In Template:

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

Below is the sortByColor function in typescript:

  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;
  }

This is working for me.Hope this will work in your case also.

Yeah answered 8/6, 2018 at 11:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.