0

As per the salesforce official doc, when we sort a column, we can get the field name by using

var fieldName = event.getParam('fieldName');

https://developer.salesforce.com/docs/component-library/bundle/lightning:datatable/documentation

({
    // Client-side controller called by the onsort event handler
    updateColumnSorting: function (cmp, event, helper) {
        var fieldName = event.getParam('fieldName');
        var sortDirection = event.getParam('sortDirection');
        // assign the latest attribute with the sorted column fieldName and sorted direction
        cmp.set("v.sortedBy", fieldName);
        cmp.set("v.sortedDirection", sortDirection);
        helper.sortData(cmp, fieldName, sortDirection);
    }
    })

Is there any way we can get the column header label?

UPDATED :

I somehow found the workaround, but still looking an for the easy way.

updateColumnSorting: function (cmp, event, helper) {
        var fieldName = event.getParam('fieldName');
        var labelName = '';
        var colList = cmp.get("v.myCols");
        colList.some(function(col){
            if(col.fieldName === fieldName) {
               labelName = col.label; 
            }
            return col.fieldName === fieldName;
        });
        var sortDirection = event.getParam('sortDirection');
        cmp.set("v.sortedBy", fieldName);
        cmp.set("v.sortedDirection", sortDirection);
        helper.sortData(cmp, fieldName,labelName, sortDirection);
    }
  • It's pretty common to have a v.columns attribute in your component bound to the columns attribute of the data table - that would pretty easily give you the column name if the pattern is used here. – David Reed Dec 28 '18 at 0:26
  • @DavidReed : Agreed, but {!v.columns}will have multiple columns. Column sorting is a single click event. So, I was wondering if event has any parameter to find the column label. – SFDC Geek Dec 28 '18 at 0:43
  • @DavidReed : I updated the question with the possible answer. – SFDC Geek Dec 28 '18 at 1:15
1

You use Array.prototype.find to find the column based on the incoming fieldName:

var columns = cmp.get("v.columns"), // whatever you called the columns attribute
    fieldName = event.getParam("fieldName"),
    sortByCol = columns.find(column => fieldName === column.fieldName),
    fieldLabel = sortByCol.label;

At this point, you have all the attributes from the column header (sortByCol) and the field label, as you've asked for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.