You need to sign in to do that
Don't have an account?
Mike Tol 1
Get leads in input fields, not in columns
Hi!
I have leadtable as in the image.
I need get leads in input fields to save them automatically, not in columns. Please tell me how can I do that.
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-input class="slds-p-around_medium" label="Name" name="leadName"
onchange={nameChangedHandler}></lightning-input>
<lightning-input class="slds-p-around_medium" label="Title" name="leadTitle"
onchange={titleChangedHandler}></lightning-input>
<lightning-input class="slds-p-around_medium" label="Phone" type="phone" name="leadPhone"
onchange={phoneChangedHandler}></lightning-input>
<br/>
</lightning-layout-item>
</lightning-layout>
</lightning-card>
</template>
Js
import { LightningElement, wire } from 'lwc';
import getLeads from '@salesforce/apex/ProjectThree.getLeads';
import LEAD_OBJECT from '@salesforce/schema/Lead';
import NAME_FIELD from '@salesforce/schema/Lead.Title';
import TITLE_FIELD from '@salesforce/schema/Lead.Title';
import PHONE_FIELD from '@salesforce/schema/Lead.Phone';
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 LdsCreateRecord extends LightningElement {
columns = columns;
leads;
lead = LEAD_OBJECT;
myFields = [NAME_FIELD, TITLE_FIELD, PHONE_FIELD];
strName;
strTitle;
strPhone;
// Change Handlers.
nameChangedHandler(event){
this.strName = event.target.value;
}
titleChangedHandler(event){
this.strAccountNumber = event.target.value;
}
phoneChangedHandler(event){
this.strPhone = event.target.value;
}
@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;
}
}
}
public with sharing class ProjectThree {
@AuraEnabled(cacheable=true)
public static List<Lead> getLeads() {
List<Lead> leads = [
SELECT Id, Name, Title, Phone
FROM Lead
//WHERE Name Like 'Lisa%'
Limit 3
];
System.debug('leads = ' + leads);
return leads;
}
}
I have leadtable as in the image.
I need get leads in input fields to save them automatically, not in columns. Please tell me how can I do that.
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-input class="slds-p-around_medium" label="Name" name="leadName"
onchange={nameChangedHandler}></lightning-input>
<lightning-input class="slds-p-around_medium" label="Title" name="leadTitle"
onchange={titleChangedHandler}></lightning-input>
<lightning-input class="slds-p-around_medium" label="Phone" type="phone" name="leadPhone"
onchange={phoneChangedHandler}></lightning-input>
<br/>
</lightning-layout-item>
</lightning-layout>
</lightning-card>
</template>
Js
import { LightningElement, wire } from 'lwc';
import getLeads from '@salesforce/apex/ProjectThree.getLeads';
import LEAD_OBJECT from '@salesforce/schema/Lead';
import NAME_FIELD from '@salesforce/schema/Lead.Title';
import TITLE_FIELD from '@salesforce/schema/Lead.Title';
import PHONE_FIELD from '@salesforce/schema/Lead.Phone';
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 LdsCreateRecord extends LightningElement {
columns = columns;
leads;
lead = LEAD_OBJECT;
myFields = [NAME_FIELD, TITLE_FIELD, PHONE_FIELD];
strName;
strTitle;
strPhone;
// Change Handlers.
nameChangedHandler(event){
this.strName = event.target.value;
}
titleChangedHandler(event){
this.strAccountNumber = event.target.value;
}
phoneChangedHandler(event){
this.strPhone = event.target.value;
}
@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;
}
}
}
public with sharing class ProjectThree {
@AuraEnabled(cacheable=true)
public static List<Lead> getLeads() {
List<Lead> leads = [
SELECT Id, Name, Title, Phone
FROM Lead
//WHERE Name Like 'Lisa%'
Limit 3
];
System.debug('leads = ' + leads);
return leads;
}
}
I changed something in code. I saw your 3 days ago requirment too but due to some reason I couldn't give the reply but your requirment was you need to save Title and Phone fileds without click any button autosave. It is possible but for achieve this requirment you need to use custom table. you can't do it in lightning-datatable due to some limitation.
Here is entire code.
you don't need apex class for saving the records.
use this code and if you satisfied with my answer, let me know by marking it as best answer.
Thank you
All Answers
I changed something in code. I saw your 3 days ago requirment too but due to some reason I couldn't give the reply but your requirment was you need to save Title and Phone fileds without click any button autosave. It is possible but for achieve this requirment you need to use custom table. you can't do it in lightning-datatable due to some limitation.
Here is entire code.
you don't need apex class for saving the records.
use this code and if you satisfied with my answer, let me know by marking it as best answer.
Thank you