You need to sign in to do that
Don't have an account?
Abhishek Sharma 527
modal popup to edit records
Hello There, I have LWC app which works to show records and edit it in same page, I need to add modal popup which should be visible whenever I try to edit the record
can anyone help in this.
//html code
<template>
<lightning-card title="Inline Edit With Lightning Datatable in LWC">
<template if:true={contacts.data}>
<lightning-datatable key-field="Id"
data={contacts.data}
columns={columns}
onsave={handleSave}
draft-values={saveDraftValues} >
</lightning-datatable>
</template>
</lightning-card>
</template>
//js code
import { LightningElement, track, wire } from 'lwc';
import getContacts from '@salesforce/apex/LWCExampleController.getContacts';
import { updateRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { refreshApex } from '@salesforce/apex';
// datatable columns
const columns = [
{
label: 'Name',
fieldName: 'Name',
type: 'text',
}, {
label: 'FirstName',
fieldName: 'FirstName',
type: 'text',
editable: true,
}, {
label: 'LastName',
fieldName: 'LastName',
type: 'text',
editable: true,
}, {
label: 'Phone',
fieldName: 'Phone',
type: 'phone',
editable: true
}
];
export default class popup extends LightningElement {
columns = columns;
@track contacts;
saveDraftValues = [];
@wire(getContacts)
cons(result) {
this.contacts = result;
if (result.error) {
this.contacts = undefined;
}
};
handleSave(event) {
this.saveDraftValues = event.detail.draftValues;
const recordInputs = this.saveDraftValues.slice().map(draft => {
const fields = Object.assign({}, draft);
return { fields };
});
// Updateing the records using the UiRecordAPi
const promises = recordInputs.map(recordInput => updateRecord(recordInput));
Promise.all(promises).then(res => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Records Updated Successfully!!',
variant: 'success'
})
);
this.saveDraftValues = [];
return this.refresh();
}).catch(error => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error',
message: 'An Error Occured!!',
variant: 'error'
})
);
}).finally(() => {
this.saveDraftValues = [];
});
}
// This function is used to refresh the table once data updated
async refresh() {
await refreshApex(this.contacts);
}
}
// class file
public with sharing class LWCExampleController {
@AuraEnabled(Cacheable = true)
public static List<Contact> getContacts() {
return [SELECT Id, Name, FirstName, LastName, Phone, Email
FROM Contact
WHERE Email != null
AND Phone != null
ORDER BY CreatedDate DESC NULLS LAST limit 10];
}
}
can anyone help in this.
//html code
<template>
<lightning-card title="Inline Edit With Lightning Datatable in LWC">
<template if:true={contacts.data}>
<lightning-datatable key-field="Id"
data={contacts.data}
columns={columns}
onsave={handleSave}
draft-values={saveDraftValues} >
</lightning-datatable>
</template>
</lightning-card>
</template>
//js code
import { LightningElement, track, wire } from 'lwc';
import getContacts from '@salesforce/apex/LWCExampleController.getContacts';
import { updateRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { refreshApex } from '@salesforce/apex';
// datatable columns
const columns = [
{
label: 'Name',
fieldName: 'Name',
type: 'text',
}, {
label: 'FirstName',
fieldName: 'FirstName',
type: 'text',
editable: true,
}, {
label: 'LastName',
fieldName: 'LastName',
type: 'text',
editable: true,
}, {
label: 'Phone',
fieldName: 'Phone',
type: 'phone',
editable: true
}
];
export default class popup extends LightningElement {
columns = columns;
@track contacts;
saveDraftValues = [];
@wire(getContacts)
cons(result) {
this.contacts = result;
if (result.error) {
this.contacts = undefined;
}
};
handleSave(event) {
this.saveDraftValues = event.detail.draftValues;
const recordInputs = this.saveDraftValues.slice().map(draft => {
const fields = Object.assign({}, draft);
return { fields };
});
// Updateing the records using the UiRecordAPi
const promises = recordInputs.map(recordInput => updateRecord(recordInput));
Promise.all(promises).then(res => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Records Updated Successfully!!',
variant: 'success'
})
);
this.saveDraftValues = [];
return this.refresh();
}).catch(error => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error',
message: 'An Error Occured!!',
variant: 'error'
})
);
}).finally(() => {
this.saveDraftValues = [];
});
}
// This function is used to refresh the table once data updated
async refresh() {
await refreshApex(this.contacts);
}
}
// class file
public with sharing class LWCExampleController {
@AuraEnabled(Cacheable = true)
public static List<Contact> getContacts() {
return [SELECT Id, Name, FirstName, LastName, Phone, Email
FROM Contact
WHERE Email != null
AND Phone != null
ORDER BY CreatedDate DESC NULLS LAST limit 10];
}
}
Refer the below links have solution for similar kind of ask.
https://salesforce.stackexchange.com/questions/341622/hook-up-a-modal-to-a-button-in-a-row-lwc-datatable
https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/
Thanks!!