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
Chris CalhounChris Calhoun 

Six pageblocktables on one Visualforce page?

I need to place six pageblocktables on one Visualforce page. I'm not finding any examples on how the controller would work. I need to execute six different queries in the controller OR one SOQL query and maybe filter each pageblocktable with only the data it is supposed to display.

Please point me to any examples of how to return mutilple datasets from a controller to one Visualforce page with six pageblocktables.
Best Answer chosen by Chris Calhoun
kaustav goswamikaustav goswami
Please note that there are multiple considerations and that depends on your requirement. To have a proper solution you need to consider the volume of data that will be handled, the view state size, the pagination requirements(if any) etc.

However, to give you a simple example I have written the following sample code:

Controller:

public class sixPageBlockTables{
	// declare the six lists that will be displayed in your six page block tables
	public List<Account> table1 {get; set;}
	public List<Account> table2 {get; set;}
	public List<Account> table3 {get; set;}
	public List<Account> table4 {get; set;}
	public List<Account> table5 {get; set;}
	public List<Account> table6 {get; set;}
	
	// declare the master list which will hold all the data from the SOQL
	public List<Account> masterList {get; set;}
	
	// constructor
	public sixPageBlockTables(){
		// instantiate all the declared variables
		table1 = new List<Account>();
		table2 = new List<Account>();
		table3 = new List<Account>();
		table4 = new List<Account>();
		table5 = new List<Account>();
		table6 = new List<Account>();
		
		masterList = new List<Account>();
		
		fetchData();
		populateLists();
	}
	
	// this method fetches all the required data from database
	// please note that the volume of data being handled is important too many records will result ina  view state error
	// to handle that you might have to take help of setController to paginate the data
	public void fetchData(){
		masterList = [SELECT Id, Name, OwnerId, Industry FROM Account LIMIT 100];
	}
	
	// this method populates the six lists based on the different conditions
	// this method in the actual class will have the logic based on which the lists will be populated
	public void populateLists(){
		table1.add(masterList[0]);
		table2.add(masterList[1]);
		table3.add(masterList[2]);
		table4.add(masterList[3]);
		table5.add(masterList[4]);
		table6.add(masterList[5]);
	}
}



Page:
<apexpage id="sixTables" title="Six Page Block Tables" controller="sixPageBlockTables">
	<apex:form id="sixTableForm">
		<apex:pageBlock id="firstPB" title="First Block">
			<apex:pageBlockTable id="firstTable" value="{!table1}" var="acc">
				<apex:column value="{!acc.Name}" />
				<apex:column value="{!acc.OwnerId}" />
				<apex:column value="{!acc.Industry}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
		<apex:pageBlock id="secPB" title="Second Block">
			<apex:pageBlockTable id="secTable" value="{!table2}" var="acc">
				<apex:column value="{!acc.Name}" />
				<apex:column value="{!acc.OwnerId}" />
				<apex:column value="{!acc.Industry}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
		<apex:pageBlock id="thirdPB" title="Third Block">
			<apex:pageBlockTable id="thirdTable" value="{!table3}" var="acc">
				<apex:column value="{!acc.Name}" />
				<apex:column value="{!acc.OwnerId}" />
				<apex:column value="{!acc.Industry}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
		<apex:pageBlock id="fourthPB" title="Fourth Block">
			<apex:pageBlockTable id="fourthTable" value="{!table4}" var="acc">
				<apex:column value="{!acc.Name}" />
				<apex:column value="{!acc.OwnerId}" />
				<apex:column value="{!acc.Industry}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
		<apex:pageBlock id="fifthPB" title="Fifth Block">
			<apex:pageBlockTable id="fifthTable" value="{!table5}" var="acc">
				<apex:column value="{!acc.Name}" />
				<apex:column value="{!acc.OwnerId}" />
				<apex:column value="{!acc.Industry}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
		<apex:pageBlock id="sixthPB" title="Sixth Block">
			<apex:pageBlockTable id="sixthTable" value="{!table6}" var="acc">
				<apex:column value="{!acc.Name}" />
				<apex:column value="{!acc.OwnerId}" />
				<apex:column value="{!acc.Industry}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
	</apex:form>
