简体   繁体   English

在Angular中对列表组件进行排序

[英]Sort a list component in Angular

I'm trying to sort my list-component that I've created with angular material. 我正在尝试对用角形材质创建的列表组件进行排序。

<div style="width:30%;">
        <mat-nav-list matSort (matSortChange)="sortData($event)">
            <th mat-sort-header="stuff.name">Name</th>
            <mat-list-item *ngFor="let stuff of vehicleDetails">
                <button matLine (click)="updateInfo(stuff.id)"> {{ stuff.name }} </button>
                <button mat-icon-button id="btn" *ngIf='check(stuff.alarm)' matTooltip="{{stuff.alarm[tooltipIndex(stuff)]?.timestamp}} - {{stuff.alarm[tooltipIndex(stuff)]?.description}}">
                    <mat-icon>info</mat-icon>
                </button>
            </mat-list-item>
        </mat-nav-list>
    </div>

The data displayed in the list is a bunch of vehicle names. 列表中显示的数据是一堆车辆名称。 It comes from my vehicleDetails which is an array of type VehicleDetail, containing properties such as name, id, etc. Right now I'm trying to sort by name. 它来自我的vehicleDetails,它是VehicleDetail类型的数组,包含诸如名称,id等属性。现在,我正在尝试按名称排序。 Im using the following functions when trying to achieve this functionality: 我在尝试实现此功能时使用以下功能:

 export class VehiclelistComponent implements OnInit, AfterViewChecked
 {

vehicleDetails: VehicleDetail[] = [];
sortedVehicleDetails: VehicleDetail[];

sortData(sort: Sort) {
    const data = this.vehicleDetails.slice();
    if (!sort.active || sort.direction === '') {
      this.sortedVehicleDetails = data;
      return;
    }
    this.sortedVehicleDetails = data.sort((a, b) => {
      const isAsc = sort.direction === 'asc';
      switch (sort.active) {
        case 'name':
          return this.compare(a.name, b.name, isAsc);
        default:
          return 0;
      }
    });
  }

  compare(a, b, isAsc) {
    return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
  }

My ngOnInit and ngAfterViewChecked looks like this 我的ngOnInit和ngAfterViewChecked看起来像这样

ngOnInit() {
    // Init vehicles
    this.getVehicleDetails();

   }

  ngAfterViewChecked() {
    this.sortedVehicleDetails = this.vehicleDetails.slice();
  }

How can I solve my problem so I can sort by name or any other properties? 如何解决我的问题,以便可以按名称或其他任何属性排序?

You need to also get the property by which the object array needs to be sorted. 您还需要获取需要对对象数组进行排序的属性。

Something like this 像这样

  sort(value: any[], criteria: SortCriteria): any[] {        
    if (!value || !criteria)
      return value;

    let p: string = criteria.property;

    let sortFn:(a: any, b: any) => any = (a, b) => {
      let value: number = 0;
      if (a[p] === undefined) value = -1;
      else if (b[p] === undefined) value = 1;
      else value = a[p] > b[p] ? 1 : (b[p] > a[p] ? -1 : 0);
      return criteria.descending ? (value * -1) : value;
    };

    value.sort(sortFn);
    return value;
  }

Where SortCriteria is, 哪里是SortCriteria

interface SortCriteria {
  property: string;
  descending?: boolean;
}

You can also create a pipe so that you can use it directly in a template 您还可以创建管道,以便可以直接在模板中使用它

Stackblitz Stackblitz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM