lwc datatable with sorting of columns ascending and descending order of account object’s fields Uses of ‘lightning-datatable’ elements in Salesforce — LWC | How to sorting of columns ascending and descending order in lwc

3,874 views


Hey guys, today in this post we are going to learn about How to sorting of columns ascending and descending order of account object’s fields Uses of ‘lightning-datatable’ elements in Salesforce Lightning Web Component — LWC.

Here I am creating lwc datatable with sorting the columns of Account Object by ascending and descending order in lightning web component (LWC).

Files we used in this post example

lwcSortingDataTable.html LWc HTML File Template HTML file to create datatable with sorting of columns by Ascending & Descending Order in lwc
lwcSortingDataTable.js LWC JavaScript File It’s hold a javascript on click function to sorting the column of Account Object by Ascending & Descending Order.
lwcSortingDataTable.js-meta.xml XML Meta File It is used to where this lightning web component file you want to display as like lightning__AppPage, lightning__RecordPage, lightning__HomePage.
lwcSortingDataTableCtrl.cls Apex Class Controller It is used for fetching the data of Account from server.

Final Output

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

sort columns in ascending and descending order in lwc -- w3web.net

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

  • Find the below steps

Create Lightning Web Component HTML

Step 1:- Create Lightning Web Component HTML ➡ lwcSortingDataTable.html

SFDX:Lightning Web Component ➡ New ➡ lwcSortingDataTable.html

lwcSortingDataTable.html [Lightning Web Component HTML]

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  1.    <template>
  2.     <lightning-card title="Data-Table with sorting of columns by ascending and descending order in LWC" icon-name="standard:account" > <br/>
  3.         <div style="width: auto;">
  4.             <template if:true={data}>
  5.                 <lightning-datatable data={data}
  6.                                      columns={columns}
  7.                                      key-field="id"
  8.                                      sorted-by={sortBy}
  9.                                      sorted-direction={sortDirection}
  10.                                      onsort={handleSortAccountData}
  11.                                      hide-checkbox-column="true"></lightning-datatable>
  12.             </template>
  13.  
  14.         </div>
  15. <br/><br/>
  16.    <!--Start RelatedTopics Section-->
  17. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  18.  
  19.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click  <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
  20.  
  21.             <br/><br/>
  22.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p>
  23.             <div style="display:block; overflow:hidden;"> 
  24.                 <div style="width: 50%; float:left; display:inline-block">
  25.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  26.                         <li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
  27.                         <li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
  28.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
  29.                         <li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
  30.                         <li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
  31.                     </ul>
  32.             </div>
  33.  
  34.             <div style="width: 50%; float:left; display:inline-block">
  35.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  36.                         <li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
  37.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
  38.                         <li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
  39.                         <li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
  40.                         <li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
  41.                     </ul>
  42.                 </div>
  43.                <div style="clear:both;"></div> 
  44.                <br/>
  45.                 <div class="youtubeIcon">
  46.                     <a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
  47.                 </div>
  48.     </div>
  49.  
  50. </div>
  51.  
  52.   <!--End RelatedTopics Section-->
  53.  
  54.    </lightning-card> 
  55. </template>
  56.  
  57.     </lightning-card>
  58. </template>

Create Lightning Web Component Javascript

Step 2:- Create Lightning Web Component Javascript ➡ lwcSortingDataTable.js

SFDX:Lightning Web Component ➡ New ➡ lwcSortingDataTable.js

lwcSortingDataTable.js [LWC JavaScript File]

  1.    import { LightningElement , wire, track} from 'lwc';
  2. import sortAccountItem from '@salesforce/apex/lwcSortingDataTableCtrl.sortAccountList';
  3. const columns = [
  4.     {
  5.         label: 'Name',
  6.         fieldName: 'Name',
  7.         sortable: "true"
  8.     }, {
  9.         label: 'Phone',
  10.         fieldName: 'Phone',
  11.         sortable: "true"
  12.     }, {
  13.         label: 'Industry',
  14.         fieldName: 'Industry',
  15.         type: 'Picklist',
  16.         sortable: "true"
  17.     }, {
  18.         label: 'Type',
  19.         fieldName: 'Type',
  20.         type: 'Picklist',
  21.         sortable: "true"
  22.     }, {
  23.         label: 'Website',
  24.         fieldName: 'Website',
  25.         type: 'URL',
  26.         sortable: "true"
  27.     },
  28. ];
  29. export default class LwcSortingDataTable extends LightningElement {
  30.     @track data;
  31.     @track columns = columns;
  32.     @track sortBy;
  33.     @track sortDirection;
  34.     @wire(sortAccountItem)
  35.     accounts(result) {
  36.         if (result.data) {
  37.             this.data = result.data;
  38.             this.error = undefined;
  39.  
  40.         } else if (result.error) {
  41.             this.error = result.error;
  42.             this.data = undefined;
  43.         }
  44.     }
  45.  
  46.  
  47.     handleSortAccountData(event) {       
  48.         this.sortBy = event.detail.fieldName;       
  49.         this.sortDirection = event.detail.sortDirection;       
  50.         this.sortAccountData(event.detail.fieldName, event.detail.sortDirection);
  51.     }
  52.  
  53.     sortAccountData(fieldname, direction) {
  54.  
  55.         let parseData = JSON.parse(JSON.stringify(this.data));
  56.  
  57.         let keyValue = (a) => {
  58.             return a[fieldname];
  59.         };
  60.  
  61.        let isReverse = direction === 'asc' ? 1: -1;
  62.  
  63.            parseData.sort((x, y) => {
  64.             x = keyValue(x) ? keyValue(x) : ''; 
  65.             y = keyValue(y) ? keyValue(y) : '';
  66.  
  67.             return isReverse * ((x > y) - (y > x));
  68.         });
  69.  
  70.         this.data = parseData;
  71.  
  72.     }
  73.  
  74. }

Create Lightning Web Component Meta XML

Step 3:- Create Lightning Web Component Meta XML ➡ lwcSortingDataTable.js-meta.xml

SFDX:Lightning Web Component ➡ New >> lwcSortingDataTable.js-meta.xml

lwcSortingDataTable.js-meta.xml [LWC Meta Data XML]

  1.    <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>45.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets>
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.       </targets>
  10. </LightningComponentBundle>

Create Apex Class Controller

Step 4:- Create Apex Controller ➡ lwcSortingDataTableCtrl.cls

SFDX:Create Apex Class ➡ New ➡ lwcSortingDataTableCtrl.cls

lwcSortingDataTableCtrl.cls [Apex Class]

  1.    public WITH sharing class lwcSortingDataTableCtrl {
  2.  
  3.     @AuraEnabled(Cacheable = TRUE)
  4.     public static List<Account> sortAccountList(){
  5.         List<Account> accListObj= [SELECT Id, Name, Phone, Industry, Description, TYPE, Website FROM Account WHERE Phone != NULL LIMIT 20 ];
  6.         RETURN accListObj;
  7.     }
  8.  
  9. }

sort columns in ascending and descending order in lwc -- w3web.net

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

How do I sort multiple columns in DataTable?

DataTables allows ordering by multiple columns at the same time, which can be activated in a number of different ways:User shift click on a column.

How do I sort a column in DataTable?

Using the order initialisation parameter, you can set the table to display the data in exactly the order that you want.

What is default view in DataTable?

The DefaultView property returns a DataView you can use to sort, filter, and search a DataTable.

Related Topics | You May Also Like

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment