In a Visualforce page, I have an apex:repeat
iterating over a List<List<a Class>>
, and I want to index the current nested list using an apex:variable. Prefer not to use two nested apex:repeats for requirement reasons.
In its simplest form:
<apex:variable var="scheduleIndex" value="0"/> <apex:repeat value="{!revenueSchedules}" var="revSched"> // I want to do something like this, where scheduleIndex is the apex:variable and NameString is some property of the List returned by revSched.scheduleIndex: <apex:outputfield value="{!revSched}.scheduleIndex.{NameString}"> </apex:outputField> <apex:variable var="scheduleIndex" value="{!scheduleIndex + 1}"/> </apex:repeat>
Is this possible with just Visualforce, i.e. can we index the current iteration of apex:repeat and simply read properties? I prefer to not modify the controller, although it’s looking like that’s where I’ll have to go.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
If you need an index, consider making a list of indexes and iterating over that instead:
public Integer[] getIndexArray() { Integer[] results = new Integer[0]; while(results.size() < someArray.size()) results.add(results.size()); return results; }
You can then loop over this:
<apex:repeat var="index" value="{!indexArray}"> <apex:outputField value="{!someArray[index].Name}" /> </apex:repeat>
You can technically use an index using apex:variable, but the documentation states it is not supported. I have successfully used this in trivial pages, though, so it’s entirely possible:
<apex:variable name="index" value="{!0}" /> <apex:repeat ...> <!-- other stuff here --> <apex:variable name="index" value="{!index+1}" /> </apex:repeat>
This isn’t always guaranteed to work, but may work in some trivial cases.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0