Angular 4中的升序和降序排序 [英] Ascending and Descending Sort in Angular 4

查看:62
本文介绍了Angular 4中的升序和降序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么排序功能起作用:

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>

这不起作用的时候:

<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

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

管道

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

谢谢.

推荐答案

这里的问题是:

The issue here is :

对象的嵌套属性,orderBy必须提供排序依据 一级属性的基础

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

我正在考虑,inner应该看起来像这样

I am considering, inner should be look like this,

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

尝试使该对象像在第一级上采用所有可排序的属性 (或者您必须通过这种方式更改order)

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',
        ...
    }
}

然后尝试.

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


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


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){
        ....
      }
    };
}

这篇关于Angular 4中的升序和降序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