You need to sign in to do that
Don't have an account?
Max Smith
How to iterate over an sObject in Visualforce
Hi All,
Please see my code below
I'm trying to dynamically create a table for an email template. The table will have a dynamic number of columns. The above visualforce markup is controlled by a custom controller on the back end and I can provide it if needed but I don't want to overly complicate things.
HostedProducts is a list of OpportunityLineItem. The controller decides which OLI fields are queried and sent back to the component. I want to display every queried field for each OLI returned. Because this is dynamic, I don't know the field names until runtime. So I can't use the typical {!hprod.unitprice}.
Any direction would be appreciated.
Thanks,
Max
Please see my code below
<table class="section"> <tr> <apex:repeat value="{!hostedHeaders}" var="header"> <th><apex:outputText value="{!header}"></apex:outputText> </th> </apex:repeat> </tr> <apex:repeat value="{!hostedProducts}" var="hProd"> <tr> <apex:repeat value="{!hProd}" var="prod"> <td><apex:outputText value="{!prod}"></apex:outputText> </td> </apex:repeat> </tr> </apex:repeat> </table>
I'm trying to dynamically create a table for an email template. The table will have a dynamic number of columns. The above visualforce markup is controlled by a custom controller on the back end and I can provide it if needed but I don't want to overly complicate things.
HostedProducts is a list of OpportunityLineItem. The controller decides which OLI fields are queried and sent back to the component. I want to display every queried field for each OLI returned. Because this is dynamic, I don't know the field names until runtime. So I can't use the typical {!hprod.unitprice}.
Any direction would be appreciated.
Thanks,
Max
{!header[fieldVar1]}
{!prod[fieldVar2]}
where fieldVar = dynamic field variable
As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.
Thanks
All Answers
{!header[fieldVar1]}
{!prod[fieldVar2]}
where fieldVar = dynamic field variable
As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.
Thanks
In order to achieve this you have to define a property as List of String in your controller class which will hold the name of query fields.
For Eg : If your query is : [Select Id,Name,UnitPrice,Amount from OLI] than this list will be containing four elements as : Id,Name,UnitPrice,Amount.
If your query is modified by either the addition of an extra field or the removal of any field than you have to maintain this list accordingly.
After your list is populated with correct elements than you can use it in your VF page as follows : Below are the two lines from above code that are modified by me :
<apex:repeat value="{!listOfFieldNames}" var="fieldName"> <td>
<apex:outputText value="{!hProd.fieldName}"></apex:outputText> </td>
Right now this is the only possible way to show colums value dynamically.
Please let me know if anything is not clear to you or you need any help on this.
Thanks,
Abhishek.
William, your solution worked like a charm.
Abhishek, for some reason your solution tries to reference the variable name instead of the variable value. I get an error similar to "hProd.fieldName does not exist". Im not sure is this is a problem with the API version I have or not.
Best,
Max