Angular 6中的数组排序不正确

问题描述 投票:1回答:1

我使用* ngFor来迭代数据表中的对象数组和序列号。我使用let index of i,然后将其打印到表格,但我面临的问题是,在第9个序列号之后。我得到了10但是在1序列号后没有。像1,10,11,2,3。任何人都可以帮我这个吗?这是我的打字稿代码和HTML代码。 Here is the screenshot

// GETTING BONUS DATA
 getAllJobposts() {
 this.apiService.hrService.getAllJobPost().subscribe((res: any) => {
  this.jobPostsData = res.data;
  console.log(this.jobPostsData);
  // this.DataTablesFunctionCallAfterDataInit()
 });
}

 <tr class="MousePointer" (click)="EditRecord(post.id, target)" *ngFor="let 
   post of jobPostsData | reverse; let i = index">

              <td>{{ i+1 }}</td>
              <td>
                <span style="display: inline">
                  <a class=" btn btn-link btn-sm " title="Delete" [id]="post.id" (click)="$event.stopPropagation();modal.ShowModal(post.id)">
                    <i [ngStyle]="{color: 'blue'}" class="fa fa-trash-o ">
                    </i>
                  </a>
                  <a class="btn btn-link btn-sm btn-round" title="Edit" (click)="$event.stopPropagation();EditRecord(post.id,target)">
                    <i [ngStyle]="{color: 'blue'}" class="fa fa-pencil-square-o "></i>
                  </a>
                </span>
              </td>
              <td>{{ post.jobTitle }}</td>
              <td>{{ getDepartmentName(post.departmentId) }}</td>
              <td>{{ getDesignationName(post.designationId) }}</td>
              <td>{{ post.totalPosition }}</td>
              <td>{{ post.minExperience }}</td>
              <td>{{ getCityName(post.cityId) }}</td>
              <td>
                <span [class]="post.active == 1? 'status success':'status danger'" style="width:60px !important; text-align: center;">
                  {{post.active == 1? "Active":"In-Active" }}
                </span>
              </td>
            </tr>
angular html-table ngfor httpservice
1个回答
1
投票

似乎你需要通过以下方式重写你的reverse管道:

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'reverse' })
export class ReversePipe implements PipeTransform {
  transform(value) {
    return value.sort((a, b) => {
      return (b.id - a.id);
    }); 
  }
}

在sort函数中,您需要指定用于排序的对象:return (b.id - a.id)

或者如果id是字符串,则使用parseInt(b.id) - parseInt(a.id)(否则它将输出与截图中相同的结果,即按字母顺序排列)

此外,您可以通过parameters of pipe传递对象字段进行排序

© www.soinside.com 2019 - 2024. All rights reserved.