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);
}
v.columns
attribute in your component bound to thecolumns
attribute of the data table - that would pretty easily give you the column name if the pattern is used here.{!v.columns}
will have multiple columns. Column sorting is a single click event. So, I was wondering ifevent
has any parameter to find the column label.