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
Dave BerenatoDave Berenato 

Apex Repeat to Show A List of Tables

I want to create a Visualforce page that displays a set of repeating tables (2 cells x 2 cells each).

How do you reference the Controller in the Apex Page to repeat so that each table shows one record?

Apex Class:
public class Repeat1 {
	public List<pba_Listing__c> ListingFields {get;set;}
	
	public GetData() {
        [Select Name, pba_Status__c From pba_Listing__c WHERE pba_Status__c = 'Active' or pba_Status__c = 'Hold Short Sale' or pba_Status__c = 'Hold Standard Sale'];
	}
    
}
Visualforce page:
 
<apex:page Controller="Repeat1" recordSetVar="a1">
    <apex:pageBlock title="Listing">
        <apex:repeat>
        	<apex:pageBlockTable value="{!a1}" var="a">
        		<table>
            		<tr>
            			<th>Name</th>
            			<th><apex:outputField value="{!a.Name}"/></th>
            		</tr>
            		<tr>
             			<td>Status</td>
                		<td><apex:outputField value="{!a.pba_Status__c}"/></td>
            		</tr>
		        </table>
        	</apex:pageBlockTable>
        </apex:repeat>
    </apex:pageBlock>
</apex:page>


 
Best Answer chosen by Dave Berenato
Nithesh NNithesh N
Hey Dave Try this, 
 
<apex:page Controller="ListViewControl">  
    <html>
        <head>
            <style>
                
            </style>
        </head>
        <body>                       
            <apex:repeat value="{!records}" var="record">
                <table border = "1" cellpadding = "5" cellspacing = "5">
                    <tr>
                        <th>Name</th>
                        <th>Level</th> 
                    </tr>
                    <tr>
                        <td>{! record.Name }</td>
                        <td>{! record.Level__c }</td> 
                    </tr>
                </table>
                <br/>
            </apex:repeat>
        </body>
    </html>
    </apex:page>

Controller:
public class ListViewControl {
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(                   [Select Name, pba_Status__c From pba_Listing__c WHERE pba_Status__c = 'Active' or pba_Status__c = 'Hold Short Sale' or pba_Status__c = 'Hold Standard Sale']));
            }
            return setCon;
        }
        set;
    }
    
    // Initialize setCon and return a list of records
    public List<pba_Listing__c> getRecords() {
        return (List<pba_Listing__c>) setCon.getRecords();
    }
}


You need to apply thestyling to the html table as you need though. Hope it Helps.

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.

Best,
Nithesh

All Answers

Nithesh NNithesh N
Hey Dave Try this, 
 
<apex:page Controller="ListViewControl">  
    <html>
        <head>
            <style>
                
            </style>
        </head>
        <body>                       
            <apex:repeat value="{!records}" var="record">
                <table border = "1" cellpadding = "5" cellspacing = "5">
                    <tr>
                        <th>Name</th>
                        <th>Level</th> 
                    </tr>
                    <tr>
                        <td>{! record.Name }</td>
                        <td>{! record.Level__c }</td> 
                    </tr>
                </table>
                <br/>
            </apex:repeat>
        </body>
    </html>
    </apex:page>

Controller:
public class ListViewControl {
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(                   [Select Name, pba_Status__c From pba_Listing__c WHERE pba_Status__c = 'Active' or pba_Status__c = 'Hold Short Sale' or pba_Status__c = 'Hold Standard Sale']));
            }
            return setCon;
        }
        set;
    }
    
    // Initialize setCon and return a list of records
    public List<pba_Listing__c> getRecords() {
        return (List<pba_Listing__c>) setCon.getRecords();
    }
}


You need to apply thestyling to the html table as you need though. Hope it Helps.

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.

Best,
Nithesh
This was selected as the best answer
Dave BerenatoDave Berenato
So helpful. Thank you!
Dave BerenatoDave Berenato
Hi Nithesh,

I have a follow up question for this repeat table.

I moved the repeat table to an Apex PageBlockSection, and I'm getting an issues with the <apex: inputfield> function. It's taking up multiple cells in the table and throwing off the code entirely.

So imagine the original code but with editable fields instead of just Read Only fields:

But the entire table gets thrown off because Apex: input field and it no longer makes a 2 x 2 cell table.

Any ideas?
 
<apex:page Controller="ListViewControl">  
<apex:form>
<apex:pageBlock>
    <html>
        <body>
      <apex:pageBlockSection columns="1">                       
            <apex:repeat value="{!records}" var="record">
                <table border = "1" cellpadding = "5" cellspacing = "5">
                    <tr>
                        <th>Name</th>
                        <th>Level</th> 
                    </tr>
                    <tr>
                        <td><apex:inputfield value="{! record.Name }"/></td>
                        <td><apex:inputfield value="{! record.Level }"/></td> 
                    </tr>
                </table>
            </apex:repeat>
          </apex:pageBlockSection>
        </body>
      </html>
</apex:pageBlock>
</apex:form>
</apex:page>



 
Nithesh NNithesh N
Try this, And let me know if this is what you want...
 
<apex:page Controller="ListViewControl">  
    <html>
        <head>
            <style>
                
            </style>
        </head>
        <body>
            <apex:pageBlock>
                <apex:pageBlockSection columns="1">  
                    <apex:form>
                        <apex:repeat value="{!records}" var="record">
                            <table border = "1" cellpadding = "5" cellspacing = "5">
                                <tr>
                                    <th>Name</th>
                                    <th>Level</th> 
                                </tr>
                                <tr>
                                    <td><apex:inputfield value="{! record.Name }"/></td>
                                    <td><apex:inputfield value="{! record.Level__c }"/></td> 
                                </tr>
                            </table>
                            <br/>
                        </apex:repeat>
                    </apex:form>
                </apex:pageBlockSection>
            </apex:pageBlock>
        </body>
    </html>
</apex:page>

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.

Best,
Nithesh
Dave BerenatoDave Berenato
Thank you, Nithesh!
Prince PranavPrince Pranav
Thank You, Nitesh N. It helped me alot. Thanks Alot..!