Angular 4中的升序和降序排序

Gra*_*ngh 5 angular-services angular-pipe angular

为什么sort函数工作正常:

<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>
Run Code Online (Sandbox Code Playgroud)

当这一个不工作时:

<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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

TS

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

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;
          }
        });
      }
    };
}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Viv*_*shi 2

这里的问题是:

对象的嵌套属性,orderBy 必须提供基于第一层属性的排序

我正在考虑,inner应该是这样的,

{
    transaction_date : '10/12/2014'
    user : {
        name : 'something',
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试使该对象像在第一级上获取所有可排序属性(或者您必须以这种方式更改顺序)

{
    transaction_date : '10/12/2014'
    user_name : 'something',
    user : {
        name : 'something',
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

并尝试一下。

<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>
Run Code Online (Sandbox Code Playgroud)

records.map(record => record['user_name'] = record.user.name);, 添加到您的transform函数中,如下所示:

这将按照我的建议创建对象:

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){
        ....
      }
    };
}
Run Code Online (Sandbox Code Playgroud)