I am testing the new <lightning:datatable>
, it is really good, and is a good replacement for most of my use cases which today I use a custom list component that I have developed.
I am getting data from my server through apex method, to populate it into the <lightning:datatable>
component. I can bind the returned list directly to the data
attribute and if the column names are same as the field names, all data is bound correctly.
The problem occurs when I get data from a parent record. For example - I want the Account name of my contact.
This example can show the problem:
Component Markup
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="mydata" type="Object"/>
<aura:attribute name="mycolumns" type="List"/>
<aura:attribute name="sortedBy" type="String"/>
<aura:attribute name="sortedDirection" type="String"/>
<lightning:datatable data="{! v.mydata }"
columns="{! v.mycolumns }"
keyField="Id"
onrowselection="{! c.getSelectedName }"
onsort="{!c.updateColumnSorting}"
sortedBy="{!v.sortedBy}"
sortedDirection="{!v.sortedDirection}"
hideCheckboxColumn="{!false}"
/>
Component Controller
({
doInit: function (component, event, helper) {
component.set('v.mycolumns', [
{label: 'Id', fieldName: 'Id', type: 'id', sortable:"true"},
{label: 'First Name', fieldName: 'FirstName', type: 'text', sortable:"true"},
{label: 'Last Name', fieldName: 'LastName', type: 'text', sortable:"true"},
{label: 'First Name Hebrew', fieldName: 'First_name_Hebrew__c', type: 'text', sortable:"true"},
{label: 'Last Name Hebrew', fieldName: 'Last_name_Hebrew__c', type: 'text', sortable:"true"},
{label: 'Account', fieldName: 'Account.Name', type: 'text', sortable:"true"},
{label: 'Email', fieldName: 'Email', type: 'email', sortable:"true"},
{label: 'Phone', fieldName: 'Phone', type: 'phone', sortable:"true"}
]);
var action = component.get("c.getRecordsFromDb");
action.setParams({
sObjectName: "Contact",
commaDelimitedFieldNames: "Id,FirstName,LastName,First_name_Hebrew__c,Last_name_Hebrew__c,Account.Name,Email,Phone",
filter: ""
});
action.setCallback(this, function(response){
var state = response.getState();
if (component.isValid() && state === "SUCCESS") {
var data = response.getReturnValue();
component.set("v.mydata", data);
}
});
$A.enqueueAction(action);
}
})
APEX Aura Enabled Method
@AuraEnabled
public static List<sObject> getRecordsFromDb(String sObjectName,
String commaDelimitedFieldNames,
String filter) {
system.debug('\n\n\n\ngetRecordsFromDb sObjectName: ' + sObjectName + ', commaDelimitedFieldNames: ' + commaDelimitedFieldNames + ', filter: ' + filter + '\n\n\n\n');
// create a dynamic soql to get the value of the field
String sql = 'SELECT ' + commaDelimitedFieldNames + ' FROM ' + sObjectName;
if (!String.isBlank(filter))
sql += ' WHERE ' + filter;
system.debug('\n\n\n\ngetRecordsFromDb sql: ' + sql + '\n\n\n\n');
List<sObject> records = Database.query(sql);
system.debug('\n\n\n\ngetRecordsFromDb: ' + records + '\n\n\n\n');
return records;
}
The problem is with the Account.Name field - the <lightning:datatable>
component does not display the column data.
I know that the data is returned ok, and I know that in the object that is returned, the Account
is contained like an object (probably this is the problem):
Is there a good way to show parent record data with this component?