Visualforce error when creating a apex:repeat on a selectoption
-
27-10-2019 - |
Question
Okay, I've been banging my head trying to figure out how to get around this issue without writing unnecessary code.
I have the following Visualforce code that is causing a error when saving:
<select id="rec_count">
<apex:repeat value="{!pg}" var="selpg">
<option {!IF(selpg.value = selectedpgtxt, 'selected','')} value="{!selpg.value}" >
{!selpg.value}
</option>
</apex:repeat>
</select>
The error is: Error: Element type "option" must be followed by either attribute specifications, ">" or "/>".
Apparently the visual force parser is upset about a option tag not having a attribute for the {!IF(selpg.value = selectedpgtxt, 'selected','')}
.
I have tried the equivalent of:
<option selected="" value="1">1</option>
<option selected="selected" value="2">2</option>
However browser considers all of the options selected doing this.
Solution
Unless this is considered unnecessary code, the following seems pretty straightforward to me.
Visualforce:
<apex:selectList value="{!theSelection}">
<apex:selectOptions value="{!theList}"/>
</apex:selectList>
Apex:
// Top of class
public List<SelectOption> theList {get; private set;}
public String theSelection {get; set;}
// In constructor
this.theList = new List<SelectOption>();
this.theList.add(new SelectOption('1', 'First Option'));
this.theList.add(new SelectOption('2', 'Second Option'));
// Now for the default
this.theSelection = '1';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow