1

Here is the code for my sorting method and it works great:

sort : function(component, event, helper) {
    let sortedBy = event.getParam('fieldName');
    let sortedField = (component.get('v.sortedBy'));
    let sortDirection = component.get('v.sortDirection');;
        let contacts = component.get('v.contacts');
        let cloneContacts = contacts.slice(0);
            sortDirection = sortedField != sortedBy || !sortDirection;
            cloneContacts.sort(function(a,b){
            var t1 = a[sortedBy] == b[sortedBy],
            t2 = (!a[sortedBy] && b[sortedBy]) || (a[sortedBy] < b[sortedBy]);
            return t1? 0: (sortDirection?-1:1)*(t2?1:-1);
    });
        component.set('v.contacts', cloneContacts);
        component.set('v.sortDirection', sortDirection);
        component.set('v.sortedBy', sortedBy);
        component.set('v.sortedOn', true);
        helper.getDisplayDataPagination(component);

}

But there is one caveat: the pointer that displays the sorting direction (as I understand it) is always directed downwards, but should change (up / down). How to fix it? Because I'm a beginner, then I admit that I missed some obvious thing ...

enter image description here enter image description here

// cmp

...
    <aura:attribute name="sortedBy" type="String"/>
    <aura:attribute name="sortDirection" type="Boolean"/>
    <aura:attribute name="contacts" type="Contact[]"/>
...

//js

init : function (component, event, helper) {
    component.set('v.columns', [
       { label: 'Name', fieldName: 'URL', type: 'url', typeAttributes: { label: { fieldName: 'Name'}, target: '_self' },sortable: true  },
       { label: 'Email', fieldName: 'Email', type: 'email', sortable: true  },
       { label: 'Contact Level', fieldName: 'Contact_Level__c', sortable: true  },
       { label: 'Account', fieldName: 'AccountName', type: 'text', sortable: true  },
       { label: 'Owner', fieldName: 'OwnerName', type: 'text', sortable: true  },
       { label: 'Created By', fieldName: 'CreatedByName', type: 'text', sortable: true  },
       { label: 'Created Date', fieldName: 'CreatedDate', type: 'date', sortable: true  },
    ]);
    helper.getDataFromDB(component, helper);
},
1
  • Are you actually setting the sort direction on the datatable (are you binding it)? Commented Mar 25, 2021 at 18:18

1 Answer 1

4

The Datatable documentation actually has an example specific to sorting you'll find helpful.

Taking a quick look, I'd use the parameter in the click event to get the sort direction versus trying to calculate as you've attempted (unless you have some other need I'm not catching).

var sortDirection = event.getParam('sortDirection');
component.set("v.sortDirection",sortDirection);

In terms of your exact code, I believe the valid values are asc or desc for sortedDirection. You can check the specification for that information.

enter image description here

4
  • Yes, I had an example like in the documentation, but I don't like how it works with my table data. Then I implemented my current sort, which works great and works for me. Could you help me with editing my variation but using 'asc' and 'desc'? I am a newbie and I can't get it ... Commented Mar 25, 2021 at 12:53
  • It'd be helpful if you could share the .cmp code as well so I can see what types the attributes are Commented Mar 25, 2021 at 14:17
  • I edited my question and tried to add all the necessary information. Commented Mar 25, 2021 at 18:09
  • If you just use a different string attribute and use what the documentation says component.set("v.sortDirectionString",event.getParam('sortDirection');, you can still keep your sorting mechanism. The sortedDirectionin lightning:datatable` just controls the up or down arrow icon that displays. Commented Mar 25, 2021 at 21:01

You must log in to answer this question.

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