3

I have a lightning-datatable (lwc) that has a number of columns that are sortable. I'm tracking the sort direction of each column with a separate variable.

The sorting works great, including sortDirection -- except that the sort arrow on every column always points up -- even if the actual sort is DESC.

I can't figure out how to change that. What is the value that datatable uses to determine which arrow to display?

Here's my markup:

       <lightning-datatable
            key-field="id"
            data={responses}
            columns={columns}
            onsave={handleSave}
            draft-values={draftValues}
            onsort={handleSort}
            sorted-by={sortedBy}
            sorted-direction={sortedDirection}
            onrowselection={handleRowSelected}
            onrowaction={handleRowAction}
            show-row-number-column
            enable-infinite-loading
            onloadmore={handleLoadMore}>
        </lightning-datatable>

1 Answer 1

1

LWC (and Aura, Lightning, JavaScript, etc) is case sensitive. The allowed values are asc and desc. If you use the correct values, the arrows should appear in the correct direction.

4
  • 1
    Sorry, I shouldn't have put DESC in my question. I do, in fact, have asc and desc in my code -- lower case. But there are arrows over several columns, and only one sortDirection parameter in the markup. How does it track direction for each column? Commented Apr 1, 2019 at 23:34
  • @PatMcClellan__c oh. Did you set a sort field as well? You need both for it to behave correctly.
    – sfdcfox
    Commented Apr 1, 2019 at 23:50
  • Just added my markup above. Yes, included sorted-by and sorted-direction. Oh... I think that might be the issue though. I'm assigning this.sortedBy the fieldName__c and using that in my server call. But the columns don't have the __c. I'll update after I check that. Commented Apr 2, 2019 at 0:01
  • yep, that was it. I was multipurposing my {sortedBy} variable. I created a new var orderBy, leaving sortedBy tied to the columnName. That fixed it. Thanks. Commented Apr 2, 2019 at 0:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .