【问题标题】:Angular 2. How to apply orderBy?Angular 2. 如何申请orderBy?
【发布时间】:2016-02-17 15:39:44
【问题描述】:

我有一段代码。

<table class="table table-bordered table-condensed" cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Score</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="#participant of participants; #i = index">
            <td>{{i+1}}</td>
            <td>{{participant.username}}</td>
            <td>{{participant.score}}</td>
        </tr>
    </tbody>
</table>

在 Angular 1 中,我有 orderBy 过滤器来按我的过滤器对行进行排序。但是我怎样才能以同样的方式在 Angular 2 中进行 orderBy 呢?谢谢。

【问题讨论】:

标签: javascript angular


【解决方案1】:

如果您使用lodash,您的管道可以是这样的:

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

@Pipe({
  name: 'orderBy'
})
export class OrderByPipe implements PipeTransform {
  transform = orderBy;
}

然后你就可以使用方法的所有威力了:

<li *ngFor="let product of products | orderBy: 'price': 'desc'">
  {{product.name}}
</li>

【讨论】:

    【解决方案2】:

    您需要为此实现自定义管道。这对应于创建一个由@Pipe 修饰的类。这是一个示例。它的 transform 方法实际上会处理列表,您可以根据需要对其进行排序:

    import { Pipe } from "angular2/core";
    
    @Pipe({
      name: "orderby"
    })
    export class OrderByPipe {
      transform(array: Array<string>, args: string): Array<string> {
        array.sort((a: any, b: any) => {
          if (a < b) {
            return -1;
          } else if (a > b) {
            return 1;
          } else {
            return 0;
          }
        });
        return array;
      }
    }
    

    然后您可以在下面的表达式中使用此管道。例如在 ngFor 中。不要忘记在您使用它的组件的pipes 属性中指定您的管道:

    @Component({
      (...)
      template: `
        <li *ngFor="list | orderby"> (...) </li>
      `,
      pipes: [ OrderByPipe ]
    })
    (...)
    

    【讨论】:

    • 更新:导入应该是import { Pipe } from "@angular/core";
    【解决方案3】:

    感谢您的回答。我在下面编写了可行的代码:

    @Pipe({name: 'orderBy'})
    
    export class orderBy implements PipeTransform {
        transform(obj: any, orderFields: string): any {
            orderFields.forEach(function(currentField) {
                var orderType = 'ASC';
    
                if (currentField[0] === '-') {
                    currentField = currentField.substring(1);
                    orderType = 'DESC';
                }
    
                obj.sort(function(a, b) {
                    if (orderType === 'ASC') {
                        if (a[currentField] < b[currentField]) return -1;
                        if (a[currentField] > b[currentField]) return 1;
                        return 0;
                    } else {
                        if (a[currentField] < b[currentField]) return 1;
                        if (a[currentField] > b[currentField]) return -1;
                        return 0;
                    }
                });
    
            });
            return obj;
        }
    }

    此代码考虑订单方向 DESC 或 ASC。用法:

    &lt;tr *ngFor="#participant of participants | orderBy: '-score'; #i = index"&gt;

    【讨论】:

    【解决方案4】:

    我知道的最新的库

    https://www.npmjs.com/package/ngx-pipes

    const numbers = [2, 1, 3];
     
    const obj = [
      {id: 4, name: 'Dave', amount: 2},
      {id: 2, name: 'Michael', amount: 2},
      {id: 3, name: 'Dan', amount: 1},
      {id: 1, name: 'John', amount: 1}
    ];
     
    const deepObj = [
      {id: 1, name: 'John', amount: 1337, deep: {prop: 4}},
      {id: 2, name: 'Michael', amount: 42, deep: {prop: 2}},
      {id: 3, name: 'Dan', amount: 1, deep: {prop: 1}},
      {id: 4, name: 'Dave', amount: 2, deep: {prop: 3}}
    ];
    
    <!-- Returns array ordered by value -->
    <p>{{ numbers | orderBy }}</p>  <!-- Output: [1, 2, 3] -->
    <p>{{ numbers | orderBy: '-' }}</p>  <!-- Output: [3, 2, 1] -->
     
    <!-- Returns array ordered by value of property -->
    <p>{{ deepObj | orderBy: 'amount' }}</p>  
    <!-- Output: [{id: 3, ...}, {id: 4, ...}, {id: 2, ...}, {id: 1, ...}] -->
    <p>{{ deepObj | orderBy: '-amount' }}</p>  
    <!-- Output: [{id: 1, ...}, {id: 2, ...}, {id: 4, ...}, {id: 3, ...}] -->
     
    <!-- Returns array ordered by value of deep property -->
    <p>{{ deepObj | orderBy: 'deep.prop' }}</p>  
    <!-- Output: [{id: 3, ...}, {id: 2, ...}, {id: 4, ...}, {id: 1, ...}] -->
    <p>{{ deepObj | orderBy: '-deep.prop' }}</p>  
    <!-- Output: [{id: 1, ...}, {id: 4, ...}, {id: 2, ...}, {id: 3, ...}] -->
     
    <!-- Returns array ordered by mutliple properties -->
    <p>{{ obj | orderBy: ['amount', 'id'] }}</p>  
    <!-- Output: [{id: 1, ...}, {id: 3, ...}, {id: 2, ...}, {id: 4, ...}] -->
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      相关资源
      最近更新 更多