I need to display a wrapper in LWC Datatable. One of the attributes of the wrapper is sObject. I'll basically pass the object name and field name to display in the data table dynamically. Based on the fields name, I need to display them as columns.
Example: I can pass the fields of Accounts or Contact and the record will be assigned to the sObject variable. Now I need to display my data in datatable.
Wrapper:
public class SubmittedRecordsWrapper{
@AuraEnabled public String comments;
@AuraEnabled public sobject rec;
}
HTML:
@track columns = [{
label: 'Comment',
fieldName: 'comments',
type: 'text',
initialWidth: 290,
wrapText: true,
editable: true
},
{
label: 'Name',
fieldName: 'rec.Name',
type: 'text',
initialWidth: 290,
wrapText: true,
editable: false
},
{
label: 'Custom Field',
fieldName: 'rec.Custom__c',
type: 'text',
initialWidth: 290,
wrapText: true,
editable: false
}]
In the console log, I can see the data. I know I need to flatten the data. But I'm not sure how can I flatten it dynamically