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
Mitesh SuraMitesh Sura 

Dynamic "headerValue" binding in VF page

Probably its just me, because I do not see anybody complaining about this. I have a simple VF page with pageblock referencing to a wrapper object. I need to display text from that wrapper object in headerValue for a column, but cannot do that, After trying for almost half a day, I have given up. 

 

Please look at column 2, dynamic referencing cannot be simple that this. It is just calling a string field, objType from the wrapper class. If one argues, that it has to be sObject field, please look at column 3. I am typecasting it with hardcoded 'ListPrice' field, but it still won't work. 

 

What is most frustrating, inputText values within those 2 columns which is replica of 'headerValue' works just fine, I can see ObjectType and field Lable in 2nd and 3rd column respt. 

 

Please help me , am I missing something here? are 'headerValues' exception to dynamic binding. I am sure someone might have come across this in the past. 

 

VF page:

<apex:pageBlock >
 <apex:pageBlockTable id="tbl1" value="{!wObjs}" var="wObj">                  
  <apex:column headerValue="Select">   
    <apex:inputCheckbox value="{!wObj.selected}" required="true" />
  </apex:column>               
  <apex:column headerValue="{!wObj.objType}">   
    <apex:inputText value="{!wObj.objType}" required="true" /> 
  </apex:column>              
  <apex:column headerValue="{!$ObjectType[wObj.objType].Fields.ListPrice.Label}">   
    <apex:inputText value="{!$ObjectType[wObj.objType].Fields['ListPrice'].Label}" required="true" /> 
  </apex:column>                             
 </apex:pageBlockTable>
</apex:pageBlock>

 Apex class (wrapper object):

public class wObj{
 public sObject sObj {get; set;}
 public string objType {get; set;}
 public Boolean selected {get; set;}
		        
 public wObj(sObject childObjRecord, boolean selected){
   this.sObj = childObjRecord; 
   this.objType = childObjRecord.getSObjectType().getDescribe().getName(); 
   this.selected = selected;
 }
}

 

Patrick DixonPatrick Dixon

I asume you also have something like:

 

public list<wObj> wObjs = new list<wObj>();

 

public list<wObj> getwObjs() {

    return wObjs;

}

 

in your controller?

Mitesh SuraMitesh Sura

This is what I have 

 

public list<wObj> wObjs {get; set;}
:
:
wObjs = new list<wObj>();
:
:
for(sObject childObjRecord : childObjRecords){
   wObjs.add(new wObj(childObjRecord, true));
}  

 If you look at input variables in VF page, they are being populated, but not the headerValue. 

 

Thanks Patrik for your time.

 

 

Caleb_SidelCaleb_Sidel

Intead of <apex:column headerValue="foo"> which I think can only take string literals?

try 

 

<apex:column>
<apex:facet name="header">{!wObj.objType}<apex:facet />
<apex:inputText value="{!wObj.objType" />
</apex:column>

 

 

basically make the header a facet vs a headerValue. I use facet to make something a command link for example, or a checkbox....so maybe it will work better for this to?

 

Let me know, I'll be curious if that solves it.

Mitesh SuraMitesh Sura
Caleb,

I tried that as well, nothing seems to be working. This is so straight fwd, I think it is a bug or a missing feature.
Are you in touch with salesforce team who manages vf bugs?

Thanks for your time.
Patrick DixonPatrick Dixon

Maybe the issue is that you are using a loop variable to set the header rather than a value fixed for the whole table?

 

So perhaps you could try setting an <apex:variable> outside the table which takes its value from the first element of your list, and then use that for the header text.

 

eg:

<apex:variable value="{!$ObjectType[wObjs[0].objType].Fields.ListPrice.Label}" var="colName">   

<apex:pageBlock >
 <apex:pageBlockTable id="tbl1" value="{!wObjs}" var="wObj">                  

    ...

    <apex:column headerValue="{!colName}">   

 

sfdcfoxsfdcfox

You can't use the repeat variable in headerValue, as it's not part of the repeat (it only appears once on the page). Instead, you'll have to use some non-repeating variable to show the headerValue dynamically, such as:

 

<apex:dataTable value="wrapperList" var="wrapper">
  <apex:column headerValue="{!$ObjectType[col1HeaderValue].Fields.Name.Label}">
     <apex:inputField value="{!wrapper.sobj['name']}" />
  </apex:column>
</apex:dataTable>

If you're trying to create the table with a variable number of columns and rows, you can use apex:repeat tags to dynamically create columns. In this case, use a List<String> to find your column names ( $ObjectType[objName].fields[fieldName].Label ) and field bindings ( inputField value="{!wrapper.record[fieldName]}" ). 

 

Also, there's the new Dynamic Visualforce feature that lets you create VF elements from Apex Code; you might also look into this route.

 

I'd recommend you take a step back, and state your goals for the project instead of starting from this half-way point. While I can speculate on what you might be trying to do, without knowing exactly whats going on, I don't know exactly how to assist you.

Mitesh SuraMitesh Sura

Patrick,

 

I cannot have apex:variable outside the pageBlockTable, because I need to display dynamic related objects with dynamic fields for an Id. 

I tried to set <apex:variable> inside the look, and assign it to column, but still blank. 

 

Thanks. 

Mitesh SuraMitesh Sura

sfdcfox,

 

You guessed it right, I need to show dynamic related objects, along with dynamic related fields. Wish I could use FieldSets, they are so easy to configure. 

 

I started with dynamic VF feature, but for some reason inputCheckBox won't set from Apex.  Please see below:

 

col = new Component.Apex.Column();
chkBox = new Component.Apex.inputCheckbox();
col.headerValue= String.Valueof(w.selected); // Does show true
chkBox.selected = true; // Does not work
chkBox.value = true; // Does not work
col.childComponents.add(chkBox);

 

Below is my best shot, can you please help with column headers, I did not quite understand your suggestion.

<apex:pageBlock >
                <apex:pageBlockTable value="{!wObjs}" var="wObj">  
                    <apex:column headerValue="Select">   
                        <apex:inputCheckbox value="{!wObj.selected}" required="true" />
                    </apex:column>  
                          
                    <!-- Fields 
                   	<apex:variable value="{!$ObjectType[wObj.objType].Fields[f.name].Label}" var="fieldLabel"/>   
					<apex:variable value="{!f.name}" var="fieldAPIName"/> 
					-->                  
                    <apex:repeat value="{!wObj.wFields}" var="f"> 
			        	<apex:column headerValue="test">
                   			<apex:inputField value="{!wObj.sObj[f.Name]}" required="true" /> 			        		
			        	</apex:column>	
			        </apex:repeat>      
                </apex:pageBlockTable>                
            </apex:pageBlock>

Except for first column (select checkbox), other columns have blank header. 

 

Thank you for your time. 

Patrick DixonPatrick Dixon

Actually, I think I said the same as sfdcfox ...

Vj@88Vj@88
Try this
<apex:facet name="header">
                  <apex:outputPanel >
                      <apex:outputtext value="{!Your dynamic label} "/>
                     </apex:outputPanel>   
            </apex:facet>