Last time we talk about Lightning Datatable in Lightning Web Components (LWC). In this post we will talk about lightning datatable example with sorting. We know lightning-datatable component displays in tabular data and each column can be displayed based on the data type. We can also achieve the column sorting with the help of onsort attribute in datatable. I hope VsCode is already setup on you machine and you know how to create Lightning Web Component. If not please check our Get started with Salesforce lightning web components post.
Lightning-datatable
Lightning datatable provides an onsort attribute which allow us to implement the sorting in lightning datatable. To enable the sorting on row you need to set sortable to true for the column and set sorted-By to match the fieldName attribute on the column.
Use onsort event handler to update the table with the new column index and sort direction. The sort event returns the following parameter.
- fieldName The fieldName that controls the sorting.
- sortDirection The sorting direction. Valid options include 'asc' and 'desc'.
You can implement sorting locally or via apex call.
Local Sorting:
We mostly implement this type of sorting when we know data elements in lightning datatable is small and limited.
1) Create Apex Class.
To select certain contacts using SOQL, use an Apex method. Check this post to learn about how to Call Apex Methods in LWC.
LWCDataTableSortingExample:
public with sharing class LWCDataTableSortingExample {
@AuraEnabled(Cacheable=true)
public static List <Contact> getContacts() {
List<Contact> contList = [ SELECT Id, FirstName, LastName, Phone, Email
FROM Contact
LIMIT 10 ];
return contList;
}
}
@AuraEnabled(Cacheable=true)
public static List <Contact> getContacts() {
List<Contact> contList = [ SELECT Id, FirstName, LastName, Phone, Email
FROM Contact
LIMIT 10 ];
return contList;
}
}
2) Create Lightning web component.
dataTableSortingLWC.html
<template>
<lightning-card title="Data Sorting in Lightning Datatable in LWC" icon-name="standard:contact" >
<br/>
<div style="width: auto;">
<template if:true={data}>
<lightning-datatable data={data}
columns={columns}
key-field="id"
sorted-by={sortBy}
sorted-direction={sortDirection}
onsort={doSorting}
hide-checkbox-column="true"></lightning-datatable>
</template>
</div>
</lightning-card>
</template>
<lightning-card title="Data Sorting in Lightning Datatable in LWC" icon-name="standard:contact" >
<br/>
<div style="width: auto;">
<template if:true={data}>
<lightning-datatable data={data}
columns={columns}
key-field="id"
sorted-by={sortBy}
sorted-direction={sortDirection}
onsort={doSorting}
hide-checkbox-column="true"></lightning-datatable>
</template>
</div>
</lightning-card>
</template>
dataTableSortingLWC.js
import {LightningElement, wire, track} from 'lwc';
import getContacts from '@salesforce/apex/LWCDataTableSortingExample.getContacts';
// datatable columns with row actions. Set sortable = true
const columns = [ { label: 'FirstName', fieldName: 'FirstName', sortable: "true"},
{ label: 'LastName', fieldName: 'LastName', sortable: "true"},
{ label: 'Phone', fieldName: 'Phone', type: 'phone', sortable: "true"},
{ label: 'Email', fieldName: 'Email', type: 'email', sortable: "true" },];
export default class DataTableSortingLWC extends LightningElement {
@track data;
@track columns = columns;
@track sortBy;
@track sortDirection;
@wire(getContacts)
contacts(result) {
if (result.data) {
this.data = result.data;
this.error = undefined;
} else if (result.error) {
this.error = result.error;
this.data = undefined;
}
}
doSorting(event) {
this.sortBy = event.detail.fieldName;
this.sortDirection = event.detail.sortDirection;
this.sortData(this.sortBy, this.sortDirection);
}
sortData(fieldname, direction) {
let parseData = JSON.parse(JSON.stringify(this.data));
// Return the value stored in the field
let keyValue = (a) => {
return a[fieldname];
};
// cheking reverse direction
let isReverse = direction === 'asc' ? 1: -1;
// sorting data
parseData.sort((x, y) => {
x = keyValue(x) ? keyValue(x) : ''; // handling null values
y = keyValue(y) ? keyValue(y) : '';
// sorting values based on direction
return isReverse * ((x > y) - (y > x));
});
this.data = parseData;
}
}
import getContacts from '@salesforce/apex/LWCDataTableSortingExample.getContacts';
// datatable columns with row actions. Set sortable = true
const columns = [ { label: 'FirstName', fieldName: 'FirstName', sortable: "true"},
{ label: 'LastName', fieldName: 'LastName', sortable: "true"},
{ label: 'Phone', fieldName: 'Phone', type: 'phone', sortable: "true"},
{ label: 'Email', fieldName: 'Email', type: 'email', sortable: "true" },];
export default class DataTableSortingLWC extends LightningElement {
@track data;
@track columns = columns;
@track sortBy;
@track sortDirection;
@wire(getContacts)
contacts(result) {
if (result.data) {
this.data = result.data;
this.error = undefined;
} else if (result.error) {
this.error = result.error;
this.data = undefined;
}
}
doSorting(event) {
this.sortBy = event.detail.fieldName;
this.sortDirection = event.detail.sortDirection;
this.sortData(this.sortBy, this.sortDirection);
}
sortData(fieldname, direction) {
let parseData = JSON.parse(JSON.stringify(this.data));
// Return the value stored in the field
let keyValue = (a) => {
return a[fieldname];
};
// cheking reverse direction
let isReverse = direction === 'asc' ? 1: -1;
// sorting data
parseData.sort((x, y) => {
x = keyValue(x) ? keyValue(x) : ''; // handling null values
y = keyValue(y) ? keyValue(y) : '';
// sorting values based on direction
return isReverse * ((x > y) - (y > x));
});
this.data = parseData;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="dataTableSortingLWC">
<apiVersion>46.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="dataTableSortingLWC">
<apiVersion>46.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Sorting by Apex Call:
We also have another way of data sorting with Apex class.
1) Create Apex Class.
LWCDataTableSortingExample:
public with sharing class LWCDataTableSortingExample {
@AuraEnabled(Cacheable=true)
public static List <Contact> getContacts(String field, String sortOrder) {
String query;
query = 'SELECT Id, FirstName, LastName, Phone, Email FROM Contact';
if(field != null && sortOrder !=null){
query += ' ORDER BY '+field+' '+sortOrder;
}
return Database.query(query);
}
}
@AuraEnabled(Cacheable=true)
public static List <Contact> getContacts(String field, String sortOrder) {
String query;
query = 'SELECT Id, FirstName, LastName, Phone, Email FROM Contact';
if(field != null && sortOrder !=null){
query += ' ORDER BY '+field+' '+sortOrder;
}
return Database.query(query);
}
}
2) Create Lightning web component.
<template>
<lightning-card title="Data Sorting by Apex" icon-name="standard:contact" >
<br/>
<div style="width: auto;">
<template if:true={data}>
<lightning-datatable data={data}
columns={columns}
key-field="id"
sorted-by={sortBy}
sorted-direction={sortDirection}
onsort={doSorting}
hide-checkbox-column="true"></lightning-datatable>
</template>
</div>
</lightning-card>
</template>
<lightning-card title="Data Sorting by Apex" icon-name="standard:contact" >
<br/>
<div style="width: auto;">
<template if:true={data}>
<lightning-datatable data={data}
columns={columns}
key-field="id"
sorted-by={sortBy}
sorted-direction={sortDirection}
onsort={doSorting}
hide-checkbox-column="true"></lightning-datatable>
</template>
</div>
</lightning-card>
</template>
dataTableSortingLWC.js
import {LightningElement, wire, track} from 'lwc';
import getContacts from '@salesforce/apex/LWCDataTableSortingExample.getContacts';
// datatable columns with row actions
const columns = [ { label: 'FirstName', fieldName: 'FirstName', sortable: "true"},
{ label: 'LastName', fieldName: 'LastName', sortable: "true"},
{ label: 'Phone', fieldName: 'Phone', type: 'phone', sortable: "true"},
{ label: 'Email', fieldName: 'Email', type: 'email', sortable: "true" },];
export default class DataTableSortingLWC extends LightningElement {
// reactive variable
@track data;
@track columns = columns;
@track sortBy='FirstName'
@track sortDirection='asc';
// retrieving the data using wire service
@wire(getContacts,{field : '$sortBy',sortOrder : '$sortDirection'})
contacts(result) {
if (result.data) {
this.data = result.data;
this.error = undefined;
} else if (result.error) {
this.error = result.error;
this.data = undefined;
}
}
doSorting(event) {
// calling sortdata function to sort the data based on direction and selected field
this.sortBy = event.detail.fieldName;
this.sortDirection = event.detail.sortDirection;
this.sortData(this.sortBy, this.sortDirection);
}
sortData(fieldname, direction) {
let parseData = JSON.parse(JSON.stringify(this.data));
// Return the value stored in the field
let keyValue = (a) => {
return a[fieldname];
};
// cheking reverse direction
let isReverse = direction === 'asc' ? 1: -1;
// sorting data
parseData.sort((x, y) => {
x = keyValue(x) ? keyValue(x) : ''; // handling null values
y = keyValue(y) ? keyValue(y) : '';
// sorting values based on direction
return isReverse * ((x > y) - (y > x));
});
// set the sorted data to data table data
this.data = parseData;
}
}
import getContacts from '@salesforce/apex/LWCDataTableSortingExample.getContacts';
// datatable columns with row actions
const columns = [ { label: 'FirstName', fieldName: 'FirstName', sortable: "true"},
{ label: 'LastName', fieldName: 'LastName', sortable: "true"},
{ label: 'Phone', fieldName: 'Phone', type: 'phone', sortable: "true"},
{ label: 'Email', fieldName: 'Email', type: 'email', sortable: "true" },];
export default class DataTableSortingLWC extends LightningElement {
// reactive variable
@track data;
@track columns = columns;
@track sortBy='FirstName'
@track sortDirection='asc';
// retrieving the data using wire service
@wire(getContacts,{field : '$sortBy',sortOrder : '$sortDirection'})
contacts(result) {
if (result.data) {
this.data = result.data;
this.error = undefined;
} else if (result.error) {
this.error = result.error;
this.data = undefined;
}
}
doSorting(event) {
// calling sortdata function to sort the data based on direction and selected field
this.sortBy = event.detail.fieldName;
this.sortDirection = event.detail.sortDirection;
this.sortData(this.sortBy, this.sortDirection);
}
sortData(fieldname, direction) {
let parseData = JSON.parse(JSON.stringify(this.data));
// Return the value stored in the field
let keyValue = (a) => {
return a[fieldname];
};
// cheking reverse direction
let isReverse = direction === 'asc' ? 1: -1;
// sorting data
parseData.sort((x, y) => {
x = keyValue(x) ? keyValue(x) : ''; // handling null values
y = keyValue(y) ? keyValue(y) : '';
// sorting values based on direction
return isReverse * ((x > y) - (y > x));
});
// set the sorted data to data table data
this.data = parseData;
}
}
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
- Lightning datatable In Lightning Web Components | lightning datatable inline edit
- Lightning Message Service (LMS) | MessageChannel
Thanks
Amit Chaudhary
No comments:
Post a Comment