+ Start a Discussion
justin_sfdcjustin_sfdc 

need to give unique id in <apex:inputField> which is inside <apex:repeat>

Hi,

I have a VFpage where in I am using the <apex:repeat> component which will repeat the code depending on number of items in the object. Inside the <apex:repeat> component, I have <apex: inputField> components; this are the fields that are being repeated. currently, in the <apex:inputField>, I gave them a regular id="test1", but the problem that I am facing is if there are 3 records in the object then the repeat component will put three sections in the VF page. In this case, all three inputFields are having the same id. 

I want to give them all the unique id's no matter how many records we have in the other object.

 

Please throw out any ideas you have for this problem. I would really appreciate it.

 

Thanks-in-Advance,

Justin~sfdc

amarcuteamarcute

Hi,

 

You dont have to make the <apex:inputField> Ids dynamic programmatically. These Ids are dynamic by default. Please check the bellow code and the HTML code generated out of it. Observe the Ids of <apex:inputField> inside <apex:repeat> 

id="thePage:myForm:theRepeat:1:myStr" & id="thePage:myForm:theRepeat:2:myStr" etc

 

<apex:page standardController="account" extensions="repeatCon" id="thePage">

<apex:form id="myForm">
    <apex:repeat value="{!strings}" var="string" id="theRepeat">

        <apex:outputText value="{!string}" id="theValue"/><br/>
        <apex:inputField id="myStr" value="{!account.Name}"/>

    </apex:repeat>
</apex:form>
</apex:page>

 

public class repeatCon {

    public repeatCon(ApexPages.StandardController controller) {

    }

    public String[] getStrings() {

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

    }
}

 

<form id="thePage:myForm" name="thePage:myForm" method="post" action="https://c.na9.visual.force.com/apex/RepeatIdTest" enctype="application/x-www-form-urlencoded">   <input type="hidden" name="thePage:myForm" value="thePage:myForm" />   <span id="thePage:myForm:theRepeat:0:theValue">ONE</span><br /><div class="requiredInput"><div class="requiredBlock"></div><input id="thePage:myForm:theRepeat:0:myStr" maxlength="255" name="thePage:myForm:theRepeat:0:myStr" size="20" type="text" /></div><span id="thePage:myForm:theRepeat:1:theValue">TWO</span><br /><div class="requiredInput"><div class="requiredBlock"></div><input id="thePage:myForm:theRepeat:1:myStr" maxlength="255" name="thePage:myForm:theRepeat:1:myStr" size="20" type="text" /></div><span id="thePage:myForm:theRepeat:2:theValue">THREE</span><br /><div class="requiredInput"><div class="requiredBlock"></div><input id="thePage:myForm:theRepeat:2:myStr" maxlength="255" name="thePage:myForm:theRepeat:2:myStr" size="20" type="text" /></div><div id="thePage:myForm:j_id1"></div>   </form>

justin_sfdcjustin_sfdc

Hi Amarcute,

 

Thanks for the quick response. 

I am actually using the tool that will take in the id and provide its functionality. With the solution you gave, I am able to retrieve the ids in the format you had explained which is: id="thePage:myForm:theRepeat:1:myStr" & id="thePage:myForm:theRepeat:2:myStr" etc

Instead, for the tool to perform its functionality, we want the id's separate. For example;

id="thePage:myForm:theRepeat:1:myStr1" & id="thePage:myForm:theRepeat:2:myStr2" etc

 

Thanks,

Justin~sfdc

Jaap ScheperJaap Scheper
Use {!$Component.myStr}