Reputation: 1719
I have two dropdown menus in my program, the second one gets populated based on the selected value from the first.
When there is only one value in first menu, the second menu is not reflecting the values, but it works when I have more than one value. What could be causing the error?
Here is the code:
<h:selectOneMenu id="blSearchFacilityInput"
value="#{bLDashBoardAction.facilityId}" style="width:80px;">
<f:ajax event="valueChange" render="blSearchzoneInput"
listener="#{bLDashBoardAction.facValueChangeEvent}"/>
<f:selectItems value="#{bLDashBoardAction.svaFaciltyList}" var="c"
itemLabel="#{c.facCode}" itemValue="#{c.id}"/>
</h:selectOneMenu>
<h:selectOneMenu id="blSearchzoneInput" value="#{bLDashBoardAction.zoneId}"
style="width:80px;">
<f:ajax event="valueChange" render="blSearchSectorInput"
listener="#{bLDashBoardAction.zoneValueChangeEvent}"/>
<f:selectItems value="#{bLDashBoardAction.zoneList}" var="c"
itemLabel="#{c.zoneCode}" itemValue="#{c.zoneId}"/>
</h:selectOneMenu>
Upvotes: 2
Views: 3916
Reputation: 1109865
If there's only one item which is by default already preselected, then there's effectively no change when you attempt to select it once again. You really need a physical second item in order to trigger the change event.
Add another item with a null
value or a noSelectionOption="true"
.
<h:selectOneMenu id="blSearchFacilityInput"
value="#{bLDashBoardAction.facilityId}" style="width:80px;">
<f:ajax render="blSearchzoneInput"
listener="#{bLDashBoardAction.facValueChangeEvent}"/>
<f:selectItem itemLabel="--select one--" noSelectionOption="true" />
<f:selectItems value="#{bLDashBoardAction.svaFaciltyList}" var="c"
itemLabel="#{c.facCode}" itemValue="#{c.id}"/>
</h:selectOneMenu>
Now you can successfully trigger the value change event. Note that it's the default event already, so I omitted it from the code for brevity. No need to repeat the defaults.
An alternative is to just automatically prefill the child dropdown if there's only one item in the parent dropdown. E.g., directly after that moment when you fill the svaFacilityList
:
if (svaFaciltyList.size() == 1) {
zoneList = fillItBasedOn(svaFaciltyList.get(0));
}
Upvotes: 1