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.
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 →
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 : contactPaginationLwc.html
SFDX:Lightning Web Component >> New >> contactPaginationLwc.html
contactPaginationLwc.html [Lightning Web Component HTML]
<template>
<template if:true={loaderSpinner}>
<lightning-spinner variant="brand" alternative-text="Loading..." size="medium"></lightning-spinner>
</template>
<div class="slds-box slds-theme_default">
<lightning-card title="Contacts">
<table class="slds-table slds-table_cell-buffer slds-table_bordered">
<thead>
<tr class="slds-line-height_reset slds-text-title_caps">
<th class="slds-is-resizable" scope="col">
<div class="slds-truncate" title="Name">
Name
</div>
</th>
<th class="slds-is-resizable" scope="col">
<div class="slds-truncate" title="First Name">
First Name
</div>
</th>
<th class="slds-is-resizable" scope="col">
<div class="slds-truncate" title="Email">
Email
</div>
</th>
<th class="slds-is-resizable" scope="col">
<div class="slds-truncate" title="Phone">
Phone
</div>
</th>
<th class="slds-is-resizable" scope="col">
<div class="slds-truncate" title="Phone">
Title
</div>
</th>
</tr>
</thead>
<tbody>
<template if:true={contacts}>
<template for:each={contacts} for:item="conItem">
<tr key={conItem.Id}>
<th scope="row" data-label="Name">
<div class="slds-truncate" title={conItem.Name}>{conItem.Name}</div>
</th>
<th scope="row" data-label="Account Number">
<div class="slds-truncate" title={conItem.FirstName}>{conItem.FirstName}</div>
</th>
<th scope="row" data-label="Industry">
<div class="slds-truncate" title={conItem.LastName}>{conItem.LastName}</div>
</th>
<th scope="row" data-label="Phone">
<template if:true={conItem.Phone}>
<div class="slds-truncate" title={conItem.Phone}>{conItem.Phone}</div>
</template>
</th>
<th scope="row" data-label="Title">
<template if:true={conItem.Title}>
<div class="slds-truncate" title={conItem.Title}>{conItem.Phone}</div>
</template>
</th>
</tr>
</template>
</template>
</tbody>
</table>
<template if:true={isDisplayNoRecords}>
<div class="slds-align_absolute-center">
<br/>
No records found
</div>
</template>
<br/>
<div class="slds-align_absolute-center">
<div class="slds-p-right_xx-small">
<lightning-button label="Prev"
disabled={isPrev} onclick={handlePagePrevAction}
variant="brand"
icon-name="utility:back"
name="prev"></lightning-button>
</div>
<span class="slds-badge slds-badge_lightest">
{recordStart}-{recordEnd} of {totalRecords} | Page {pageNumber} of {totalPages}
</span>
<div class="slds-p-left_xx-small">
<lightning-button label="Next"
disabled={isNext} onclick={handlePageNextAction}
variant="brand"
icon-name="utility:forward"
icon-position="right"
name="next"></lightning-button>
</div>
</div>
<br/><br/>
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<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>
<br/><br/>
<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>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<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>
<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>
<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>
<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>
<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>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<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>
<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>
<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>
<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>
<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>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<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>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</lightning-card>
</div>
</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]
import { LightningElement, wire, api, track } from 'lwc';
import getContactList from '@salesforce/apex/contactPaginationLwcCtrl.getContactList';
export default class ContactPaginationLwc extends LightningElement {
@track recordEnd = 0;
@track recordStart = 0;
@track pageNumber = 1;
@track totalRecords = 0;
@track totalPages = 0;
@track loaderSpinner = false;
@track error = null;
@track pageSize = 10;
@track isPrev = true;
@track isNext = true;
@track contacts = [];
connectedCallback() {
this.getContacts();
}
handlePageNextAction(){
this.pageNumber = this.pageNumber+1;
this.getContacts();
}
handlePagePrevAction(){
this.pageNumber = this.pageNumber-1;
this.getContacts();
}
getContacts(){
this.loaderSpinner = true;
getContactList({pageSize: this.pageSize, pageNumber : this.pageNumber})
.then(result => {
this.loaderSpinner = false;
if(result){
var resultData = JSON.parse(result);
this.recordEnd = resultData.recordEnd;
this.totalRecords = resultData.totalRecords;
this.recordStart = resultData.recordStart;
this.contacts = resultData.contacts;
this.pageNumber = resultData.pageNumber;
this.totalPages = Math.ceil(resultData.totalRecords / this.pageSize);
this.isNext = (this.pageNumber == this.totalPages || this.totalPages == 0);
this.isPrev = (this.pageNumber == 1 || this.totalRecords < this.pageSize);
}
})
.catch(error => {
this.loaderSpinner = false;
this.error = error;
});
}
get isDisplayNoRecords() {
var isDisplay = true;
if(this.contacts){
if(this.contacts.length == 0){
isDisplay = true;
}else{
isDisplay = false;
}
}
return isDisplay;
}
}
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]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Create Apex Class Controller →
Step 4:- Create Apex Class : contactPaginationLwcCtrl.cls
From Developer Console >> File >> New >> Apex Class
contactPaginationLwcCtrl.cls [Apex Class Controller]
public WITH sharing class contactPaginationLwcCtrl {
public class ContactItemWrapper {
public INTEGER recordStart {GET;SET;}
public INTEGER pageNumber {GET;SET;}
public INTEGER totalRecords {GET;SET;}
public INTEGER recordEnd {GET;SET;}
public INTEGER pageSize {GET;SET;}
public List<Contact> contacts {GET;SET;}
}
@AuraEnabled
public static String getContactList(INTEGER pageSize, INTEGER pageNumber){
String jsonObjItm = '';
INTEGER offset = (pageNumber - 1) * pageSize;
INTEGER totalRecords = [SELECT COUNT() FROM Contact];
INTEGER recordEnd = pageSize * pageNumber;
ContactItemWrapper conObj = NEW ContactItemWrapper();
conObj.pageNumber = pageNumber;
conObj.pageSize = pageSize;
conObj.recordStart = offset + 1;
conObj.recordEnd = totalRecords >= recordEnd ? recordEnd : totalRecords;
conObj.totalRecords = totalRecords;
conObj.contacts = [SELECT Id, Name, FirstName, LastName, Email, Phone, Title FROM Contact LIMIT :pageSize OFFSET :offset];
jsonObjItm = JSON.serialize(conObj);
RETURN jsonObjItm;
}
}
Further post that would you like to learn in Salesforce
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
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 |