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);
    }
3
  • 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
    Commented Dec 28, 2018 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. Commented Dec 28, 2018 at 0:43
  • @DavidReed : I updated the question with the possible answer. Commented Dec 28, 2018 at 1:15

1 Answer 1

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.

2
  • 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; } Commented Apr 24, 2020 at 11:58
  • @HarshitVermabluCursor const fieldLabel = this.columns.find(field=>field.fieldName===sortedBy).label; replacing this.columns with your columns variable.
    – sfdcfox
    Commented Apr 24, 2020 at 12:05

You must log in to answer this question.

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