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
KaityKaity 

apex:repeat and apex:variable

Hi,

 

I have seen in many examples but donot know when and in which situation should we use <apex;repeat> within <apex:variable>.

 

CAn some one understand me with example?

 

 

 

 

.

Best Answer chosen by Admin (Salesforce Developers) 
Puja_mfsiPuja_mfsi

Hi,

 

apex:repeat An iteration component that allows you to output the contents of a collection according to a structure that you specify. The collection can include up to 1,000 items.

 

public class repeatCon {

      public String[] getStringsVal() {

            return new String[]{'ONE','TWO','THREE'};

      }

}

 

<apex:page controller="repeatCon">

       <apex:form>

              <apex:repeat value="{!StringsVal}" var="acc">

                      <apex:outText value="{!acc}"/>

              </apex:repeat>

       </apex:form>

</apex:page>

 

you can iterate the collection types variable over apex:repeat. there r two attribue one value which takes the collection variable and other one is var which rpresent the each record like first rec,sec rec etc.

 

 

apex:variable  A local variable that can be used as a replacement for a specified expression within the body of the component. Use <apex:variable> to reduce repetitive and verbose expressions within a page.

 

Suppose u need to color the last value of the above outpout text.

 

<apex:page controller="repeatCon">

       <apex:form>

           <apex:variable var="count" value="{!1}"   />        <!-- Initialise-->

             <apex:repeat value="{!StringsVal}" var="acc">

                      <apex:outText value="{!acc}"  style="color:{!IF(count == StringsVal.size,'red','')}/>

                      <apex:variable value="{!count=1}" var="count"/>  <!--increment-->

              </apex:repeat>

       </apex:form>

</apex:page>

 

 

please let me know if u have any problem on same,and if this post helps u then plz give KUDOS by click on star at left.

All Answers

Puja_mfsiPuja_mfsi

Hi,

 

apex:repeat An iteration component that allows you to output the contents of a collection according to a structure that you specify. The collection can include up to 1,000 items.

 

public class repeatCon {

      public String[] getStringsVal() {

            return new String[]{'ONE','TWO','THREE'};

      }

}

 

<apex:page controller="repeatCon">

       <apex:form>

              <apex:repeat value="{!StringsVal}" var="acc">

                      <apex:outText value="{!acc}"/>

              </apex:repeat>

       </apex:form>

</apex:page>

 

you can iterate the collection types variable over apex:repeat. there r two attribue one value which takes the collection variable and other one is var which rpresent the each record like first rec,sec rec etc.

 

 

apex:variable  A local variable that can be used as a replacement for a specified expression within the body of the component. Use <apex:variable> to reduce repetitive and verbose expressions within a page.

 

Suppose u need to color the last value of the above outpout text.

 

<apex:page controller="repeatCon">

       <apex:form>

           <apex:variable var="count" value="{!1}"   />        <!-- Initialise-->

             <apex:repeat value="{!StringsVal}" var="acc">

                      <apex:outText value="{!acc}"  style="color:{!IF(count == StringsVal.size,'red','')}/>

                      <apex:variable value="{!count=1}" var="count"/>  <!--increment-->

              </apex:repeat>

       </apex:form>

</apex:page>

 

 

please let me know if u have any problem on same,and if this post helps u then plz give KUDOS by click on star at left.

This was selected as the best answer
KaityKaity

Thanks Puja...

ksmoksmo
Hi,

Inside the <apex:repeat> I am having around 5 divs which get populated based on the number of times the apex repeat value has.So if there are 5 items the title,checkboxname,heckbox and preview button gets populated 5 times to the side of the item. But for the checkbox alone I want it to populate only once and not 5 times. How do I acheive this?


<div class="xContent">

<apex:repeat value="{!itemList}"
                         var="item">
<div class="xRow">
<div class="xRowRight">

                  
                     
                     <apex:outputPanel rendered="{!ifPDF != true}">
                            
                        <div class="xRowName ">
                                {!benefit.cockpittitle}
                 
                            <div class="xcheckboxname">
                                   CHECKBOXNAME                                                                                      
                                     </div>  <br/>
                                    
                                                                                 
                          </div> 
                       
                        </apex:outputpanel>                      
  <div class="Xrowcategorypanel">

 <div class="Xcheckboxpanel">
                                      <input id="changecolor" type="checkbox" name="BRAND"  onclick="Changecolor()" /> 
                                   
                                 
                                               <div id="XpreviewButton" onclick="ShowPopup()">
                                                     PREVIEW <img class="cockpitImage"
                                               src="{!URLFOR($Resource.BEN_IM, 'magnifying.png')}"/>                               
                                      </div>                                                 
                               </div> 

</div>

</div>
</div>
</div>
 
Arun Garg 9Arun Garg 9
How to use apex:variable in Visualforce Page?
Click here for the example.

https://sfdcadda.blogspot.in/2017/05/how-to-use-apexvariable-in-visualforce.html
Phil WPhil W

I note that a number of these suggestions include use of a variable as a counter in a repeat. To quote the documentation (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_variable.htm):

"Note: <apex:variable> does not support reassignment inside of an iteration component, such as <apex:dataTable> or <apex:repeat>. The result of doing so, e.g., incrementing the <apex:variable> as a counter, is unsupported and undefined."

This tells me that even if this behaviour currently works, it is unsupported and future updates to the internals of Salesforce Visualforce page rendering could change this behaviour at any time.

TechBieTechBie
I have a problem with <apex:repeat> tag. I don't wanna reppeat certain elements inside my apex: repeat. please check my code below.

<apex:repeat var="v" value="{!list 1}">
<apex:outtputpanel layout="none" rendered="{!if(v.post_no__c!=null,true,false)}"
                        <tr>                             
                            <td > post no </td>   
                            <td><apex:outputField value="{!v.post_no__c}" /></td
                        </tr>
 </apex:repeat>

In my above code, I put <td>  and <tr> inside repeat so that they are also got displayed based on the number of iterations. I want to restrict the number of iterations. please don't suggest javascript why because I am using this VF as pdf and passing id to the pdf from the controller. Once after clicking on the button 'generate pdf' then only values are passed to 'list1' in the code.

I think it would be somewhat difficult to answer if there are any persons who can answer please let me know. 

Thanks in Advance.