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
benwrigleybenwrigley 

Assigning dynamic IDs to elements

Hi There,

 

Very new to visualforce here, so sorry if this is a dumb question.

 

I'm using the pageBlockTable tag to render a list of objects to the page. I want to make one of the columns hold an empty string that will be updated by some AJAX later on. In order to do that I want to add a unique ID to each one of these fields to reference it later. What I was hoping would work was:

 

 

   <apex:pageBlockTable value="{!RateCardPriceBreaks}" var="pb">
            <apex:column headerValue="Select" >
                <apex:commandButton action="{!getQuantity('test')}" value="Select"/>
            </apex:column>
            <apex:column headerValue="Product" value="{!pb.product__r.Name}" />
            <apex:column headerValue="Quantity" id="quantity" value="{!pb.ID}"/>
   </apex:pageBlockTable>

 

 

<apex:pageBlockTable value="{!RateCardPriceBreaks}" var="pb">                      
    <apex:column headerValue="Product" value="{!pb.product__r.Name}" />            
    <apex:column headerValue="Quantity" id="quantity{!pb.ID}" value=""/>  
</apex:pageBlockTable>
Error: Literal value is required for attribute id in <apex:column> at line 8 column 45

 

 

So that's obviously *not* the way to do it. Any tips?

 

TIA

Pradeep_NavatarPradeep_Navatar

In my opinion we can not assign dynamic id's to VF components but can assign dynamic id's to html elements.

 

In salesforce <apex:repeat> component shows dynamic id's automatically but if you try to assign by yourself then it will show an error.

 

Hope this helps.

miteshsuramiteshsura

I have very similar situation.

 

I am trying to add input element: Qty dynamically for selected Products. And I want to retrive the Qty values for each of the selected products in my controller. Dynamic Id is one work around, if someone can think of other work around please post it here. Thanks.

pkumarpkumar

Inside <apex:repeat> I have :

<td>                

<apex:inputtext value="{!fObj.val}" id="bVAL" />            

</td>

 

In this case the id is being generated dynamically by VF and we can retrieve the dynamic id by '{!$Component.bVAL}'.

 

hope this helps!

 

thanks.

Mitesh SuraMitesh Sura
Thank pkumar. That helped, but there is still some lag when selecting a row within datatable.

When user selects a record, there are 4 fields that needs to be udpate based on the logic in controller. Do you think there is anything else I can do to speed up the things? In long run, I may use jQuery.
Mitesh SuraMitesh Sura
Well spoke too soon. I have issue with id in repeat tag. Please see code below

<apex:repeat value="{!fieldSet}" var="f">
                                
   !-- Product quantity -->     
   <apex:column headerValue="{!f.Label}" rendered="{!f.fieldPath=='Quantity'}" width="50px"> 
      <apex:inputText id="prodQuantity" value="{!wP.quantity}" onchange="alert('{!$Component.prodSalePrice}');" />
   </apex:column>   
       
   /// Above alert: j_id0:form:j_id233:j_id241:j_id242:sectionProducts:0:j_id252:7:prodSalePrice ///
			        			
   <!-- Product sale price -->
   <apex:column headerValue="{!f.Label}" rendered="{!f.fieldPath=='UnitPrice'}" width="100px">
      <apex:inputText id="prodSalePrice" value="{!wP.salePrice}" />
   </apex:column>   

   /// Actual id: j_id0:form:j_id233:j_id241:j_id242:sectionProducts:0:j_id252:9:prodSalePrice ///

</apex:repeat>

I am doing simple alert and the id in the alert does not match with actual id of "prodSalePrice" input field. I have highlighted the difference ( 7 vs 9) above. 

Any clue?

Mitesh
Priyanka Reddy 74Priyanka Reddy 74
Hi @Mitesh Sura,

Even im also face issue. do you have any clue.

thank you