首页 > 解决方案 > 按内部 *ngFor 中的属性对整个 *ngFor 进行排序

问题描述

使用 Angular,我创建了一个使用两个*ngFor表达式的表。我创建了一个允许基于对象属性进行排序/排序的管道。但是,我不知道如何或是否可以根据内部 *ngFor 中的属性对外部 *ngFor 进行排序。

这是我正在使用的 HTML:

<table *ngIf="option == 'Document Type'">
  <tr>
    <th class="modalheader">Sources</th>
    <th class="modalheader">Systems</th>
  </tr>
  <table *ngFor="let doctype of doctypes | sort: 'source.Title'" style="width: 200%; margin-top: 0px;">
  <tr *ngFor="let source of doctype.Source;">
    <td style="width:50%;">{{source.Title}}</td>
    <td>
      {{doctype.System.Title}}
    </td>
  </tr>
  </table>
</table>

我附上了一个带有实际数据的示例,我的管道用于排序。

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

@Pipe({
  name: 'sort'
})
export class SortPipe implements PipeTransform {

  transform(array: any[], field: string): any[] {
      array.sort((a: any, b: any) => {
        if (a[field] < b[field]) {
          return -1;
        } else if (a[field] > b[field]) {
          return 1;
        } else {
          return 0;
        }
      });
      return array;
    }

}
.modalheader {
  background-color: #00539B;
  color: white !important;
  padding-left: 5px;
  margin-bottom: 10px;
  /* REVIEW: ADDED FOR TABLE */
  font-size: 1.46em;
  font-weight: 300;
  width: 50%;
}

.docs {
  width: 99%;
  /* margin-top: 25px; */
  margin-top: 20px;
}

table {
  width: 100%;
  margin-top: 5px;
}
<table>
  <tr>
    <th class="modalheader">Sources</th>
    <th class="modalheader">Systems</th>
  </tr>
  <table style="width: 200%; margin-top: 0px;">
    <tr>
      <td style="width:25%;">Lafayette Savings Bank</td>
      <td>
        Nautilus
      </td>
    </tr>
  </table>
  <table style="width: 200%; margin-top: 0px;">
    <tr>
      <td style="width:25%;">United Bancorp</td>
      <td>
        Nautilus
      </td>
    </tr>
  </table>
  <table style="width: 200%; margin-top: 0px;">
    <tr>
      <td style="width:25%;">Old National</td>
      <td>
        FIS
      </td>
    </tr>
  </table>
</table>

这可能吗?如果可以,怎么做?

标签: angular

解决方案


推荐阅读