1
({
    method2 : function(component,event,helper){
        component.set("v.mycolumns",[{label: 'Subject ', fieldName: 'Subject', type: 'string'},
                                  {label: 'Priority', fieldName: 'Priority', type: 'list'},
                                  {label: 'Status', fieldName: 'Status', type: 'list'},
                                   {label: 'Createddate', fieldName: 'CreatedDate', type: 'Date'},
                                   {label: 'Owner Name', fieldName: 'Task.Owner', type: 'String'} ] );

        var action =component.get("c.fetchTasklist");

        action.setCallback(this, function(response){
             action.setParams({
        });
            var state = response.getState();
            if (state === "SUCCESS") { 
        component.set("v.Tasklist", response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})
2

Actually nested properties are not supported by lightning:datatable yet. But we can achieve it by adding the nested properties on the main object as a property like below:

({
  method2 : function(component,event,helper){
    component.set("v.mycolumns",[{label: 'Subject', fieldName: 'Subject', type: 'string'},
                              {label: 'Priority', fieldName: 'Priority', type: 'list'},
                              {label: 'Status', fieldName: 'Status', type: 'list'},
                               {label: 'Createddate', fieldName: 'CreatedDate', type: 'Date'},
                               {label: 'Owner Name', fieldName: 'TaskOwner', type: 'String'} ] );

    var action = component.get("c.fetchTasklist");

    /* It should be outside the callback and before the enqueue method call, and no need to do this if there is no parameter to pass. */
    action.setParams({}); 

    action.setCallback(this, function(response){
      var state = response.getState();
      if (state === "SUCCESS") {
        var taskList = response.getReturnValue();
        taskList.forEach(function(task){
          try{
            task['TaskOwner'] = task.Task.Owner; /* You can adjust the field name here. So here basically we are populating the nested property on the main object. */
          }catch(e){}
        });
        component.set("v.Tasklist", taskList);
      }
    });
    $A.enqueueAction(action);
  }
});

Hope this is what you are looking for.

  • Thank you! After adjusting the field name to Task['Owner']['Name'], it worked for me. – Raja Shekar Reddy Apr 2 at 6:20
  • @RajaShekarReddy, glad to know that it worked for you, Yes, that I have already mentioned in the comment that you have to adjust the fields here. – itzmukeshy7 Apr 2 at 6:30
  • Is there a way to display phone field from task object in lightning:datatable .After querying the field like who.phone in soql. – Raja Shekar Reddy Apr 2 at 7:38
  • @RajaShekarReddy yes in the same way. – itzmukeshy7 Apr 2 at 7:49
  • @ itzmukeshy7 Task['who']['phone'] this is how i tried but i didn't worked. – Raja Shekar Reddy Apr 2 at 8:57

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.