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
svdsvd 

problem in accessing data using repeat tag

Hi All,

My requirement is to access the variable of repeat tag. My controller returns List < List<Trans__c> >. My repeat tag builds a pageblock section for each sublist. Data is being displayed fine, but I'm unable to access the repeat variable in the title of Pageblocksection.

here is sample data for more clarity on what I need.

     <apex:repeat value="{!Transcript}" var="subList" >             // My Controller returns List <List<Trans__c> > where Trans__c is my object.  
         
                <apex:pageBlockSection title=" ??? " columns="1" id="pbsTrans">
               
                    <apex:pageBlockTable value="{!subList}" var="trans" >

                         <apex:column value="{!trans.Activity_Title__c}"  />                                                 
                        
                          <apex:column value="{!trans.Year__c}" /> // I want this year to be displayed in the page block section TITLE
                  
                   </apex:pageBlockTable>
                  
              </apex:pageBlockSection>
           
           
            </apex:repeat> 

I tried returning Map<Integer, List<Trans__c > > from my controller where integer would be year. But in my repeat tag I did not know how to access the sublist and integer of the returned map obj for my page block table.

please let me know how I can get this achieved.

Thanks in Advance,
dvs

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae
Here is a sample wrapper class using Accounts and Contacts that should help getting you started.

Code:
<apex:page controller="wrappertestcon">
  <h1>Repeat Nested List Example</h1>
<apex:form >
<apex:pageBlock id="table"  >
   <apex:pageblockButtons location="bottom">
      <apex:commandLink value=" Get Accts " action="{!RetrieveAccts}" rerender="table"/>
   </apex:pageblockButtons>
   <apex:repeat value="{!accttree}" var="a" id="table"> 
        <apex:pageblockSection title="{!a.Name}" columns="1">      
           <apex:repeat value="{!a.contacts}" var="c">
                 <apex:pageblockSectionItem>
                     <apex:outputLabel value="     Contact Name: " for="contact_name"/>
                     <apex:outputText title="Contact Name: " style="font-style:bold" value="{!c.lastname}" id="contact_name" />
                 </apex:pageblockSectionItem>
                 <br/>
           </apex:repeat>
       </apex:pageblockSection>
    </apex:repeat> 
     </apex:pageBlock>
           </apex:form> 
</apex:page>

 
Code:
public class wrappertestcon {

    public List<cAccts> getAccttree() {
        return retAccts;
    }

    public List<cAccts> retAccts = new List<cAccts>{};
    public class cAccts{
       public String Name {get; set;}
       public List<Contact> contacts {get; set;}
       cAccts(Account a){
           Name=a.Name;
           contacts=[select id,lastname from Contact where accountid =:a.id];
       } 
    }
    
    
    public PageReference RetrieveAccts() {
        Set<ID> conID = new Set<ID>{};
        for(contact c:[select accountid from contact where accountid<>null limit 100]){
            conID.add(c.accountid);
        }
        for(Account a:[select id,Name,(select id,lastname from contacts) from Account where id in :conID]){
            retAccts.add(new cAccts(a));
        }        
        return null;
    }

}

 


All Answers

JimRaeJimRae
My guess would be that you need to create a wrapper class to do this.  Create a custom class that will contain both your year parameter as a string and the list of your Trans__c objects. There is a good tutorial on wrapper classes on the wiki that should help.
JimRaeJimRae
Here is a sample wrapper class using Accounts and Contacts that should help getting you started.

Code:
<apex:page controller="wrappertestcon">
  <h1>Repeat Nested List Example</h1>
<apex:form >
<apex:pageBlock id="table"  >
   <apex:pageblockButtons location="bottom">
      <apex:commandLink value=" Get Accts " action="{!RetrieveAccts}" rerender="table"/>
   </apex:pageblockButtons>
   <apex:repeat value="{!accttree}" var="a" id="table"> 
        <apex:pageblockSection title="{!a.Name}" columns="1">      
           <apex:repeat value="{!a.contacts}" var="c">
                 <apex:pageblockSectionItem>
                     <apex:outputLabel value="     Contact Name: " for="contact_name"/>
                     <apex:outputText title="Contact Name: " style="font-style:bold" value="{!c.lastname}" id="contact_name" />
                 </apex:pageblockSectionItem>
                 <br/>
           </apex:repeat>
       </apex:pageblockSection>
    </apex:repeat> 
     </apex:pageBlock>
           </apex:form> 
</apex:page>

 
Code:
public class wrappertestcon {

    public List<cAccts> getAccttree() {
        return retAccts;
    }

    public List<cAccts> retAccts = new List<cAccts>{};
    public class cAccts{
       public String Name {get; set;}
       public List<Contact> contacts {get; set;}
       cAccts(Account a){
           Name=a.Name;
           contacts=[select id,lastname from Contact where accountid =:a.id];
       } 
    }
    
    
    public PageReference RetrieveAccts() {
        Set<ID> conID = new Set<ID>{};
        for(contact c:[select accountid from contact where accountid<>null limit 100]){
            conID.add(c.accountid);
        }
        for(Account a:[select id,Name,(select id,lastname from contacts) from Account where id in :conID]){
            retAccts.add(new cAccts(a));
        }        
        return null;
    }

}

 


This was selected as the best answer
svdsvd
Hi Jim,

Thanks a lot. Wrapper class concept worked absolutely fine for my requirement.
note:
Place a empty pageblock section before your repeat tag to make other pageblock sections collapse/expand.

Thanks for your time,
dvs
pratiktannapratiktanna

I have a different problem handling repeats. I am not sure how the Ids are created for the elements inside the repeat. I have an inputtext field inside the repeat and I want to access its value so that I can pass it as a url parameter in the outputlink. What I am trying to do is to, on clicking of the outputlink, invoking a javascript function which will capture the quantity of the correct inputtext and pass the value back to the link. (Not sure if it is the best way to do it). So I want to get access of the right Id of the inputtext , but since its inside repeat not sure how can I do it.

guest1231231guest1231231

Is it possible to throw in a getQueryLocator in here so that pagination can be used?

 

Thanks