How to sort data in a PrimeNG DataTable with Row Grouping

前端 未结 3 687
滥情空心
滥情空心 2021-02-10 21:30

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

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-10 22:10

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

    In Template:

    
    

    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.

提交回复
热议问题