I am using Lodash to sort data in a table by column. When I click an arrow in the table column header, that specific table column will then sort in ascending or descending order. However, I would like each column to sort in ascending order first regardless of the current order of other columns. Right now, my function only toggles ascending and descending dependent on the current direction regardless of column. How do I fix this function?
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>