How to create custom dynamic pagination in custom table using apex class method in Lightning Web Component (LWC) | Example of dynamic pagination creation with custom table uses of apex class method in LWC – lightning web component

7,193 views

Hey guys, today in this post we are going to learn about How to create custom dynamic pagination in custom table using apex class method in Lightning Web Component (lwc).

Pagination is the process of displaying large number of records and displaying the records on multiple pages within in Salesforce.

In order to control the number of records displayed on each page, we use pagination.

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

 

You can add pagination to a page using a list controller by utilizing the next and previous actions.

By default, a list controller returns 20 records on the page. To know more details about Pagination with a List Controller, Click Here..

 

Files we used to add dynamic pagination in Salesforce LWC →

contactPaginationLwc.html LWC HTML File Template HTML file to create dynamic pagination using apex method in Salesforce Lightning Web Component (LWC)
contactPaginationLwc.js LWC JavaScript File It’s javascript actions (Next/Prev) functions to control the dynamic pagination data in Salesforce LWC
contactPaginationLwc.js-meta.xml XML Meta File It is used to where this lightning web component file you want to display as lightning__AppPage, lightning__RecordPage, lightning__HomePage.

 

 

Final Output →

creaet custom dynamic pagination 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

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  • Find the below steps ▾

 

Create Lightning Web Component HTML →

Step 1:- Create Lightning Web Component : contactPaginationLwc.html

SFDX:Lightning Web Component >> New >> contactPaginationLwc.html

