Create lwc datatable with sorting of columns by ascending and descending order that displays the rows and columns of data in lightning web component — LWC | How to sort columns by asc and desc order in lwc

4,284 views


Hey guys, today in this post we are going to learn about How to Create lwc datatable as resizing, text wrapping and clipping of columns with sorting of columns by ascending and descending order that displays the rows and columns of data in lightning web component — LWC.

Here I am creating lwc datatable with sorting of the ‘Age’ columns by ascending and descending order that displays the rows and columns of data in lightning web component (LWC).

Files we used in this post example

lwcDatatableSortableColm.html LWc HTML File Template HTML file to create datatable with sorting of columns by Ascending & Descending Order in lwc
lwcDatatableSortableColm.js LWC JavaScript File It’s hold a javascript on click function to sorting the ‘Age’ column by Ascending & Descending Order.
lwcVerticalTabs.js-meta.xml XML Meta File It is used to where this lightning web component file you want to expose.
lwcDatatableSortableColmApp Lightning Application It is used, call the LWC HTML file to preview on browser.

Final Output

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

lwc datatable with sorting of columns by asc and desc order -- www.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 ➑ lwcDatatableSortableColm.html

SFDX:Lightning Web Component ➑ New ➑ lwcDatatableSortableColm.html

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

lwcDatatableSortableColm.html [Lightning Web Component HTML]

  1.   <template>
  2.    <div class="slds-p-around--medium">
  3.     <h3 class="slds-text-heading_medium"><strong style="color:#270086;">Create Custom <span style="color:#ff8000">Data-Table with Sortable Column </span> in Lightning Web Component (LWC)</strong></h3> 
  4.     <lightning-datatable
  5.             key-field="id"
  6.             columns={columns}
  7.             data={data}
  8.             hide-checkbox-column
  9.             default-sort-direction={defaultSortDirection}
  10.             sorted-direction={sortDirection}
  11.             sorted-by={sortedBy}
  12.             onsort={onHandleSort}>
  13.     </lightning-datatable>
  14.  
  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. </div>  
  54. </template>

Create Lightning Web Component Javascript

Step 2:- Create Lightning Web Component Javascript ➑ lwcDatatableSortableColm.js

SFDX:Lightning Web Component ➑ New ➑ lwcDatatableSortableColm.js

lwcDatatableSortableColm.js [LWC JavaScript File]

  1.   import { LightningElement } from 'lwc';
  2.  
  3. const data = [
  4.     { id: 1, name: 'Alex Mike', age: 29, email: 'alexmike@w3web.net' },
  5.     { id: 2, name: 'Tina Roy', age: 38, email: 'tinaroy@w3web.net' },
  6.     { id: 3, name: 'Michael Jackson', age: 20, email: 'michaelJackson@w3web.net' },
  7.     { id: 4, name: 'Madan Mohan', age: 18, email: 'madanmohan@w3web.net',},
  8.     { id: 5, name: 'Suresh Mehta', age: 27, email: 'sureshmehta@w3web.net',},
  9.     { id: 6, name: 'Madhusudan Roy', age: 35, email: 'madhusudanroy@w3web.net',},
  10.     { id: 7, name: 'Vijay Kumar', age: 39, email: 'vijaykumar@w3web.net',},
  11.     { id: 8, name: 'Dharmendra Kapoor', age: 21, email: 'dharmendrakapoor@w3web.net',},
  12. ];
  13.  
  14. const columns = [
  15.     { label: 'Name', fieldName: 'name' },
  16.     {
  17.         label: 'Age',
  18.         fieldName: 'age',
  19.         type: 'number',
  20.         sortable: true,
  21.         cellAttributes: { alignment: 'left' },
  22.     },
  23.     { label: 'Email', fieldName: 'email', type: 'email' },
  24. ];
  25.  
  26. export default class lwcDatatableSortableColm extends LightningElement {
  27.     data = data;
  28.     columns = columns;
  29.     defaultSortDirection = 'asc';
  30.     sortDirection = 'asc';
  31.     sortedBy;
  32.  
  33.     // Used to sort the 'Age' column
  34.     sortBy(field, reverse, primer) {
  35.         const key = primer
  36.             ? function(x) {
  37.                   return primer(x[field]);
  38.               }
  39.             : function(x) {
  40.                   return x[field];
  41.               };
  42.  
  43.         return function(a, b) {
  44.             a = key(a);
  45.             b = key(b);
  46.             return reverse * ((a > b) - (b > a));
  47.         };
  48.     }
  49.  
  50.     onHandleSort(event) {
  51.         const { fieldName: sortedBy, sortDirection } = event.detail;
  52.         const cloneData = [...this.data];
  53.  
  54.         cloneData.sort(this.sortBy(sortedBy, sortDirection === 'asc' ? 1 : -1));
  55.         this.data = cloneData;
  56.         this.sortDirection = sortDirection;
  57.         this.sortedBy = sortedBy;
  58.     }
  59. }

Create Lightning Web Component Meta XML

Step 3:- Create Lightning Web Component Meta XML ➑ lwcDatatableSortableColm.js-meta.xml

SFDX:Lightning Web Component ➑ New >> lwcDatatableSortableColm.js-meta.xml

lwcDatatableSortableColm.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 Lightning Application

Step 4:- Create Lightning Application : LwcDatatableSortableColmApp.app

From Developer Console >> File >> New >> Lightning Application

LwcDatatableSortableColmApp.app [Component Application File]

  1.    <aura:application extends="force:slds">
  2.     <c:lwcDatatableSortableColm/>    
  3. </aura:application>

lwc datatable with sorting of columns by asc and desc order -- www.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 (added the clicked column as a secondary, tertiary etc ordering column).

What is multiple column sorting?

Allows you to choose up to three columns to sort by and returns a data set sorted by the selected column(s), adding a column called row_index that enables you to filter the output based on the sorting results.

How do you sort multiple columns independently of each other in Excel?

If you want to sort the table columns independently from each other, click on the Arrange All button in the ribbon toolbar tab Variables. After clicking, the Arrange_All function appears in the sidebar.

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