You need to sign in to do that
Don't have an account?
Roopa S 1
im displaying account and related contacts using aura component...
i need to display 10 records in one page and i should display "PREVIOUS" and "NEXT" buttons to display remaining records. plz help me
Controller class
public class AccRelatedConC{
@AuraEnabled
public static List<Account> fetchAcc (){
return [SELECT Id, Name, Phone FROM Account];
}
@AuraEnabled
public static List<Contact> fetchCon (String recordId){
return [SELECT Id, Name, Phone FROM Contact WHERE AccountId=:recordId];
}
}
Component
<aura:component controller="AccRelatedConC"
implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="PageHeading" type="String" default="Account and Related Contacts" />
<aura:attribute name="accData" type="List"/>
<aura:attribute name="conData" type="List"/>
<aura:attribute name="show" type="boolean" default="false"/>
<aura:handler name="init" value="{!this}" action="{!c.doinit}" />
<div class=" slds-page-header slds-text-heading--large slds-align--absolute-center">
{!v.PageHeading}
</div>
<div class="slds-section slds-is-open">
<h3 class="slds-section__title slds-theme_shade">
<span class="slds-truncate slds-p-horizontal_small" title="Section Title">Accounts</span>
</h3>
<br/>
<table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout">
<thead>
<tr class="slds-text-heading--label">
<th scope="col"><div class="slds-truncate" title="Account Name">Account Name</div></th>
<th scope="col"><div class="slds-truncate" title="Phone">Phone</div></th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.accData}" var="row" indexVar="index">
<tr>
<th scope="row">
<div class="slds-truncate" >
<a onclick="{!c.showCon}" value="{!row}" data-index="{!index}">{!row.Name} </a>
</div>
</th>
<td><div class="slds-truncate" title="{!row.Phone}">{!row.Phone}</div></td>
</tr>
</aura:iteration>
</tbody>
</table>
</div>
<br/>
<aura:if isTrue="{!v.show}">
<div class="slds-section slds-is-open">
<h3 class="slds-section__title slds-theme_shade">
<span class="slds-truncate slds-p-horizontal_small" title="Section Title">Related Contact Details</span>
</h3>
<br/>
<table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout">
<thead>
<tr class="slds-text-heading--label">
<th scope="col"><div class="slds-truncate" title="Contact Name">Contact Name</div></th>
<th scope="col"><div class="slds-truncate" title="Phone">Phone</div></th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.conData}" var="row2">
<tr>
<th scope="row"><div class="slds-truncate" title="{!row2.Name}">{!row2.Name}</div></th>
<td><div class="slds-truncate" title="{!row2.Phone}">{!row2.Phone}</div></td>
</tr>
</aura:iteration>
</tbody>
</table>
<div class="slds-clearfix">
<div class="slds-page-header" role="banner">
<div class="slds-float_right">
<lightning:button disabled="{!v.PageNumber == 1}" variant="brand" aura:id="prevPage" label="Prev" onclick="{!c.handlePrev}" />
<lightning:button disabled="{!v.PageNumber == v.TotalPages}" aura:id="nextPage" variant="brand" label="Next" onclick="{!c.handleNext}"/>
</div>
<p class="slds-page-header__title">{!v.RecordStart}-{!v.RecordEnd} of {!v.TotalRecords} | Page {!v.PageNumber} of {!v.TotalPages}</p>
</div>
</div>
</div>
</aura:if>
</aura:component>
controller
({
doinit : function(component, event, helper) {
var action = component.get('c.fetchAcc');
action.setCallback(this, function(response){
var state = response.getState();
if(state === "SUCCESS"){
var allValues = response.getReturnValue();
console.log("allValues--->>> " + allValues);
component.set('v.accData', allValues);
}
else if(state === "ERROR") {
var errors = response.getError();
if(errors){
if(errors[0] && errors[0].message){
console.log("Error Message: " + errors[0].message);
}
}
else{
console.log("Unknown Error");
}
}
});
$A.enqueueAction(action);
},
handleNext: function(component, event, helper) {
var pageNumber = component.get("v.PageNumber");
var pageSize = component.find("pageSize").get("v.value");
pageNumber++;
helper.getContactList(component, pageNumber, pageSize);
},
handlePrev: function(component, event, helper) {
var pageNumber = component.get("v.PageNumber");
var pageSize = component.find("pageSize").get("v.value");
pageNumber--;
helper.getContactList(component, pageNumber, pageSize);
},
onSelectChange: function(component, event, helper) {
var page = 1
var pageSize = component.find("pageSize").get("v.value");
helper.getContactList(component, page, pageSize);
},
showCon : function(component, event, helper){
component.set("v.show",true);
var idx = event.target.getAttribute('data-index');
console.log('idx---->>> ' + idx);
var rowRecord = component.get("v.accData")[idx];
console.log('rowRecord---->>> ' + JSON.stringify(rowRecord));
var action = component.get('c.fetchCon');
action.setParams({recordId : rowRecord.Id});
action.setCallback(this, function(response){
var state = response.getState();
if(state === "SUCCESS"){
var allValues = response.getReturnValue();
console.log("allValues--->>> " + JSON.stringify(allValues));
component.set('v.conData', allValues);
}
else if(state === "ERROR") {
var errors = response.getError();
if(errors){
if(errors[0] && errors[0].message){
console.log("Error Message: " + errors[0].message);
}
}
else{
console.log("Unknown Error");
}
}
});
$A.enqueueAction(action);
}
})
helper
({
getContactList: function(component, pageNumber, pageSize) {
var action = component.get("c.getContactData");
action.setParams({
"pageNumber": pageNumber,
"pageSize": pageSize
});
action.setCallback(this, function(result) {
var state = result.getState();
if (component.isValid() && state === "SUCCESS"){
var resultData = result.getReturnValue();
component.set("v.ContactList", resultData.contactList);
component.set("v.PageNumber", resultData.pageNumber);
component.set("v.TotalRecords", resultData.totalRecords);
component.set("v.RecordStart", resultData.recordStart);
component.set("v.RecordEnd", resultData.recordEnd);
component.set("v.TotalPages", Math.ceil(resultData.totalRecords / pageSize));
}
});
$A.enqueueAction(action);
}
})
Controller class
public class AccRelatedConC{
@AuraEnabled
public static List<Account> fetchAcc (){
return [SELECT Id, Name, Phone FROM Account];
}
@AuraEnabled
public static List<Contact> fetchCon (String recordId){
return [SELECT Id, Name, Phone FROM Contact WHERE AccountId=:recordId];
}
}
Component
<aura:component controller="AccRelatedConC"
implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="PageHeading" type="String" default="Account and Related Contacts" />
<aura:attribute name="accData" type="List"/>
<aura:attribute name="conData" type="List"/>
<aura:attribute name="show" type="boolean" default="false"/>
<aura:handler name="init" value="{!this}" action="{!c.doinit}" />
<div class=" slds-page-header slds-text-heading--large slds-align--absolute-center">
{!v.PageHeading}
</div>
<div class="slds-section slds-is-open">
<h3 class="slds-section__title slds-theme_shade">
<span class="slds-truncate slds-p-horizontal_small" title="Section Title">Accounts</span>
</h3>
<br/>
<table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout">
<thead>
<tr class="slds-text-heading--label">
<th scope="col"><div class="slds-truncate" title="Account Name">Account Name</div></th>
<th scope="col"><div class="slds-truncate" title="Phone">Phone</div></th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.accData}" var="row" indexVar="index">
<tr>
<th scope="row">
<div class="slds-truncate" >
<a onclick="{!c.showCon}" value="{!row}" data-index="{!index}">{!row.Name} </a>
</div>
</th>
<td><div class="slds-truncate" title="{!row.Phone}">{!row.Phone}</div></td>
</tr>
</aura:iteration>
</tbody>
</table>
</div>
<br/>
<aura:if isTrue="{!v.show}">
<div class="slds-section slds-is-open">
<h3 class="slds-section__title slds-theme_shade">
<span class="slds-truncate slds-p-horizontal_small" title="Section Title">Related Contact Details</span>
</h3>
<br/>
<table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout">
<thead>
<tr class="slds-text-heading--label">
<th scope="col"><div class="slds-truncate" title="Contact Name">Contact Name</div></th>
<th scope="col"><div class="slds-truncate" title="Phone">Phone</div></th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.conData}" var="row2">
<tr>
<th scope="row"><div class="slds-truncate" title="{!row2.Name}">{!row2.Name}</div></th>
<td><div class="slds-truncate" title="{!row2.Phone}">{!row2.Phone}</div></td>
</tr>
</aura:iteration>
</tbody>
</table>
<div class="slds-clearfix">
<div class="slds-page-header" role="banner">
<div class="slds-float_right">
<lightning:button disabled="{!v.PageNumber == 1}" variant="brand" aura:id="prevPage" label="Prev" onclick="{!c.handlePrev}" />
<lightning:button disabled="{!v.PageNumber == v.TotalPages}" aura:id="nextPage" variant="brand" label="Next" onclick="{!c.handleNext}"/>
</div>
<p class="slds-page-header__title">{!v.RecordStart}-{!v.RecordEnd} of {!v.TotalRecords} | Page {!v.PageNumber} of {!v.TotalPages}</p>
</div>
</div>
</div>
</aura:if>
</aura:component>
controller
({
doinit : function(component, event, helper) {
var action = component.get('c.fetchAcc');
action.setCallback(this, function(response){
var state = response.getState();
if(state === "SUCCESS"){
var allValues = response.getReturnValue();
console.log("allValues--->>> " + allValues);
component.set('v.accData', allValues);
}
else if(state === "ERROR") {
var errors = response.getError();
if(errors){
if(errors[0] && errors[0].message){
console.log("Error Message: " + errors[0].message);
}
}
else{
console.log("Unknown Error");
}
}
});
$A.enqueueAction(action);
},
handleNext: function(component, event, helper) {
var pageNumber = component.get("v.PageNumber");
var pageSize = component.find("pageSize").get("v.value");
pageNumber++;
helper.getContactList(component, pageNumber, pageSize);
},
handlePrev: function(component, event, helper) {
var pageNumber = component.get("v.PageNumber");
var pageSize = component.find("pageSize").get("v.value");
pageNumber--;
helper.getContactList(component, pageNumber, pageSize);
},
onSelectChange: function(component, event, helper) {
var page = 1
var pageSize = component.find("pageSize").get("v.value");
helper.getContactList(component, page, pageSize);
},
showCon : function(component, event, helper){
component.set("v.show",true);
var idx = event.target.getAttribute('data-index');
console.log('idx---->>> ' + idx);
var rowRecord = component.get("v.accData")[idx];
console.log('rowRecord---->>> ' + JSON.stringify(rowRecord));
var action = component.get('c.fetchCon');
action.setParams({recordId : rowRecord.Id});
action.setCallback(this, function(response){
var state = response.getState();
if(state === "SUCCESS"){
var allValues = response.getReturnValue();
console.log("allValues--->>> " + JSON.stringify(allValues));
component.set('v.conData', allValues);
}
else if(state === "ERROR") {
var errors = response.getError();
if(errors){
if(errors[0] && errors[0].message){
console.log("Error Message: " + errors[0].message);
}
}
else{
console.log("Unknown Error");
}
}
});
$A.enqueueAction(action);
}
})
helper
({
getContactList: function(component, pageNumber, pageSize) {
var action = component.get("c.getContactData");
action.setParams({
"pageNumber": pageNumber,
"pageSize": pageSize
});
action.setCallback(this, function(result) {
var state = result.getState();
if (component.isValid() && state === "SUCCESS"){
var resultData = result.getReturnValue();
component.set("v.ContactList", resultData.contactList);
component.set("v.PageNumber", resultData.pageNumber);
component.set("v.TotalRecords", resultData.totalRecords);
component.set("v.RecordStart", resultData.recordStart);
component.set("v.RecordEnd", resultData.recordEnd);
component.set("v.TotalPages", Math.ceil(resultData.totalRecords / pageSize));
}
});
$A.enqueueAction(action);
}
})
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!
its displaying more than 10 records but it should display only 10 records per page.
and its not showing contacts related to particular account plz help me to solve that.
thank you
Click on Account And Scroll Down And You See A Section With Related Contacts As Shown In Image Below
Please Mark It As Best Answer If It Helps
Thank You!