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

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


    // 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?


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");
            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

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.

  • Please tell me how to fetch Sorted column label from fieldApi in lwc onHandleSort(event) { const { fieldName: sortedBy, sortDirection } = event.detail; const cloneData = [...this.eFileFields]; cloneData.sort(this.sortBy(sortedBy, sortDirection === 'asc' ? 1 : -1)); this.eFileFields = cloneData; this.sortDirection = sortDirection; this.isSorted = true; this.sortedBy = sortedBy; } – Harshit Verma bluCursor Apr 24 '20 at 11:58
  • @HarshitVermabluCursor const fieldLabel = this.columns.find(field=>field.fieldName===sortedBy).label; replacing this.columns with your columns variable. – sfdcfox Apr 24 '20 at 12:05

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.