contactPaginationLwc.html [Lightning Web Component HTML]

  1.    <template>
  2.  
  3.     <template if:true={loaderSpinner}>
  4.         <lightning-spinner variant="brand" alternative-text="Loading..." size="medium"></lightning-spinner>
  5.     </template>
  6.  
  7.     <div class="slds-box slds-theme_default">
  8.         <lightning-card  title="Contacts">
  9.             <table class="slds-table slds-table_cell-buffer slds-table_bordered">
  10.                 <thead>
  11.                     <tr class="slds-line-height_reset slds-text-title_caps">
  12.                         <th  class="slds-is-resizable" scope="col">
  13.                             <div class="slds-truncate" title="Name">
  14.                                 Name
  15.                             </div>
  16.                         </th>
  17.                         <th  class="slds-is-resizable" scope="col">
  18.                             <div class="slds-truncate" title="First Name">
  19.                                First Name
  20.                             </div>
  21.                         </th>
  22.                         <th  class="slds-is-resizable" scope="col">
  23.                             <div class="slds-truncate" title="Email">
  24.                                 Email
  25.                             </div>
  26.                         </th>
  27.                         <th class="slds-is-resizable" scope="col">
  28.                             <div class="slds-truncate" title="Phone">
  29.                                 Phone
  30.                             </div>
  31.                         </th>
  32.                         <th class="slds-is-resizable" scope="col">
  33.                             <div class="slds-truncate" title="Phone">
  34.                                 Title
  35.                             </div>
  36.                         </th>
  37.                     </tr>
  38.                 </thead>
  39.                 <tbody>
  40.                     <template if:true={contacts}>
  41.                         <template for:each={contacts} for:item="conItem">
  42.                             <tr key={conItem.Id}>
  43.                                 <th scope="row" data-label="Name">
  44.                                     <div class="slds-truncate" title={conItem.Name}>{conItem.Name}</div>
  45.                                 </th>
  46.                                 <th scope="row" data-label="Account Number">
  47.                                     <div class="slds-truncate" title={conItem.FirstName}>{conItem.FirstName}</div>
  48.                                 </th>
  49.                                 <th scope="row" data-label="Industry">
  50.                                     <div class="slds-truncate" title={conItem.LastName}>{conItem.LastName}</div>
  51.                                 </th>
  52.                                 <th scope="row" data-label="Phone">
  53.                                     <template if:true={conItem.Phone}>
  54.                                     <div class="slds-truncate" title={conItem.Phone}>{conItem.Phone}</div>
  55.                                 </template>
  56.                                 </th>
  57.                                 <th scope="row" data-label="Title">
  58.                                     <template if:true={conItem.Title}>
  59.                                     <div class="slds-truncate" title={conItem.Title}>{conItem.Phone}</div>
  60.                                 </template>
  61.                                 </th>
  62.                             </tr>
  63.                         </template>
  64.                     </template>
  65.                 </tbody>
  66.             </table>
  67.             <template if:true={isDisplayNoRecords}>
  68.                 <div class="slds-align_absolute-center">
  69.                     <br/>
  70.                     No records found
  71.                 </div>
  72.             </template>
  73.             <br/>
  74.             <div class="slds-align_absolute-center"> 
  75.                 <div class="slds-p-right_xx-small">
  76.  
  77.                     <lightning-button label="Prev"
  78.                     disabled={isPrev} onclick={handlePagePrevAction}
  79.                                         variant="brand"
  80.                                         icon-name="utility:back"
  81.                                         name="prev"></lightning-button>  
  82.                 </div>
  83.                 <span class="slds-badge slds-badge_lightest">
  84.                     {recordStart}-{recordEnd} of {totalRecords} | Page {pageNumber} of {totalPages}
  85.                 </span>
  86.                 <div class="slds-p-left_xx-small">
  87.                     <lightning-button label="Next"
  88.                     disabled={isNext} onclick={handlePageNextAction}
  89.                                         variant="brand"
  90.                                         icon-name="utility:forward"
  91.                                         icon-position="right"
  92.                                         name="next"></lightning-button>
  93.                 </div>
  94.             </div>  
  95.  
  96.  
  97.             <br/><br/>
  98.    <!--Start RelatedTopics Section-->
  99. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  100.  
  101.             <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>
  102.  
  103.             <br/><br/>
  104.             <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>
  105.             <div style="display:block; overflow:hidden;"> 
  106.                 <div style="width: 50%; float:left; display:inline-block">
  107.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  108.                         <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>
  109.                         <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>
  110.                         <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>
  111.                         <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>
  112.                         <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>
  113.                     </ul>
  114.             </div>
  115.  
  116.             <div style="width: 50%; float:left; display:inline-block">
  117.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  118.                         <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>
  119.                         <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>
  120.                         <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>
  121.                         <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>
  122.                         <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>
  123.                     </ul>
  124.                 </div>
  125.                <div style="clear:both;"></div> 
  126.                <br/>
  127.                 <div class="youtubeIcon">
  128.                     <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>
  129.                 </div>
  130.     </div>
  131.  
  132. </div>
  133.  
  134.   <!--End RelatedTopics Section-->
  135.         </lightning-card>
  136.     </div>
  137. </template>

Create Lightning Web Component JavaScript →

Step 2:- Create Lightning Web Component : contactPaginationLwc.js

SFDX:Lightning Web Component >> New >> contactPaginationLwc.js

