function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

LWC to render child records in datatable

Hello,
I am trying to do the following.  Have a button on teh parenet record that will render the child records in a datatable(editable)
What ever is selected I need to be able to create a new record from the rows slected.
I have a LWC and controller.  The LWC is wrapped in a aura compone tbut when selected it just has the header columns.
Should I try to  grab the child records in the .js?

Then how do I add a button to the LWS to create a new record from the rows slected?

Thank you for your help,
P
Best Answer chosen by Phuc Nguyen 18
David Zhu 🔥David Zhu 🔥
You may refer to the following steps and code
1. add onrowselection to let datatable
<lightning-datatable
onrowselection={getRowSelected}>
……

2. Add menu button to column definition

const columns = [ // Other column data here { type: 'action', typeAttributes: { rowActions: actions, menuAlignment: 'left' } } ];

getRowActions(row, doneCallback) {
const actions = [];
actions.push({ 'label': 'Creat a record', 'name': 'create' });
} // simulate a trip to the server
setTimeout(() => { doneCallback(actions); }), 200);
}

3. In J's file 
action;
selectedrow;

getRowSelected(event){
   this.action = event.detail.action.name;
 this.selectedrow = event.detail.row;

// Your code creating a new record
}
 

All Answers

sofiya merisofiya meri
In this post, I have been given all the information.
mycfavisit (https://customersatisfactionsurvey3.home.blog/2020/03/05/mycfavisit/)
David Zhu 🔥David Zhu 🔥
You may refer to the following steps and code
1. add onrowselection to let datatable
<lightning-datatable
onrowselection={getRowSelected}>
……

2. Add menu button to column definition

const columns = [ // Other column data here { type: 'action', typeAttributes: { rowActions: actions, menuAlignment: 'left' } } ];

getRowActions(row, doneCallback) {
const actions = [];
actions.push({ 'label': 'Creat a record', 'name': 'create' });
} // simulate a trip to the server
setTimeout(() => { doneCallback(actions); }), 200);
}

3. In J's file 
action;
selectedrow;

getRowSelected(event){
   this.action = event.detail.action.name;
 this.selectedrow = event.detail.row;

// Your code creating a new record
}
 
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Hey David,
Thanks for the reply. So you are not using a controller? How are you selecting the child records and rendering them in the datatable?
<template>
    <lightning-datatable
         data={?}
        columns={columns} >
    </lightning-datatable>
</template>
Thanks,
P
 
David Zhu 🔥David Zhu 🔥
Please follow the instructions in sales force documents. 
it us very similar to data table in aura component.

https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/documentation
madonaa max 9madonaa max 9
Get the world-class accounting platform support at QuickBooks Support Phone Number #1-833-57O-O8OO. QuickBooks Pro is known to be a world-class accounting platform that has also provided its customers with a range of accounting services. If you’re managing your company, you can use this QuickBooks Support Phone Number #1-833-57O-O8OO Accounting service to make your job simpler. For this, you should be able to manage all sorts of company dealings quickly.
Read more: QuickBooks Support Phone Number
QuickBooks Support Phone Number
QuickBooks Support Phone Number
 
Phuc Nguyen 18Phuc Nguyen 18
Hey David,
I did see that documentation but the table never populated.  I just see the header columns. 
Thanks,
P
David Zhu 🔥David Zhu 🔥
1. html file
 <template>
<lightning-datatable data={contacts}  //contact is a variable defined in js file
columns={columns} >
</lightning-datatable> </template>

2. js file
   import getContactListfrom '@salesforce/apex/myController.getContactList';
   @track contacts;

    @wire (getContactList)       
    wiredContatcts(result) {
        if (result.data) {
            this.contacts = result.data;
            this.error = undefined;
        } else if (result.error) {
            this.error = result.error.body.message;
            this.data = undefined;
        }
    }

3. apex file
 @aurable......
  public method List<Contact> getContactList()
 {
......
 }
Phuc Nguyen 18Phuc Nguyen 18
Hey David,
Now I do not even see the header columns.  I am wondering if its becuase the controller is not returning any data.
 
@AuraEnabled
    public static List<Contacs> getPoLineItemList() 
    {
     @AuraEnabled(cacheable=true)
    public static List<Contact> getContactList() {
        return [SELECT Id, FirstName, LastName, Title, Phone, Email FROM Contact ];
    }
    }

So for this example.  I would have the LWC in an Action item on athe Account Page.  Would that return any data? 

Thank you,
P
David Zhu 🔥David Zhu 🔥
You need to make sure datatable columns have the right setting to see the header.
I don't know why you nest get contact list in another method. I assume it is typo
Phuc Nguyen 18Phuc Nguyen 18
David, 
What do you mean by 'datatable columns have the right setting'?
 
Phuc Nguyen 18Phuc Nguyen 18
It was a typo.  should have it looks l;ike this:
public with sharing class ContactController {

    @AuraEnabled(cacheable=true)
    public static List<Contact> getContactList() {
        return [SELECT Id, FirstName, LastName, Title, Phone, Email FROM Contact LIMIT 10];
    }
}

 
User 444User 444
@Phuc Nguyen 18--I am also facing same issue, data table is having headers and displaying correct number of rows but with no data in tha table, even console log is showing correct output. Did you get answer for your question. If yes, pls suggest