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
Mike Tol 1Mike Tol 1 

Autosave in datatable

Hi!
I have lead table with three columns: NAME, TITLE, PHONE. Please tell me How can I implement autosave - when entering the new value of the field this value should be saved in the database automatically (user should not click on some button)? TITLE, PHONE columns must be edited and data saved in database. Like on this image:

User-added image

My code:
Html:
<template>
    <lightning-card title="Leads">
        <lightning-layout multiple-rows="true" vertical-align="end">
           
            <lightning-layout-item size="12" padding="around-small">
                <lightning-datatable key-field="id" data={leads} columns={columns}></lightning-datatable>
            </lightning-layout-item>
           
        </lightning-layout>
    </lightning-card>
</template>

Js:
import { LightningElement , wire} from 'lwc';
import getLeads from '@salesforce/apex/Qqq.getLeads';
const columns = [
    { label: 'Name', fieldName: 'recordLink', type: 'url',
    typeAttributes: {label: {fieldName: "LeadName"}, tooltip: "Name", linkify: true} },
    { label: 'Title', fieldName: 'Title', type: 'text', editable: true },
    { label: 'Phone', fieldName: 'Phone', type: 'text', editable: true }    
];
export default class LeadTable extends LightningElement {
    columns = columns;
    leads;
   
    @wire(getLeads)
    wiredLeads(value) {
        const {error, data} = value;
        if (data) {
            let leadData = JSON.parse(JSON.stringify(data));  
            leadData.forEach(record => {
                                record.recordLink = "/" + record.Id;  
                                record.LeadName = record.Name;
                        });  
            this.leads = leadData;
        } else if (error) {
            this.error = error;
        }
    }
}

Apex:
public with sharing class Qqq {
    @AuraEnabled(cacheable=true)
    public static List<Lead> getLeads() {
        List<Lead> leads = [
            SELECT Id, Name, Title, Phone
            FROM Lead          
        ];
        return leads;                
    }   
}
Best Answer chosen by Mike Tol 1
PriyaPriya (Salesforce Developers) 

Hey Mike ,

As I understand from the title of your question, you are trying to auto save the draftValues on a lightning:datatable. You cannot auto save draftValues, you will need to write a server side logic to do so. Excerpt from the :- 
https://developer.salesforce.com/docs/component-library/bundle/lightning:datatable/documentation

The draftValues data structure is an array of objects. Each object describes the changed values by specifying the keyField to identify the row containing the changed values, the data column name, and the changed value for that column. The data structure is useful to make a server side call that persists the changes in the datatable.

Kindly mark it as best answer if this information helps. 

Regards,

Priya Ranjan

All Answers

PriyaPriya (Salesforce Developers) 

Hey Mike ,

As I understand from the title of your question, you are trying to auto save the draftValues on a lightning:datatable. You cannot auto save draftValues, you will need to write a server side logic to do so. Excerpt from the :- 
https://developer.salesforce.com/docs/component-library/bundle/lightning:datatable/documentation

The draftValues data structure is an array of objects. Each object describes the changed values by specifying the keyField to identify the row containing the changed values, the data column name, and the changed value for that column. The data structure is useful to make a server side call that persists the changes in the datatable.

Kindly mark it as best answer if this information helps. 

Regards,

Priya Ranjan

This was selected as the best answer
Mike Tol 1Mike Tol 1
Thank you Priya!