function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdctrrsfdctrr 

How to Display huge number of records page wise?

Hi Folks,


I Have a requirement like i need to

 

1. Display 2,00,000 records page wise.


2.Each page should contain only 50 records.


3. User should have flexibility of clicking on Next,Previous,First and Last links/buttons to get other records on the page.

 

What is the best way to do that Any examples with code appreciated.

 

Thanks in advance.


Thanks.

vishal@forcevishal@force

You can use "first" and "rows" attributes in <apex:pageBlockTable> as well as <apex:repeat>

 

First is the start index of the record you want to display on your table, while rows will tell you the number of records you want on a single page.

 

Here is an example:

 

PAGE:

 

<apex:page controller="Pagination_Controller">
	<apex:form >
	<apex:pageBlock title="Pagination" id="pb">
		<apex:pageBlockTable id="tb" value="{!lstAccounts}" first="{!currentPage}" rows="{!pageSize}" var="a">
			<apex:column value="{!a.Name}"/>
			<apex:column value="{!a.Phone}"/>
		</apex:pageBlockTable>
		<center>
		<apex:commandButton value="First" action="{!showFirst}" rerender="pb" disabled="{!!showPrevious}"/>
		<apex:commandButton value="Previous" action="{!showPrevious}" rerender="pb" disabled="{!!showPrevious}"/>
		<apex:commandButton value="Next" action="{!showNext}" rerender="pb" disabled="{!!showNext}"/>
		<apex:commandButton value="Last" action="{!showLast}" rerender="pb" disabled="{!!showNext}"/>
		</center>
		<apex:outputText value="{!currentPageInfo}" />
	</apex:pageBlock> 
	</apex:form>
</apex:page>

 

Controller:

 

public with sharing class Pagination_Controller 
{
	public List<Account> lstAccounts	{get;set;}
	
	// for pagination
	public Integer pageSize	{get;set;}
	public Integer currentPage	{get;set;}
	public String currentPageInfo	{get;set;}
	
	// for disabling pagination links
	public Boolean showPrevious	{get;set;}
	public Boolean showNext	{get;set;}
	
	private Integer totalRecords;
	private Integer pageNumber;
	
	public Pagination_Controller()
	{
		lstAccounts = [Select Name, Phone From Account limit 23];
		currentPage = 0;
		pageSize = 3;
		pageNumber = 1;
		showPrevious = false;
		totalRecords = lstAccounts.size();
		if(totalRecords > pageSize)
			showNext = true;
		else
			showNext = false;
		currentPageInfo = 'Showing Page ' + pageNumber +   ' of ' + (Math.mod(totalRecords, pageSize) == 0 ? totalRecords/pageSize : (totalRecords/pageSize) + 1); 
	}
	
	public void showPrevious()
	{
		currentPage = currentPage - pageSize;
		pageNumber--;
		enableDisablePaginationLinks();		
	}
	
	public void showNext()
	{
		currentPage = currentPage + pageSize;
		pageNumber++;
		enableDisablePaginationLinks();
	}
	
	public void showFirst()
	{
		currentPage = 0;
		pageNumber = 1;
		enableDisablePaginationLinks();
	}
	
	public void showLast()
	{
		currentPage = (Math.mod(totalRecords, pageSize) == 0) ? totalRecords - pageSize : (totalRecords / pageSize) * pageSize;
		pageNumber = (Math.mod(totalRecords, pageSize) == 0 ? totalRecords/pageSize : (totalRecords/pageSize) + 1);
		enableDisablePaginationLinks();
	}
	
	private void enableDisablePaginationLinks()
	{
		if(currentPage == 0)
			showPrevious = false;
		else
			showPrevious = true;
		
		if((totalRecords - pageSize) <= currentPage)
			showNext = false;
		else
			showNext = true;
		
		currentPageInfo = 'Showing Page ' + pageNumber +   ' of ' + (Math.mod(totalRecords, pageSize) == 0 ? totalRecords/pageSize : (totalRecords/pageSize) + 1);
	}
}