Welcome back, In this post we are going to create another lightning web component (LWC), Where we can search contact records and will display result using lightning-datatable lwc component. Lightning datatable tag is same as lightning:datatable tag in aura. In this lightning Datatable example we will also talk about lightning datatable inline edit.
lighning-datatable syntax:-
<lightning-datatable key-field="Id"
data={contacts}
columns={columns}
hide-checkbox-column="true"
show-row-number-column="true"
>
</lightning-datatable>
1) Create Apex Class
This apex class we will call from Lightning web components. If you want to learn more about how to call apex class from lightning web components then please check this post.
2) Create Lightning Web Components.
lwcLightningDataTableDemo.html
lwcLightningDataTableDemo.js
lwcLightningDataTableDemo.js-meta.xml
NOTE:- Now from Winter 20 release we can Add Lightning Web Components as Custom Tabs for that we need to add lightning__Tab target to the component’s configuration file.
Now Your page will look like this
Let's see how we can do inline Edit
Let's see how our code will look like:
lwcLightningDataTableDemo.html
lwcLightningDataTableDemo.js
Check our YouTube Channel for more recording in Lightning Web Components.
Reference :-
1) https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/example
2) https://developer.salesforce.com/docs/component-library/documentation/lwc/data_table_inline_edit
Please share your feedback.
lighning-datatable syntax:-
<lightning-datatable key-field="Id"
data={contacts}
columns={columns}
hide-checkbox-column="true"
show-row-number-column="true"
>
</lightning-datatable>
- If you want to hide the checkbox from table then add "hide-checkbox-column"
- If you want to show row number then please add "show-row-number-column".
1) Create Apex Class
public with sharing class LWCDataTableExample {
@AuraEnabled(Cacheable=true)
public static List <Contact> getContacts(String strLastName) {
String strLastNameLike = '%'+strLastName+'%';
List<Contact> contList = [SELECT Id,FirstName,LastName,Account.Name
FROM Contact
Where LastName like :strLastNameLike
LIMIT 10];
return contList;
}
}
@AuraEnabled(Cacheable=true)
public static List <Contact> getContacts(String strLastName) {
String strLastNameLike = '%'+strLastName+'%';
List<Contact> contList = [SELECT Id,FirstName,LastName,Account.Name
FROM Contact
Where LastName like :strLastNameLike
LIMIT 10];
return contList;
}
}
This apex class we will call from Lightning web components. If you want to learn more about how to call apex class from lightning web components then please check this post.
2) Create Lightning Web Components.
lwcLightningDataTableDemo.html
<template>
<lightning-card title = "Search Contacts" icon-name = "custom:custom63">
<div class = "slds-m-around_medium">
<lightning-input type = "search" onchange = {handleKeyChange} class = "slds-m-bottom_small" label = "Search" >
</lightning-input>
<template if:true = {contacts}>
<div style="height: 300px;">
<lightning-datatable key-field="Id"
data={contacts}
columns={columns}
hide-checkbox-column="true"
show-row-number-column="true">
</lightning-datatable>
</div>
</template>
<template if:true = {error}>
{error}>
</template>
</div>
</lightning-card>
</template>
<lightning-card title = "Search Contacts" icon-name = "custom:custom63">
<div class = "slds-m-around_medium">
<lightning-input type = "search" onchange = {handleKeyChange} class = "slds-m-bottom_small" label = "Search" >
</lightning-input>
<template if:true = {contacts}>
<div style="height: 300px;">
<lightning-datatable key-field="Id"
data={contacts}
columns={columns}
hide-checkbox-column="true"
show-row-number-column="true">
</lightning-datatable>
</div>
</template>
<template if:true = {error}>
{error}>
</template>
</div>
</lightning-card>
</template>
lwcLightningDataTableDemo.js
import { LightningElement,track } from 'lwc';
import getContacts from '@salesforce/apex/LWCDataTableExample.getContacts';
const columns = [
{ label: 'Id', fieldName: 'Id' },
{ label: 'First Name', fieldName: 'FirstName' },
{ label: 'Last Name', fieldName: 'LastName' }
];
export default class LwcLightningDataTableDemo extends LightningElement {
@track contacts;
@track error;
@track columns = columns;
handleKeyChange( event ) {
const strLastName = event.target.value;
if ( strLastName ) {
getContacts( { strLastName } )
.then(result => {
this.contacts = result;
console.log('I am here',this.contacts);
// console.log(JSON.stringify(result, null, '\t'));
})
.catch(error => {
this.error = error;
});
} else
this.contacts = undefined;
}
}
import getContacts from '@salesforce/apex/LWCDataTableExample.getContacts';
const columns = [
{ label: 'Id', fieldName: 'Id' },
{ label: 'First Name', fieldName: 'FirstName' },
{ label: 'Last Name', fieldName: 'LastName' }
];
export default class LwcLightningDataTableDemo extends LightningElement {
@track contacts;
@track error;
@track columns = columns;
handleKeyChange( event ) {
const strLastName = event.target.value;
if ( strLastName ) {
getContacts( { strLastName } )
.then(result => {
this.contacts = result;
console.log('I am here',this.contacts);
// console.log(JSON.stringify(result, null, '\t'));
})
.catch(error => {
this.error = error;
});
} else
this.contacts = undefined;
}
}
lwcLightningDataTableDemo.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="lwcLightningDataTableDemo">
<apiVersion>47.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
<target>lightning__Tab</target>
</targets>
</LightningComponentBundle>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="lwcLightningDataTableDemo">
<apiVersion>47.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
<target>lightning__Tab</target>
</targets>
</LightningComponentBundle>
NOTE:- Now from Winter 20 release we can Add Lightning Web Components as Custom Tabs for that we need to add lightning__Tab target to the component’s configuration file.
Now Your page will look like this
Lightning datatable inline edit
Let's see how we can do inline Edit
- For inline editing we can use the uiRecordAPI. For that we need to import the "lightning/uiRecordApi"
- We also need to add "onsave={handleSave}" and "draft-values={draftValues}"on lightning-datatable and on column we need to add enable "editable: true".
- Then we need to add handler Save method.
Let's see how our code will look like:
lwcLightningDataTableDemo.html
<template>
<lightning-card title = "Search Contacts" icon-name = "custom:custom63">
<div class = "slds-m-around_medium">
<lightning-input type = "search" onchange = {handleKeyChange} class = "slds-m-bottom_small" label = "Search" >
</lightning-input>
<template if:true = {contacts}>
<div style="height: 300px;">
<lightning-datatable key-field="Id"
data={contacts}
columns={columns}
hide-checkbox-column="true"
show-row-number-column="true"
onsave={handleSave}
draft-values={draftValues} >
</lightning-datatable>
</div>
</template>
<template if:true = {error}>
{error}>
</template>
</div>
</lightning-card>
</template>
<lightning-card title = "Search Contacts" icon-name = "custom:custom63">
<div class = "slds-m-around_medium">
<lightning-input type = "search" onchange = {handleKeyChange} class = "slds-m-bottom_small" label = "Search" >
</lightning-input>
<template if:true = {contacts}>
<div style="height: 300px;">
<lightning-datatable key-field="Id"
data={contacts}
columns={columns}
hide-checkbox-column="true"
show-row-number-column="true"
onsave={handleSave}
draft-values={draftValues} >
</lightning-datatable>
</div>
</template>
<template if:true = {error}>
{error}>
</template>
</div>
</lightning-card>
</template>
lwcLightningDataTableDemo.js
import { LightningElement,track } from 'lwc';
import getContacts from '@salesforce/apex/LWCDataTableExample.getContacts';
import { updateRecord } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
const columns = [
{ label: 'Id', fieldName: 'Id' },
{ label: 'First Name', fieldName: 'FirstName', editable: true },
{ label: 'Last Name', fieldName: 'LastName', editable: true }
];
export default class LwcLightningDataTableDemo extends LightningElement {
@track contacts;
@track error;
@track columns = columns;
@track draftValues = [];
handleKeyChange( event ) {
const strLastName = event.target.value;
if ( strLastName ) {
getContacts( { strLastName } )
.then(result => {
this.contacts = result;
// console.log('I am here',this.contacts);
// console.log(JSON.stringify(result, null, '\t'));
})
.catch(error => {
this.error = error;
});
} else
this.contacts = undefined;
}
handleSave(event) {
const recordInputs = event.detail.draftValues.slice().map(draft => {
const fields = Object.assign({}, draft);
return { fields };
});
const promises = recordInputs.map(recordInput => updateRecord(recordInput));
Promise.all(promises).then(contacts => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'All Contacts updated',
variant: 'success'
})
);
// Clear all draft values
this.draftValues = [];
// Display fresh data in the datatable
return refreshApex(this.contact);
}).catch(error => {
// Handle error
});
}
}
import getContacts from '@salesforce/apex/LWCDataTableExample.getContacts';
import { updateRecord } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
const columns = [
{ label: 'Id', fieldName: 'Id' },
{ label: 'First Name', fieldName: 'FirstName', editable: true },
{ label: 'Last Name', fieldName: 'LastName', editable: true }
];
export default class LwcLightningDataTableDemo extends LightningElement {
@track contacts;
@track error;
@track columns = columns;
@track draftValues = [];
handleKeyChange( event ) {
const strLastName = event.target.value;
if ( strLastName ) {
getContacts( { strLastName } )
.then(result => {
this.contacts = result;
// console.log('I am here',this.contacts);
// console.log(JSON.stringify(result, null, '\t'));
})
.catch(error => {
this.error = error;
});
} else
this.contacts = undefined;
}
handleSave(event) {
const recordInputs = event.detail.draftValues.slice().map(draft => {
const fields = Object.assign({}, draft);
return { fields };
});
const promises = recordInputs.map(recordInput => updateRecord(recordInput));
Promise.all(promises).then(contacts => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'All Contacts updated',
variant: 'success'
})
);
// Clear all draft values
this.draftValues = [];
// Display fresh data in the datatable
return refreshApex(this.contact);
}).catch(error => {
// Handle error
});
}
}
Please check below post on Lightning Web Components:-
- Lightning Web Components ( LWC ) in Salesforce with Non-Scratch Org
- Invoke Apex Controller from Lightning Web Component | Lightning Web Component inside Another LWC
- Design attributes in Lightning Web Components | CSS and SVG Files | Lightning Web Components | targetConfigs
- How to get current user id in lightning web component | Access logged in user ID in LWC
- Toast Notification in Lightning Web Components | ShowToastEvent | (LWC)
- Lightning Web Components Best practices
- Events in Lightning web components (LWC) | Communicate with Events
Check our YouTube Channel for more recording in Lightning Web Components.
Reference :-
1) https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/example
2) https://developer.salesforce.com/docs/component-library/documentation/lwc/data_table_inline_edit
Please share your feedback.
Is there a way to custom css cell/row's background color in lightning:datatable. I need that for conditional highlighting?
ReplyDeleteafter save , record is getting updated but the datatable is not getting updated withe the lastest data
ReplyDeleteHi Amit,
DeleteCan you please help regarding this? Should I use @wire to call apex method ?
Thanks
Suman
There could be two reasons,
DeleterefreshApex() refreshes the data that the wire service provisioned. It does not refresh if we call apex method imperatively. That could be one reason.
and other one is a typo. It should be return refreshApex(this.contacts); there is no contact property associated to result.
This comment has been removed by the author.
DeleteHello, check this post: https://salesforce.stackexchange.com/questions/318917/how-to-clear-draft-values-in-lwc-datatable-after-save
DeleteIt helped-me to solve my problem.
And I added the following to clear the draftValues.
After refresh over the provisioned value, I inserted the line: this.draftValues = [];
Important to say that in the html, also declare in the datatable: draft-values={draftValues}
Is there an way to hide row no's from data table?
ReplyDeleteget rid of "show-row-number-column" on lightning-datatable in case you have it and that should solve the problem as by default it is set to No
DeleteGood post and easy example for iniline editing
ReplyDeleteI am getting an error. An error occurred while trying to update the record. Please try again.
ReplyDeleteAny idea why is this error?
Thanks for sharing your thoughts on %meta_keyword%. Regards
ReplyDeleteHow can we write test class for apex method? I want to pass event.detail.draftValues from test class.
ReplyDeleteHow to display row number as seperate column in datatable
ReplyDelete