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
hgolovhgolov 

Render Field Based on String Variable In Controller

Hi All,

First of all, thanks for taking the time to look at my question.

I have the following issue. I am writing a detail page in vf, and the client wants inline editing.

I am trying to implement as follows:

 

  <apex:outputField value="{!myTask.Owner.Name}" id="ownerDisplay" /><apex:commandLink id="editOwnerLink"  action="{!renderField}" reRender="ownerEdit, fieldToEditDisplay" >Edit<apex:param name="fieldToShow" value="Owner"/> </apex:commandLink>
         
           <apex:inputField value="{!myTask.OwnerId}" id="ownerEdit" rendered="{!IF(FieldToEdit == 'Owner','true','false')}"/>
          <apex:outputText value='{!FieldToEdit}' id='fieldToEditDisplay'></apex:outputText>

 

The controller code:

public String getFieldToEdit(){
		return fieldToEdit;
	}
	
	public void renderField(){
		fieldToEdit = ApexPages.currentPage().getParameters().get('fieldToShow');
	}
	

 

 

I have tried to do the render formula in different ways: plain {!FieldToEdit == 'Owner'}, {!FieldToEdit.equals('Owner')},{!IF(FieldToEdit == 'Owner', true, false}.

I've also tried with = and not ==. NOTHING SEEMS TO WORK.

I know that the fieldToEdit does get set to owner because I output with the <apex:outputText> just below.

Does anyone have any idea why this wouldn't work???

 

Any suggestions are welcome!

Thanks..

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

As your input field is not rendered when the page is initially displayed, there is nothing there to be re-rendered when the link is clicked.

 

You need to wrap your input field in an output panel and re-render that.  That way the re-rendered item is always present, it just doesn't contain anything to begin with.

 

E.g.

 

<apex:outputField value="{!myTask.Owner.Name}" id="ownerDisplay" /><apex:commandLink id="editOwnerLink"  action="{!renderField}" reRender="ownerEditPanel, fieldToEditDisplay" >Edit<apex:param name="fieldToShow" value="Owner"/> </apex:commandLink>
        <apex:outputPanel id="ownerEditPanel">
           <apex:inputField value="{!myTask.OwnerId}" id="ownerEdit" rendered="{!IF(FieldToEdit == 'Owner','true','false')}"/>
       </apex:outputPanel>
          <apex:outputText value='{!FieldToEdit}' id='fieldToEditDisplay'></apex:outputText>

All Answers

bob_buzzardbob_buzzard

As your input field is not rendered when the page is initially displayed, there is nothing there to be re-rendered when the link is clicked.

 

You need to wrap your input field in an output panel and re-render that.  That way the re-rendered item is always present, it just doesn't contain anything to begin with.

 

E.g.

 

<apex:outputField value="{!myTask.Owner.Name}" id="ownerDisplay" /><apex:commandLink id="editOwnerLink"  action="{!renderField}" reRender="ownerEditPanel, fieldToEditDisplay" >Edit<apex:param name="fieldToShow" value="Owner"/> </apex:commandLink>
        <apex:outputPanel id="ownerEditPanel">
           <apex:inputField value="{!myTask.OwnerId}" id="ownerEdit" rendered="{!IF(FieldToEdit == 'Owner','true','false')}"/>
       </apex:outputPanel>
          <apex:outputText value='{!FieldToEdit}' id='fieldToEditDisplay'></apex:outputText>
This was selected as the best answer
hgolovhgolov

Thank you! This is exactly what I needed.