contactPaginationLwc.js [LWC JavaScript File]

  1.    import { LightningElement, wire, api, track } from 'lwc';
  2.  
  3. import getContactList from '@salesforce/apex/contactPaginationLwcCtrl.getContactList';
  4.  
  5. export default class ContactPaginationLwc extends LightningElement {
  6.  
  7.     @track recordEnd = 0;
  8.     @track recordStart = 0;
  9.     @track pageNumber = 1;
  10.     @track totalRecords = 0;
  11.     @track totalPages = 0;
  12.     @track loaderSpinner = false;
  13.     @track error = null;
  14.     @track pageSize = 10;    
  15.     @track isPrev = true;
  16.     @track isNext = true;
  17.     @track contacts = [];
  18.  
  19.     connectedCallback() {
  20.         this.getContacts();
  21.     }
  22.  
  23.  
  24.     handlePageNextAction(){
  25.         this.pageNumber = this.pageNumber+1;
  26.         this.getContacts();
  27.     }
  28.  
  29.  
  30.     handlePagePrevAction(){
  31.         this.pageNumber = this.pageNumber-1;
  32.         this.getContacts();
  33.     }
  34.  
  35.  
  36.     getContacts(){
  37.         this.loaderSpinner = true;
  38.         getContactList({pageSize: this.pageSize, pageNumber : this.pageNumber})
  39.         .then(result => {
  40.             this.loaderSpinner = false;
  41.             if(result){
  42.                 var resultData = JSON.parse(result);
  43.                 this.recordEnd = resultData.recordEnd;
  44.                 this.totalRecords = resultData.totalRecords;
  45.                 this.recordStart = resultData.recordStart;
  46.                 this.contacts = resultData.contacts;
  47.                 this.pageNumber = resultData.pageNumber;                
  48.                 this.totalPages = Math.ceil(resultData.totalRecords / this.pageSize);
  49.                 this.isNext = (this.pageNumber == this.totalPages || this.totalPages == 0);
  50.                 this.isPrev = (this.pageNumber == 1 || this.totalRecords < this.pageSize);
  51.             }
  52.         })
  53.         .catch(error => {
  54.             this.loaderSpinner = false;
  55.             this.error = error;
  56.         });
  57.     }
  58.  
  59.  
  60.     get isDisplayNoRecords() {
  61.         var isDisplay = true;
  62.         if(this.contacts){
  63.             if(this.contacts.length == 0){
  64.                 isDisplay = true;
  65.             }else{
  66.                 isDisplay = false;
  67.             }
  68.         }
  69.         return isDisplay;
  70.     }
  71.  
  72. }

Create Lightning Web Component Meta XML →

Step 3:- Create Lightning Web Component : contactPaginationLwc.js-meta.xml

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

contactPaginationLwc.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 Class : contactPaginationLwcCtrl.cls

From Developer Console >> File >> New >> Apex Class

contactPaginationLwcCtrl.cls [Apex Class Controller]

  1.     public WITH sharing class contactPaginationLwcCtrl {
  2.  
  3.     public class ContactItemWrapper {       
  4.         public INTEGER recordStart {GET;SET;}
  5.         public INTEGER pageNumber {GET;SET;}
  6.         public INTEGER totalRecords {GET;SET;}
  7.         public INTEGER recordEnd {GET;SET;}
  8.         public INTEGER pageSize {GET;SET;}       
  9.         public List<Contact> contacts {GET;SET;}
  10.     }
  11.  
  12.     @AuraEnabled
  13.     public static String getContactList(INTEGER pageSize, INTEGER pageNumber){
  14.         String jsonObjItm = '';
  15.  
  16.  
  17.         INTEGER offset = (pageNumber - 1) * pageSize;         
  18.  
  19.         INTEGER totalRecords = [SELECT COUNT() FROM Contact];
  20.         INTEGER recordEnd = pageSize * pageNumber;
  21.  
  22.         ContactItemWrapper conObj =  NEW ContactItemWrapper();  
  23.         conObj.pageNumber = pageNumber;
  24.         conObj.pageSize = pageSize;        
  25.         conObj.recordStart = offset + 1;
  26.         conObj.recordEnd = totalRecords >= recordEnd ? recordEnd : totalRecords;
  27.         conObj.totalRecords = totalRecords;
  28.         conObj.contacts = [SELECT Id, Name, FirstName, LastName, Email, Phone, Title FROM Contact LIMIT :pageSize OFFSET :offset];
  29.         jsonObjItm = JSON.serialize(conObj);
  30.         RETURN jsonObjItm;
  31.     }
  32.  
  33. }

 

creaet custom dynamic pagination in lwc -- w3web.net
 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

What is pagination in LWC?

It is used to split a huge content in the tables into smaller parts. By default, pagination provides Previous button, Next button with page numbers and total records.

How do I Paginate in Salesforce?

Pagination is the process of displaying large number of records and displaying the records on multiple pages within in Salesforce. In order to control the number of records displayed on each page, we use pagination. By default, a list controller returns 20 records on the page.

What is pagination programming?

Pagination is a process that is used to divide a large data into smaller discrete pages, and this process is also known as paging. Pagination is commonly used by web applications and can be seen on Google.

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