Visualforce Page with Custom Controller and Pagination > Apex and Visualforce
Visualforce Page with Custom Controller and Pagination > Apex and Visualforce
Salesforce Development and Implementations
As I go on learning about development on and using the Salesforce Platform, I have decided to share my practice sessions / codes, hoping it could help someone in the future.
Here is the code for a Visualforce Page with a Custom Apex Controller, that displays Account and User Standard Objects information and also has Pagination implemented on the Account Object.
Code:
AccountUserView.vfp
<apex:page controller="AccountUserController" sidebar="false" showHeader="false" lightningStylesheets="true">
<apex:form >
<apex:pageBlock title="List of Accounts and Users from Custom Controller with Pagination on Accounts Object">
<apex:pageBlockSection title="Your Info ..">
<br />
<h1>
Current User : {! $User.FirstName } {! $User.LastName }
( {! $User.Username } )
</h1>
<br />
<p> Today's Date is {! TODAY() } </p>
<p> So Next week it will be {! TODAY() + 7 } </p>
<p>{! IF( CONTAINS('salesforce.com','force.com'),
'Yep', 'Nope') }</p>
<p>{! IF( DAY(TODAY()) < 15,
'Before the 15th', 'The 15th or after') }</p>
<br /><br />
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="<<" action="{!beginning}" disabled="{!DisablePrevious}"/>
<apex:commandButton value="<" action="{!previous}" disabled="{!DisablePrevious}"/>
<apex:commandButton value=">" action="{!next}" disabled="{!DisableNext}"/>
<apex:commandButton value=">>" action="{!last}" disabled="{!DisableNext}"/>
<apex:commandButton action="{!updateUsrs}" value="Users" title="Users"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Accounts and Users" columns="2">
<apex:pageBlockTable title="Account Info" value="{!Accs}" var="a">
<apex:column value="{!a.Name}"/>
</apex:pageBlockTable>
<apex:pageBlockTable title="User Info" value="{!Usrs}" var="u">
<apex:column value="{!u.Name}"/>
<apex:column value="{!u.Email}"/>
<apex:column value="{!u.UserType}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
AccountUserController.apxc
public class AccountUserController { private List<Account> accs = new List<Account>(); private List<User> usrs; //limited display list public List<Account> accstoshow{get;set;} //Navigation Variables Integer counter = 0; // To track number of records parsed Integer limitSize = 20; // Number of records to be displayed Integer totalSize = 0; // To store the total number of records available public AccountUserController(){ accstoshow = new List<Account>(); accs = [select ID, Name from Account ORDER BY Name LIMIT 999]; totalSize = accs.size(); if((counter+limitSize) <= totalSize){ for(Integer i = 0; i<limitSize; i++) accstoshow.add(accs.get(i)); } else{ for(Integer i=0;i<totalSize;i++) accstoshow.add(accs.get(i)); } } //Navigation methods public void beginning(){ accstoshow.clear(); counter=0; if((counter + limitSize) <= totalSize){ for(Integer i=0;i<limitSize;i++){ accstoshow.add(accs.get(i)); } } else{ for(Integer i=0;i<totalSize;i++){ accstoshow.add(accs.get(i)); } } } public void next(){ accstoshow.clear(); counter=counter+limitSize; if((counter+limitSize) <= totalSize){ for(Integer i=counter-1;i<(counter+limitSize);i++){ accstoshow.add(accs.get(i)); } } else{ for(Integer i=counter;i<totalSize;i++){ accstoshow.add(accs.get(i)); } } } public void previous(){ accstoshow.clear(); counter=counter-limitSize; for(Integer i=counter;i<(counter+limitSize); i++){ accstoshow.add(accs.get(i)); } } public void last (){ accstoshow.clear(); if(math.mod(totalSize , limitSize) == 0){ counter = limitSize * ((totalSize/limitSize)-1); } else if (math.mod(totalSize , limitSize) != 0){ counter = limitSize * ((totalSize/limitSize)); } for(Integer i=counter-1;i<totalSize-1;i++){ accstoshow.add(accs.get(i)); } } public Boolean getDisableNext(){ if((counter + limitSize) >= totalSize ) return true ; else return false ; } public Boolean getDisablePrevious(){ if(counter == 0) return true ; else return false ; } public List <Account> getAccs(){ return accstoshow; } public List <User> getUsrs(){ return usrs; } public pageReference updateUsrs(){ usrs = [select Id, Username, Firstname, Lastname, Name, Email, UserType from User]; return null; } }
This comment has been removed by the author.
ReplyDelete