What to do if sort datatable is not working with custom data types?

256    Asked by DipikaAgarwal in Salesforce , Asked on Mar 6, 2023

I tried creating a lightning-datatable with custom data types, but unfortunately when trying to sort a column, it does not work. I have set the columns to sortable: true but still it does not work.

Component structure

c-main-component
    |---> c-custom-datatable

One thing i noticed though, it highlights the first cell when i tried to sort it.

Answered by Diya tomar

To resolve the issue of sort datatable, I have a property declared as:


sortedBy = 'contractId';
sortedDirection = 'asc';
Im consuming it in the template as:
   
   
When I hit the sort I utilized the event handler:
onHandleSort(event) {
    const { fieldName: sortedBy, sortDirection } = event.detail;
    const cloneData = [...this.recordToShow];
    cloneData.sort(this.sortBy(sortedBy, sortDirection === 'asc' ? 1 : -1));
    this.recordToShow = cloneData;
    this.sortDirection = sortDirection;
    this.sortedBy = sortedBy;
}
My mistake was on the event handler
    this.sortDirection = sortDirection;
It should be:
    this.sortedDirection = sortDirection;


Your Answer

Interviews

Parent Categories