3

I am trying to build a VF page which dynamically displays the datatable. The fields to be displayed are not known, they need to taken from a custom setting.

Here is what i am trying to achieve

I have a custom setting which has field names(API names) of columns that need to be displayed in the data table

I also have a list of a quotelineitem which also uses this custom setting to get the records. The issue i am facing is how do we display the columns in datatable as we would only know the field name at run time.

In the code below to display account name i would need to specify <apex:outputText value="{!account.name}"/> in the VF page, but since i really dont know which fields would be displayed is there a way we can dynamically populate the field names.

I am not stuck with datatable, i am open to any other way to dynamically display the fields

            <apex:facet name="header">Name</apex:facet>

        <apex:facet name="footer">column footer</apex:facet>

        <apex:outputText value="{!account.name}"/>

   </apex:column>
3

Visualforce allows you to use brackets ([]) that treats data like a map. For sObjects in a visualforce page they're treated like a map of field values with api names for keys. So you just need to loop over the api names from your custom setting. The example below uses a page block, but you can use pretty much any output structure you want.

Example

<apex:pageBlock>
  <apex:pageBlockTable value="{!records}" var="record">
    <apex:repeat value="{!fieldAPINames}" var="fieldAPIName">
      <apex:column value="{!record[fieldAPIName]}"/>
    </apex:repeat>
  </apex:pageBlockTable>
</apex:pageBlock>
|improve this answer|||||
4

Have you considered using Fieldset to let your users configure the visible fields instead of using your own Custom Setting? This is a more native solution to what I believe is your use case. There is a good example how to use it in Visualforce here. You can also use apex:outputField to bind to the columns and display the values, which will leverage the native Visualforce formatting for various field types, currency, date, time etc.

|improve this answer|||||
1

Check out this post, talks about dynamically adding columns to VF table: http://www.cldpartners.com/blog/dynamic-columns-salesforce-visualforce-tables/

|improve this answer|||||
  • 2
    You may want to summarize the process in case the content you linked to ever goes away. – Mike Chale Apr 22 '13 at 18:20
  • The link went away :( – Dan Wooding Nov 11 '16 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.