0

I am displaying task records using lightning datatable in a lightning component. This table contains task records with 2 columns: DueDate (date) and Related To (Account). When the component is loaded Duedate comes as "Jan 23, 2014" and Related To comes as (001**********). I want to display DueDate as - 01/23/2014 and Related To as - ABC Client. Also sorting on DueDate doesnot work correctly.. Below is my code for quick reference:

Lightning Component: sortedDirection="{!v.sortedDirection}"/>

Apex Class: @AuraEnabled public static DataTableResponse getAccRecords(String strObjectName, String strFieldSetName){

    //Get the fields from FieldSet
    Schema.SObjectType SObjectTypeObj = Schema.getGlobalDescribe().get(strObjectName);
    Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();            
    Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(strFieldSetName);

    //To hold the table hearders 
    List<DataTableColumns> lstDataColumns = new List<DataTableColumns>();

    //Field to be queried - fetched from fieldset
    List<String> lstFieldsToQuery = new List<String>();

    //The final wrapper response to return to component
    DataTableResponse response = new DataTableResponse();

    for( Schema.FieldSetMember eachFieldSetMember : fieldSetObj.getFields() ){
        //This way we can set the type of a column

        //Create a wrapper instance and store label, fieldname and type.

             DataTableColumns datacolumns = new DataTableColumns( String.valueOf(eachFieldSetMember.getLabel()) , 
                                                            String.valueOf(eachFieldSetMember.getFieldPath()), 
                                                            String.valueOf(eachFieldSetMember.getType()).toLowerCase() );

        lstDataColumns.add(datacolumns);
       // }
       System.debug('===lstDataColumns==='+lstDataColumns);
        lstFieldsToQuery.add(String.valueOf(eachFieldSetMember.getFieldPath()));
    }

    //Form an SOQL to fetch the data - Set the wrapper instance and return as response
    if(! lstDataColumns.isEmpty()){            
        response.lstDataTableColumns = lstDataColumns;
        String query = 'SELECT Id, ' + String.join(lstFieldsToQuery, ',') + ', What.Name' +' FROM Task';
        System.debug(query);
        response.lstDataTableData = Database.query(query);
    }

    return response;
}

//Wrapper class to hold Columns with headers
public class DataTableColumns {
    @AuraEnabled
    public String label {get;set;}
    @AuraEnabled       
    public String fieldName {get;set;}
    @AuraEnabled
    public String type {get;set;}
    @AuraEnabled
    public Boolean sortable {get;set;}

    //Create and set three variables label, fieldname and type as required by the lightning:datatable
    public DataTableColumns(String label, String fieldName, String type){
        this.label = label;
        this.fieldName = fieldName;
        this.type = type;  
        this.sortable = true;
    }
}

//Wrapper calss to hold response - This response is used in the lightning:datatable component
public class DataTableResponse {
    @AuraEnabled
    public List<DataTableColumns> lstDataTableColumns {get;set;}
    @AuraEnabled
    public List<sObject> lstDataTableData {get;set;}                

    public DataTableResponse(){
        lstDataTableColumns = new List<DataTableColumns>();
        lstDataTableData = new List<sObject>();
    }
}

Thanks in advance..!!

  • you didn't supply any client component code - there should be all what you are after. also - why do you need all these wrapper classes? – Itai Shmida Jul 22 '18 at 15:56

Your Answer

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

Browse other questions tagged or ask your own question.