1

How do I retrieve all lwc lightning datatable rows and not just the ones that are editable.

2 of my columns are non-editable (Section__c & Type__c) (coming from custom metadata) and 3 of my columns are editable (Findings, Comments, Location)

const COLS = [
{ label: 'Section', fieldName: 'Section__c' },
{ label: 'Type', fieldName: 'Type__c' },
{ label: 'Findings', fieldName: 'Findings', editable: true },
{ label: 'Location', fieldName: 'Location', editable: true },
{ label: 'Comments', fieldName: 'Comments', editable: true }];

I am using event.detail.draftValues to get the values but they return just the editable fields and only the rows which are edited. However, I need to access the values of entire datatable. I tried this as well but row returns undefined and table data returns this
enter image description here

var table = this.template.querySelector('lightning-datatable');
var rows = table.data;
console.log('table data', table.data);
    rows.forEach(function(element){
    console.log('row ',  element.fieldName);
});

If Event.detail.draftValues returns the edited / changed values in the datatable and not data from Section__c and Type__c columns. In contrast, if I use data attribute then it returns only values from Section__c and Type__c columns.

Is there any way I can retrieve entire table data?

1 Answer 1

1

The rows from draftValues will include the key field, which you can use to get the rest of the row:

draftValues.forEach((row) => {
  const record = this.data.find((data) => data.Id === row.Id);
  // Here, row is the edited data, record contains everything else
});
8
  • Thanks for the response, I did this and it did not log anything event.detail.draftValues.forEach((row) => { const record = this.data.find((data) => data.Id === row.Id); console.log('record ',record); // Here, row is the edited data, record contains everything else });
    – user138847
    Feb 18, 2022 at 20:08
  • the code stops executing after const record assignment, @sfdcfox
    – user138847
    Feb 18, 2022 at 20:49
  • This is the exact error "TypeError: Cannot read properties of undefined (reading 'find') at eval (gnbsocialdevelopment--devnp--c.visualforce.com/flow/modules/c/…) at c (gnbsocialdevelopment--devnp--c.visualforce.com/auraFW/…) at Array.forEach (<anonymous>) at Proxy.c (gnbsocialdevelopment--devnp--c.visualforce.com/auraFW/…)
    – user138847
    Feb 18, 2022 at 21:05
  • comments are not for extended discussions, if you are facing a new issue, please open a new post, and/or consider updating your original question. Thanks
    – glls
    Feb 18, 2022 at 21:15
  • 1
    @user138847 Here's an example of merging the edits back with the original data: webcomponent demo. Basically, we combine the original values with the edits. If you want, you can then use that merged data to perform further calculations.
    – sfdcfox
    Feb 19, 2022 at 3:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .