I have built an LWC datatable and am trying to finalize the sorting direction for each column. The data in the table sorting correctly, but the sorting 'arrow' is not toggling when I click on a column. Below is my 'onHandleSort' function. I have confirmed with debug statements that this.sortDirection
is toggling from 'asc' to 'desc' when I click on. column, however the event.detail
is not updating. How can I make it so the event.detail
is also updating with the new value?
@track sortDirection;
defaultSortDirection = 'asc';
onHandleSort(event) {
const { fieldName: newSortedBy, sortDirection: newSortDirection } = event.detail;
console.log('Sorting event:', event.detail);
// If a different column is clicked, use the default sort direction
if (this.sortedBy !== newSortedBy) {
console.log('Different column clicked. Resetting to default sort direction:', this.defaultSortDirection);
this.sortDirection = this.defaultSortDirection;
this.sortedBy = newSortedBy;
}
if (this.sortedBy == newSortedBy) {
// If the same column is clicked, toggle the sort direction
console.log('Same column clicked. Toggling sort direction.');
this.sortDirection = (this.sortDirection === 'asc') ? 'desc' : 'asc';
}
console.log('Sorted By:', this.sortedBy);
console.log('Sort Direction:', this.sortDirection);
this.tasks = this.sortData(this.sortedBy, this.sortDirection);
}