</apex:page>

Please let  me know if you need further help on this.

Thanks,
Kaustav

All Answers

kaustav goswamikaustav goswami
Please note that there are multiple considerations and that depends on your requirement. To have a proper solution you need to consider the volume of data that will be handled, the view state size, the pagination requirements(if any) etc.

However, to give you a simple example I have written the following sample code:

Controller:

public class sixPageBlockTables{
	// declare the six lists that will be displayed in your six page block tables
	public List<Account> table1 {get; set;}
	public List<Account> table2 {get; set;}
	public List<Account> table3 {get; set;}
	public List<Account> table4 {get; set;}
	public List<Account> table5 {get; set;}
	public List<Account> table6 {get; set;}
	
	// declare the master list which will hold all the data from the SOQL
	public List<Account> masterList {get; set;}
	
	// constructor
	public sixPageBlockTables(){
		// instantiate all the declared variables
		table1 = new List<Account>();
		table2 = new List<Account>();
		table3 = new List<Account>();
		table4 = new List<Account>();
		table5 = new List<Account>();
		table6 = new List<Account>();
		
		masterList = new List<Account>();
		
		fetchData();
		populateLists();
	}
	
	// this method fetches all the required data from database
	// please note that the volume of data being handled is important too many records will result ina  view state error
	// to handle that you might have to take help of setController to paginate the data
	public void fetchData(){
		masterList = [SELECT Id, Name, OwnerId, Industry FROM Account LIMIT 100];
	}
	
	// this method populates the six lists based on the different conditions
	// this method in the actual class will have the logic based on which the lists will be populated
	public void populateLists(){
		table1.add(masterList[0]);
		table2.add(masterList[1]);
		table3.add(masterList[2]);
		table4.add(masterList[3]);
		table5.add(masterList[4]);
		table6.add(masterList[5]);
	}
}



Page:
<apexpage id="sixTables" title="Six Page Block Tables" controller="sixPageBlockTables">
	<apex:form id="sixTableForm">
		<apex:pageBlock id="firstPB" title="First Block">
			<apex:pageBlockTable id="firstTable" value="{!table1}" var="acc">
				<apex:column value="{!acc.Name}" />
				<apex:column value="{!acc.OwnerId}" />
				<apex:column value="{!acc.Industry}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
		<apex:pageBlock id="secPB" title="Second Block">
			<apex:pageBlockTable id="secTable" value="{!table2}" var="acc">
				<apex:column value="{!acc.Name}" />
				<apex:column value="{!acc.OwnerId}" />
				<apex:column value="{!acc.Industry}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
		<apex:pageBlock id="thirdPB" title="Third Block">
			<apex:pageBlockTable id="thirdTable" value="{!table3}" var="acc">
				<apex:column value="{!acc.Name}" />
				<apex:column value="{!acc.OwnerId}" />
				<apex:column value="{!acc.Industry}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
		<apex:pageBlock id="fourthPB" title="Fourth Block">
			<apex:pageBlockTable id="fourthTable" value="{!table4}" var="acc">
				<apex:column value="{!acc.Name}" />
				<apex:column value="{!acc.OwnerId}" />
				<apex:column value="{!acc.Industry}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
		<apex:pageBlock id="fifthPB" title="Fifth Block">
			<apex:pageBlockTable id="fifthTable" value="{!table5}" var="acc">
				<apex:column value="{!acc.Name}" />
				<apex:column value="{!acc.OwnerId}" />
				<apex:column value="{!acc.Industry}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
		<apex:pageBlock id="sixthPB" title="Sixth Block">
			<apex:pageBlockTable id="sixthTable" value="{!table6}" var="acc">
				<apex:column value="{!acc.Name}" />
				<apex:column value="{!acc.OwnerId}" />
				<apex:column value="{!acc.Industry}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
	</apex:form>
</apex:page>

Please let  me know if you need further help on this.

Thanks,
Kaustav

This was selected as the best answer
Chris CalhounChris Calhoun
This worked!!!! Thank you very much!!  And best of all, I understand it now.

Chris
kaustav goswamikaustav goswami
Good to know that it helped. You have mistakenly marked your own reply as the best answer.

Thanks,
Kaustav