Angular 角度表中的排序方向和顺序(升序、降序)

Angular 角度表中的排序方向和顺序(升序、降序),angular,sorting,lodash,Angular,Sorting,Lodash,我使用Lodash按列对表中的数据进行排序。单击表列标题中的箭头时,该特定表列将按升序或降序排序。但是,我希望每个列先按升序排序,而不考虑其他列的当前顺序。现在,我的函数只根据当前方向切换升序和降序,而不考虑列。如何修复此函数 export class TableComponent { @Input() data direction: string = '' sort(val) { if (this.direction === '' || this.direction ==

我使用Lodash按列对表中的数据进行排序。单击表列标题中的箭头时,该特定表列将按升序或降序排序。但是,我希望每个列先按升序排序,而不考虑其他列的当前顺序。现在,我的函数只根据当前方向切换升序和降序,而不考虑列。如何修复此函数

export class TableComponent {
  @Input() data
  direction: string = ''

  sort(val) {
    if (this.direction === '' || this.direction === 'asc') {
      this.data = _.orderBy(this.data, [val], ['desc'])
      this.direction = 'desc'
    } else {
      this.data = _.orderBy(this.data, [val], ['asc'])
      this.direction = 'asc'
    }
    console.log(this.direction)
  }

}
表3.1.ts

<table>
    <thead>
        <tr>
            <th *ngFor="let col of cols">
                {{col.header}}
                <input type="text" 
                [(ngModel)]=fields[col.value] 
                (ngModelChange)="handleFilterChange()" 
                (click)="selectCol(col.value)"
                *ngIf=col.enableFilter/>
                <img 
                class="arrow" 
                (click)="sort(col.value)"/>
            </th>
        </tr>
    </thead>
    <tbody *ngFor="let row of data | filter: filters:selectedCol">
        <tr>
            <td *ngFor="let col of cols">{{row[col.value]}}</td>
        </tr>
    </tbody>
</table>

{{col.header}}
{{row[col.value]}
问题: 假设您必须呈现一个客户详细信息列表,其中包括客户名称、加入日期、地址、电话等,而加入日期作为字符串出现在JSON响应中

因为不可能将此“Join Date”(joinDate)列排序为日期,并假设有另一个隐藏列以毫秒为单位给出此“Join Date”(joinDateInMs)

解决方案:此列使用“cellRenderer”和“comparator”组合。 下面的代码片段是不言自明的

columnDefs: [{
 headerName: “Join Date”, field: “joinDateInMs”,  
 cellRenderer: function (params) {
                return params.data.joinDate;
             }, 
comparator: self.createDateComparator
},
]
......
//
createDateComparator(date1, date2) {
        return date2 - date1;
    }

请提供一些纪念品。我也不清楚什么是“不管其他列的当前顺序如何,首先按升序排序的每个列”。你是说每个特定行的列中的信息不相关,你只想排序特定列的值,而不管其他列的值吗?实际上,如果您希望对每列进行独立排序,那么只需对每列使用单独的数组即可。我的意思是,我只希望对特定列的值进行排序,而不管其他列当前是升序还是降序。现在它只是在两者之间切换。另外,我从一个端点获取数据,所以我不认为将每个列分离到它们自己的数组中是最好的方法。