+ Start a Discussion
Michael3.BrownMichael3.Brown 

I need Help Using <Apex:Repeat> !!

 

Hello, I have a VF page where I want to use two groups of <Apex:Repeat> tags. One will call from a custom object, while the other will call from the Account object.  I want to display a couple of fields from each object, on the same row, but I am having trouble. Ideally, I want my  table to look like the following

 

Car Type      Car Make      Car Model      Account

Truck             Ford                F150               AAA

Car                Lotus              Elise               BBB

SUV               Toyota            Rav4               CCC

 

However, I am ending up with the following display instead:

 

Car Type      Car Make      Car Model      Account

Truck             Ford                F150              

Car                Lotus              Elise              

SUV               Toyota            Rav4

AAA

BBB

CCC              

 

Here's the code I have for this table.

  <TABLE border="1" cellpadding="2" cellspacing="2">
  <TH width="75">Category</TH>
  <TH width="100">Account</TH>
  <TH width="40">Engine Project</TH>
  <TH width="75">Engine Model</TH>
  <TH width="75">Region</TH>
  
 
  <apex:repeat value="{!opportunities}" var="a">       
       <TR><TD width="75"><apex:outputText value="{!a.Type__c}" /></TD>
       <TD width="100"><apex:outputField value="{!a.Make__c}" /></TD>
       <TD width="40">{!a.Model__c}</TD></TR>
  </apex:repeat>
  <apex:repeat value="{!accounts}" var="b">
      <TR><TD width="75"><apex:outputField value="{!b.Account__c}" /></TD></TR>
  </apex:repeat>
  </TABLE>

 

The <TR> tags seems to be where my problem lies. I tried having only 1 group of <TR> tags. I originally placed <TR> following the first <apex:repeat> and </TR> right before the last </apex:repeat>. However, I got an error saying that I needed to have a matching <TR> tags.

 

There's got to be a way to get my Account names to line up on the records as they should. I would appreciate any insight!

 

Thanks,

Mike

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

You're looking to do a composite between Account and your custom object, unfortunately, you're not going to be able to achieve it using the approach you've adopted.

 

But it shouldn't be too hard to do  - what you need is a wrapper class (http://wiki.developerforce.com/index.php/Wrapper_Class)


Declare a class which holds an instance of an Account as well as your custom object

 

Return a list of this wrapper class to the page and render that in your repeat tag.

 

eg.

public class AccountCustomWrapper{

public Account acc;

public Custom__c cust;

}

 

in your controller

 

public List<AccountCustomWrapper> getWrappers(){

 

//construct a list of wrappers, setting Account and Custom Objects, return a list

}

 

In your VF Page then, just one apex:repeat over this list

<apex:repeat value="{!wrappers}" var="wrap">

<tr>

<td> <apex:outputText value="{!wrap.cust.Type__c}" /> </td>

 

</tr>

</apex:repeat>

 

 

All Answers

Ritesh AswaneyRitesh Aswaney

You're looking to do a composite between Account and your custom object, unfortunately, you're not going to be able to achieve it using the approach you've adopted.

 

But it shouldn't be too hard to do  - what you need is a wrapper class (http://wiki.developerforce.com/index.php/Wrapper_Class)


Declare a class which holds an instance of an Account as well as your custom object

 

Return a list of this wrapper class to the page and render that in your repeat tag.

 

eg.

public class AccountCustomWrapper{

public Account acc;

public Custom__c cust;

}

 

in your controller

 

public List<AccountCustomWrapper> getWrappers(){

 

//construct a list of wrappers, setting Account and Custom Objects, return a list

}

 

In your VF Page then, just one apex:repeat over this list

<apex:repeat value="{!wrappers}" var="wrap">

<tr>

<td> <apex:outputText value="{!wrap.cust.Type__c}" /> </td>

 

</tr>

</apex:repeat>

 

 

This was selected as the best answer
Michael3.BrownMichael3.Brown

Interesting. I had no idea about these wrapper classes. Thanks Rijesh. I will see what I can do with these.

 

- Mike