Visualforce dynamic binding works for table columns

I was pleasantly surprised this week to find out that some customization that I needed to implement – allowing the set of columns in an apex:pageBlockTable to be specified in a custom setting – could be implemented in Visualforce using the most obvious solution:

<apex:pageBlockTable var="c" value="{!contacts}">
    <apex:repeat var="f" value="{!fields}">
        <apex:column value="{!c[f]}"/>
    </apex:repeat>
</apex:pageBlockTable>

where “contacts” is a list of Contact objects and “fields” is a list of Contact field names obtained from a custom setting holding comma separated values. (The query that populates “contacts” must also use the field names to avoid the “SObject row was retrieved via SOQL without querying the requested field” error.)

Advertisements