[SalesForce] How to choose the Owner Name field in Lightning data table

({
    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);
    }
})

Best Answer

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.

Related Topic