如何使用自定义管道在Angular中按属性值对对象数组进行排序?

阿列克谢伊

我需要在Angular中编写一个自定义管道,该管道接受对象数组和以作为参数order调用的变量,然后按属性值对对象数组进行排序。ascendingdescending

数据看起来像这样:

[
    {
        "location_type": "KAUPPA",
        "postalcode": "100",
        "availability": "LIIKETILAN AUKIOLO",
        "location": "SUOMALAINEN KIRJAKAUPPA / SISÄKÄYTÄVÄ",
        "municipality": "TURKU",
        "target_address": "ALEKSANTERINKATU 23",
        "availability_details": "",
        "coordinates_lon": "24.941095",
        "coordinates_lat": "60.168718"
    },
    {
        "location_type": "PANKIN KONTTORI",
        "postalcode": "100",
        "availability": "ITSEPALVELUALUEEN AUKIOLO",
        "location": "NORDEA SENAATINTORI",
        "municipality": "VANTAA",
        "target_address": "ALEKSANTERINKATU 30",
        "availability_details": "ma-su klo 06-22",
        "coordinates_lon": "24.950720",
        "coordinates_lat": "60.168930"
    },
    {
        "location_type": "TAVARATALO",
        "postalcode": "100",
        "availability": "LIIKETILAN AUKIOLO",
        "location": "STOCKMANN / 8. KERROS",
        "municipality": "HELSINKI",
        "target_address": "ALEKSANTERINKATU 52",
        "availability_details": "",
        "coordinates_lon": "24.941870",
        "coordinates_lat": "60.168430"
    }
]

数组中的对象需要按municipality的值进行排序

悉达(SiddAjmera)

正如在评论中以及在Pardeep的上述答案中多次提到的那样,Pipe对数据进行排序并不是一个好主意。

如果要对字段进行排序,只需在模板上实现它,然后仅对事件触发排序功能。这将大大节省您的性能。

在这里,尝试一下:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  lastSortedByField;
  ascendingOrder = true;
  data = [
    {
      "location_type": "KAUPPA",
      "postalcode": "100",
      "availability": "LIIKETILAN AUKIOLO",
      "location": "SUOMALAINEN KIRJAKAUPPA / SISÄKÄYTÄVÄ",
      "municipality": "TURKU",
      "target_address": "ALEKSANTERINKATU 23",
      "availability_details": "",
      "coordinates_lon": "24.941095",
      "coordinates_lat": "60.168718"
    },
    {
      "location_type": "PANKIN KONTTORI",
      "postalcode": "100",
      "availability": "ITSEPALVELUALUEEN AUKIOLO",
      "location": "NORDEA SENAATINTORI",
      "municipality": "VANTAA",
      "target_address": "ALEKSANTERINKATU 30",
      "availability_details": "ma-su klo 06-22",
      "coordinates_lon": "24.950720",
      "coordinates_lat": "60.168930"
    },
    {
      "location_type": "TAVARATALO",
      "postalcode": "100",
      "availability": "LIIKETILAN AUKIOLO",
      "location": "STOCKMANN / 8. KERROS",
      "municipality": "HELSINKI",
      "target_address": "ALEKSANTERINKATU 52",
      "availability_details": "",
      "coordinates_lon": "24.941870",
      "coordinates_lat": "60.168430"
    }
  ];

  sortByField(field) {
    if(this.lastSortedByField === field) {
      this.ascendingOrder = !this.ascendingOrder;
    }
    else {
      this.lastSortedByField = field;
      this.ascendingOrder = true;
    }

    if(this.ascendingOrder) {
      this.data = this.data.sort((a, b) => {
        if (a[field] < b[field])
          return -1;
        if (a[field] > b[field])
          return 1;
        return 0;
      });
    } else {
      this.data = this.data.sort((a, b) => {
        if (a[field] < b[field])
          return 1;
        if (a[field] > b[field])
          return -1;
        return 0;
      });
    }

  }

}

并在模板中:

<table border="1">
  <thead>
    <tr>
      <td (click)="sortByField('location_type')">location_type</td>
      <td (click)="sortByField('postalcode')">postalcode</td>
      <td (click)="sortByField('availability')">availability</td>
      <td (click)="sortByField('location')">location</td>
      <td (click)="sortByField('municipality')">municipality</td>
      <td (click)="sortByField('target_address')">target_address</td>
      <td (click)="sortByField('availability_details')">availability_details</td>
      <td (click)="sortByField('coordinates_lon')">coordinates_lon</td>
      <td (click)="sortByField('coordinates_lat')">coordinates_lat</td>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let datum of data">
      <td>{{ datum.location_type }}</td>
      <td>{{ datum.postalcode }}</td>
      <td>{{ datum.availability }}</td>
      <td>{{ datum.location }}</td>
      <td>{{ datum.municipality }}</td>
      <td>{{ datum.target_address }}</td>
      <td>{{ datum.availability_details }}</td>
      <td>{{ datum.coordinates_lon }}</td>
      <td>{{ datum.coordinates_lat }}</td>
    </tr>
  </tbody>
</table>

这是供您参考工作样本StackBlitz

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用自定义排序顺序在angular js中对对象进行排序?

按自定义顺序对对象数组进行排序

如何使用Java 8中的自定义Comparator接口对对象数组进行排序?

Swift如何按属性值对自定义对象数组进行排序

Javascript自定义按年份使用“前一年”对对象数组进行排序

如何按属性值对对象数组进行排序

如何按预定义值对对象数组进行排序?

如何在Swift 3中按自定义状态值对自定义对象数组进行排序?

按属性对对象列表进行排序 C#(自定义排序顺序)

uasort性能差,按自定义顺序对对象数组进行排序

JavaScript按属性的数组值对对象数组进行排序

使用三个级别的自定义排序标准对对象数组进行排序

按属性值对对象数组进行排序

按值对对象数组中的每个对象进行排序

在javascript中按子数组属性值对对象数组进行排序

按JavaScript中定义值数组列表对对象数组进行排序

按属性对对象数组进行排序

如何在自定义排序功能中对对象的特定属性实施排序?

如何按属性 PHP 对对象数组进行排序

在MATLAB中按属性对对象数组进行排序?

使用 lodash 按嵌套属性对对象数组进行排序

如何在Java数组中按字母顺序对自定义对象进行排序?

按值对对象属性进行排序

在Java中按属性值对对象ArrayList进行排序

按值对对象数组进行排序

使用Typescript按值对对象数组进行排序

使用Lodash按值对对象数组进行排序

如何使用Angular 2管道对对象列表进行排序

按字母顺序对对象的 JSON 数组以及 dart 中属性的布尔值进行排序