【问题标题】:Ascending and Descending Sort in Angular 4Angular 4中的升序和降序排序
【发布时间】:2017-11-01 05:59:53
【问题描述】:

为什么排序功能在起作用:

<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

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

提前致谢。

【问题讨论】:

    标签: angular angular-services angular-pipe


    【解决方案1】:

    这里的问题是:

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

    我在考虑,inner应该是这个样子,

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

    试着让这个对象像,把所有可排序的属性放在第一级 (或者您必须通过这种方式更改订单)

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

    【讨论】:

    • 我已经在上面添加了管道。是的你是对的。内部是这个 { transaction_date : '10/12/2014' user : { name : 'something', ... } }
    • 但我希望用户 : { name : 'something', ... } } 被读取。我已经读取了 transaction_date 但无法读取 user.name
    • 我必须同时输入 transaction_date 和 user.name 怎么办?
    • 它有效。我是否必须添加 records.map(record => record['user_name'] = record.user.name);因为我的应用中有很多这样的情况?
    猜你喜欢
    • 2018-06-03
    • 1970-01-01
    • 2015-03-09
    • 2012-11-08
    • 2023-03-07
    • 2016-03-03
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    相关资源
    最近更新 更多