You need to sign in to do that
Don't have an account?
Mike 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:
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;
}
}
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:
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;
}
}
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
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