我正在使用Lodash按列对数据进行排序 . 当我单击表列 Headers 中的箭头时,该特定表列将按升序或降序排序 . 但是,我希望每列都按升序排序,而不管其他列的当前顺序如何 . 现在,我的函数只根据当前方向切换上升和下降,而不管列 . 我该如何修复此功能?
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)
}
}
table.component.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>