Find your content:

Search form

You are here

How to use nested repeat function in visualforce page

 
Share

Im trying to display the below format using a visualforce page.

Particulars    Receipts     Date           Amount Received  Amount paid

OnBooking                   29-Oct-2012    12000            0
               rec1         12-Oct-2012    0                23000
Agreement                   29-Oct-2012    13000            0
               rec2         13-oct-2012    0                1200   

Particulars belongs to one object and receipts belongs to another object. Amount received is the field in particulars, and amount paid is the field in recipts. Currently thse two objects are related to opportunity. Im trying to use repeat functions inside repeat.

<table width="100%" border="1" cellspacing="0">
    <tr>
        <td colspan="11" align="center" style="font-size:15px; font-weight:bold;">SCHEDULES</td>
    </tr>
    <tr style="font-size:14px; font-weight:bold;">
        <td> Particulars </td>
        <td> Receipt No</td>
        <td> Due Date </td>
        <td> Amount Due </td>
        <td> Amount Received </td>
    </tr>         
    <apex:repeat value="{!psList}"  var="ps">
        <tr style="font-size:14px;">
            <td>{!ps.Name}</td>
            <td></td>
        </tr>
        <apex:repeat value="{!rcList}" var="rc">
            <tr style="font-size:14px;">
                <td></td>
                <td><apex:outputfield value="{!rc.Name}" rendered="{!rc.Payment_Towards__c ='On Booking'} || {!ps.Name=='On Booking'}"/>
                 <!-- <apex:outputfield value="{!rc.Name}" rendered="{!rc.Payment_Towards__c ='Agreement Signing'}"/>
                 <apex:outputfield value="{!rc.Name}" rendered="{!rc.Payment_Towards__c ='Excavation'}"/>
            </td>
            <td>
                <apex:outputtext value="{0,date,dd-MMM-yyyy}">
                    <apex:param value="{!ps.Due_Date__c}"/>
                </apex:outputtext>      
            </td>    
            <td>  
                <apex:outputtext value="{0,number,###,###,###,##0.00}">     
                    <apex:param value="{!ps.Total__c}"/> 
                </apex:outputtext> 
            </td>
              <td>  
                <apex:outputtext value="{0,number,###,###,###,##0.00}">     
                    <apex:param value="{!ps.Payment_Received__c}"/> 
                </apex:outputtext> 
            </td>
        </tr> 
    </apex:repeat>     
</table>

How can I generate this visualforce page, can I use repeat inside repeat? The values for the second list are always the same.


Attribution to: user958915

Possible Suggestion/Solution #1

Yes, you can use a repeat inside a repeat.


Attribution to: Ralph Callaway

Possible Suggestion/Solution #2

Your problem is that you want to interleave two lists, or list the children for each parent, but your inner repeat is always binding to the same list variable in the controller, it's not changed for each parent record.

The cleanest solution here would be to use a wrapper class in your controller like so (note that I'm guessing you have 1 Particular to Many Receipts, and guessing at the object names).

public class TableEntry
{
    public Particular__c particular {get; set;}
    public list<Receipt__c> receiptsList {get; set;}

    public TableEntry(Particular__c p)
    {
        particular = p;
        receiptsList = new list<Receipt__c>();
    }
}

You build up a list of these entries in your controller code:

// declaration
public list<TableEntry> particularList {get; set};

** SNIP **

// inside some method where you have the data (pseudo code!)
for(Particular__c p : particularQueryResults)
{
    TableEntry e = new TableEntry(p);

    for(Receipt__c r : mapParticularToResults.get(p.Id))
    {
        e.receiptsList.add(r);
    }
}

Then finally, in your page you can use nested <apex:repeat> tags, one which loops through the list of TableEntry objects, and one that loops through the Receipts in each:

<apex:repeat value="{!particularList}"  var="ps">

    <!-- Reference the particular member when you need the fields for that -->
    <tr style="font-size:14px;">
        <td>{!ps.particular.Name}</td>
        <td></td>
    </tr>

    <!-- Now reference the receiptsList for the current Table Entry -->
    <apex:repeat value="{!ps.receiptsList}" var="rc">
        <tr style="font-size:14px;">
            <td></td>
            <td><apex:outputfield value="{!rc.Name}" rendered="{!rc.Payment_Towards__c ='On Booking'} || {!ps.Name=='On Booking'}"/>
            <!-- etc. -->

You haven't posted your controller code, so a lot of this is stabs in the dark, but it demonstrates a method in which you can achieve what you want. Of course, if there's only one receipt per particular then you won't need a list inside the class or the nested repeat, you'd just use {!ps.receipt.<<fieldname>>}.


Attribution to: Matt Lacey
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3836

My Block Status

My Block Content