Pagination Of Account Using Server Side Sorting On Lightning Component
PAGINATION OF ACCOUNT USING SERVER SIDE SORTING ON LIGHTNING COMPONENT
Component:
<aura:component controller="AccountServersidesorting" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes"
access="global">
scripts="/resource/bootstrap/js/jquery.js,/resource/bootstrap/js/bootstrap.min.js"/> -->
<aura:attribute name="arrowDirection" type="string" default="arrowup" description="Use for change arrow sign direction on header based on click"/>
<aura:attribute name="isAsc" type="boolean" default="true" description="boolean flag for pass sorting condition to apex class"/>
<aura:attribute name="selectedTabsoft" type="string" default="Name" description="Use for show/hide arraow sign on header based on conditions"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:attribute name="Accounts" type="Account[]"/>
<aura:attribute name="page" type="integer" description="using for store page Number"/>
<aura:attribute name="pages" type="integer" description="using for store All Pages page Number"/>
<aura:attribute name="total" type="integer" description="total records count store "/>
<div class="slds-page-header" role="banner">
<div class="slds-grid">
<div class="slds-col slds-has-flexi-truncate">
<div class="slds-media slds-media--center slds-no-space slds-grow">
<div class="slds-media__figure">
<lightning:icon iconName="standard:account" size="small" alternativeText="Indicates approval"/>
</div>
<div class="slds-media__body">
<h1 class="slds-page-header__title slds-m-right--small slds-truncate slds-align-middle" title="Accounts">Accounts</h1>
</div>
</div>
</div>
</div>
</div>
<form class="slds-form--stacked">
<div class="slds">
<table class="slds-table slds-table--bordered slds-max-medium-table--stacked-horizontal"><!--Table must be responsive-->
<thead>
<tr class="slds-text-heading--label ">
<th class="slds-is-sortable slds-text-title--caps" scope="col" onclick="{!c.sortAccountName}">
<a href="javascript:void(0);" class="slds-th__action slds-text-link--reset">
<span class="slds-assistive-text">Sort</span>
<span class="slds-truncate" title="Account Name">Account Name</span>
<aura:if isTrue="{! and(v.arrowDirection == 'arrowdown', v.selectedTabsoft == 'Name') }">
<div class="slds-icon_container">
<lightning:icon iconName="utility:arrowdown" size="xx-small" alternativeText="sort arraw down"/>
</div>
</aura:if>
<aura:if isTrue="{! and(v.arrowDirection != 'arrowdown', v.selectedTabsoft == 'Name') }">
<div class="slds-icon_container">
<lightning:icon iconName="utility:arrowup" size="xx-small" alternativeText="sort arraw up"/>
</div>
</aura:if>
</a>
</th>
<th class="slds-is-sortable slds-text-title--caps" scope="col" onclick=" {!c.sortAccountSource}">
<a href="javascript:void(0);" class="slds-th__action slds-text-link--reset">
<span class="slds-assistive-text">Sort</span>
<span class="slds-truncate" title="Account Source">Account Source</span>
<aura:if isTrue="{! and(v.arrowDirection == 'arrowdown', v.selectedTabsoft == 'AccountSource') }">
<div class="slds-icon_container">
<lightning:icon iconName="utility:arrowdown" size="xx-small" alternativeText="sort arraw down"/>
</div>
</aura:if>
<aura:if isTrue="{! and(v.arrowDirection != 'arrowdown', v.selectedTabsoft == 'AccountSource') }">
<div class="slds-icon_container">
<lightning:icon iconName="utility:arrowup" size="xx-small" alternativeText="sort arraw up"/>
</div></aura:if>
</a>
</th>
<th class="slds-is-sortable slds-text-title--caps" scope="col" onclick=" {!c.sortAccountNumber}">
<a href="javascript:void(0);" class="slds-th__action slds-text-link--reset">
<span class="slds-assistive-text">Sort</span>
<span class="slds-truncate" title="Account Number">Account Number</span>
<aura:if isTrue="{! and(v.arrowDirection == 'arrowdown', v.selectedTabsoft == 'AccountNumber') }">
<div class="slds-icon_container">
<lightning:icon iconName="utility:arrowdown" size="xx-small" alternativeText="sort arraw down"/>
</div>
</aura:if>
<aura:if isTrue="{! and(v.arrowDirection != 'arrowdown', v.selectedTabsoft == 'AccountNumber') }">
<div class="slds-icon_container">
<lightning:icon iconName="utility:arrowup" size="xx-small" alternativeText="sort arraw up"/>
</div>
</aura:if>
</a>
</th>
<th class="slds-is-sortable slds-text-title--caps" scope="col" onclick="{!c.sortActive}">
<a href="javascript:void(0);" class="slds-th__action slds-text-link--reset">
<span class="slds-assistive-text">Sort</span>
<span class="slds-truncate" title="Active">Active</span>
<aura:if isTrue="{! and(v.arrowDirection == 'arrowdown', v.selectedTabsoft == 'Active__c') }">
<div class="slds-icon_container">
<lightning:icon iconName="utility:arrowdown" size="xx-small" alternativeText="sort arraw down"/>
</div>
</aura:if>
<aura:if isTrue="{! and(v.arrowDirection != 'arrowdown', v.selectedTabsoft == 'Active__c') }">
<div class="slds-icon_container">
<lightning:icon iconName="utility:arrowup" size="xx-small" alternativeText="sort arraw up"/>
</div>
</aura:if>
</a>
</th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.Accounts}" var="account">
<tr class="slds-hint-parent">
<td><a href="{! '#/sObject/' + account.Id + '/view'}">{!account.Name}</a></td>
<td data-label="Account Number" >{!account.AccountNumber}</td>
<td data-label="Account Source" >{!account.AccountSource}</td>
<td data-label="Active">{!account.Active__c}</td>
</tr>
</aura:iteration>
</tbody>
</table>
</div>
<div class="slds-docked-form-footer">
<div>
<ui:inputSelect aura:id="recordSize" label="Display Record Per Page:" class="testDiv" change="{!c.onSelectChange}">
<ui:inputSelectOption text="10" label="10" value="true"/>
<ui:inputSelectOption text="15" label="15"/>
<ui:inputSelectOption text="20" label="20"/>
</ui:inputSelect>
</div>
<lightning:button disabled="{!v.page == 1}" onclick="{!c.gotoFirstPage}" class="slds-button slds-button--neutral" label="First"/>
<lightning:button disabled="{!v.page == 1}" onclick="{!c.previousPage}" class="slds-button slds-button--neutral" label="Previous"/>
<lightning:button disabled="{!v.page == v.pages}" aura:id="nextbtn" class="slds-button slds-button--neutral" onclick="{!c.nextPage}" label="Next"/>
<lightning:button disabled="{!v.page == v.pages}" onclick="{!c.gotoLastPage}" class="slds-button slds-button--neutral" label="Last"/>
</div>
</form>
</aura:component>
Clint Side Controller :
({
doInit: function(component, event, helper) {
var page = component.get("v.page") || 1;
var recordToDisply = component.find("recordSize").get("v.value");
helper.getAccounts(component, page, recordToDisply, component.get('v.selectedTabsoft'), component.get("v.isAsc"));
},
previousPage: function(component, event, helper) {
var recordToDisply = component.find("recordSize").get("v.value");
component.set("v.page", component.get("v.page") - 1);
helper.getAccounts(component, component.get("v.page"), recordToDisply,component.get('v.selectedTabsoft'), component.get("v.isAsc"));
},
gotoFirstPage : function(component, event, helper) {
component.set("v.page",1);
var recordToDisply = component.find("recordSize").get("v.value");
helper.getAccounts(component, 1, recordToDisply,component.get('v.selectedTabsoft'), component.get("v.isAsc"));
},
gotoLastPage : function(component, event, helper) {
component.set("v.page",component.get("v.pages"));
var recordToDisply = component.find("recordSize").get("v.value");
helper.getAccounts(component, component.get("v.pages"), recordToDisply,component.get('v.selectedTabsoft'), component.get("v.isAsc"));
},
nextPage: function(component, event, helper) {
var recordToDisply = component.find("recordSize").get("v.value");
component.set("v.page", component.get("v.page") + 1);
helper.getAccounts(component, component.get("v.page"), recordToDisply,component.get('v.selectedTabsoft'), component.get("v.isAsc"));
},
onSelectChange: function(component, event, helper) {
var page = 1
var recordToDisply = component.find("recordSize").get("v.value");
helper.getAccounts(component, page, recordToDisply,component.get('v.selectedTabsoft'), component.get("v.isAsc"));
},
sortAccountName: function(component, event, helper) {
component.set("v.selectedTabsoft", 'Name');
helper.sortHelper(component, event, 'Name');
},
sortAccountSource: function(component, event, helper) {
component.set("v.selectedTabsoft", 'AccountSource');
helper.sortHelper(component, event, 'AccountSource');
},
sortAccountNumber: function(component, event, helper) {
component.set("v.selectedTabsoft", 'AccountNumber');
helper.sortHelper(component, event, 'AccountNumber');
},
sortActive: function(component, event, helper) {
component.set("v.selectedTabsoft", 'Active__c');
helper.sortHelper(component, event, 'Active__c');
}
})
Helper:
({
getAccounts: function(component, page, recordToDisply, sortField, isAsc) {
var action = component.get("c.fetchAccount");
action.setParams({
"pageNumber": page,
"recordToDisply": recordToDisply,
"sortField": sortField,
"isAsc":isAsc
});
action.setCallback(this, function(a) {
var result = a.getReturnValue();
console.log('result ---->' + JSON.stringify(result));
component.set("v.Accounts", result.accounts);
component.set("v.page", result.page);
component.set("v.total", result.total);
component.set("v.pages", Math.ceil(result.total / recordToDisply));
});
$A.enqueueAction(action);
},
sortHelper: function(component, event, sortFieldName) {
var currentDir = component.get("v.arrowDirection");
if (currentDir == 'arrowdown') {
component.set("v.arrowDirection", 'arrowup');
component.set("v.isAsc", true);
} else {
component.set("v.arrowDirection", 'arrowdown');
component.set("v.isAsc", false);
}
this.getAccounts(component, component.get("v.page"), component.find("recordSize").get("v.value"), sortFieldName, component.get("v.isAsc"));
},
})
Style:
.THIS .testDiv {
width :17%;
}
Server Side Controller:
public class AccountServersidesorting {
@AuraEnabled
public static Id getDispRecTypeId() {
return [SELECT Id FROM RecordType WHERE Name = 'Dispensary'].Id;
}
@AuraEnabled
public static string getProfileName() {
return [SELECT Name FROM Profile WHERE Id =: userinfo.getProfileId() LIMIT 1].Name;
}
@AuraEnabled
public static AccountPagerWrapper fetchAccount(Decimal pageNumber ,Integer recordToDisply, String sortField, boolean isAsc) {
Integer pageSize = recordToDisply;
Integer offset = ((Integer)pageNumber - 1) * pageSize;
AccountPagerWrapper obj = new AccountPagerWrapper();
obj.pageSize = pageSize;
obj.page = (Integer) pageNumber;
obj.total = [SELECT count() FROM account];
String query = 'SELECT Id, Name,AccountNumber,Active__c,AccountSource FROM Account ORDER BY '+sortField+ (isAsc?' ASC':' DESC')+' LIMIT '+recordToDisply+' OFFSET '+offset;
system.debug('query ::: ' + query);
obj.accounts = Database.Query(query);
return obj;
}
public class AccountPagerWrapper {
@AuraEnabled public Integer pageSize {get;set;}
@AuraEnabled public Integer page {get;set;}
@AuraEnabled public Integer total {get;set;}
@AuraEnabled public List<Account> accounts {get;set;}
}
Result :
Comments
Post a Comment