Ascending and Descending Sort in Angular 4

时光总嘲笑我的痴心妄想 提交于 2019-11-30 15:56:50

问题


Why is it that sort function is working :

<th (click)="sort('transaction_date')">Transaction Date <i class="fa" [ngClass]="{'fa-sort': column != 'transaction_date', 'fa-sort-asc': (column == 'transaction_date' && isDesc), 'fa-sort-desc': (column == 'transaction_date' && !isDesc) }" aria-hidden="true"> </i></th>

WHILE THIS ONE ISN'T WORKING :

<th (click)="sort('user.name')">User <i class="fa" [ngClass]="{'fa-sort': column != 'user.name', 'fa-sort-asc': (column == 'user.name' && isDesc), 'fa-sort-desc': (column == 'user.name' && !isDesc) }" aria-hidden="true"> </i></th>

html

 <tr *ngFor="let inner of order.purchase_orders | orderBy: {property: column, direction: direction}">
        <td>{{ inner.transaction_date | date  }}</td>
        <td>{{ inner.user.name  }}</td>
 </tr>

ts

sort(property){
    this.isDesc = !this.isDesc; //change the direction    
    this.column = property;
    this.direction = this.isDesc ? 1 : -1;
    console.log(property);
  };

pipe

import {Pipe, PipeTransform} from '@angular/core';

@Pipe({
  name: 'orderBy'
})

export class OrderByPipe implements PipeTransform {

  transform(records: Array<any>, args?: any): any {
    if(records && records.length >0 ){
    return records.sort(function(a, b){
          if(a[args.property] < b[args.property]){
            return -1 * args.direction;
          }
          else if( a[args.property] > b[args.property]){
            return 1 * args.direction;
          }
          else{
            return 0;
          }
        });
      }
    };
}

Thanks in advance.


回答1:


The issue here is :

Nested property of object, orderBy must be providing the sorting on the basis of first level of properties

I am considering, inner should be look like this,

{
    transaction_date : '10/12/2014'
    user : {
        name : 'something',
        ...
    }
}

try to make this object like, take all sortable property on first level ( OR you must have to change the orderBy that way )

{
    transaction_date : '10/12/2014'
    user_name : 'something',
    user : {
        name : 'something',
        ...
    }
}

And try.

<th (click)="sort('user_name')">
    User <i class="fa" [ngClass]="{'fa-sort': column != 'user_name', 
                                    'fa-sort-asc': (column == 'user_name' && isDesc), 
                                    'fa-sort-desc': (column == 'user_name' && !isDesc) }" 
            aria-hidden="true"> 
        </i>
</th>

Add records.map(record => record['user_name'] = record.user.name); , to your transform function , like this :

This will make the object as I suggested :

export class OrderByPipe implements PipeTransform {

  transform(records: Array<any>, args?: any): any {
    if(records && records.length >0 ){

    records.map(record => record['user_name'] = record.user.name); // add here

    return records.sort(function(a, b){
        ....
      }
    };
}


来源:https://stackoverflow.com/questions/47049077/ascending-and-descending-sort-in-angular-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!