3

I have two picklists in a visualforce page with a controller extension:

<apex:actionFunction name="updateModelList" rerender="modelSelect"/>
....
<apex:selectList value="{!selectedBrand}" multiselect="false" size="1" onChange="updateModelList();">
    <apex:selectOptions value="{!brandObjects}"/>
</apex:selectList>
...
<apex:selectList id="modelSelect" value="{!selectedModel}" multiselect="false" size="1">
    <apex:selectOptions value="{!modelObjects}"/>
</apex:selectList> 

Then there is some controller functions code that creates the Options for these selects. This works as long as i don't touch the second picklist. Values are correctly updated and everything is fine.

Basically what I noticed is that whenever I trigger the onChange event on the first picklist and I changed the value on the second picklist, values on the second one are not correctly updated as the controller variable selectedBrand is set to '' (empty string) whereas the variable selectedModel is set to null. This causes the second picklist to be empty after this action.

Any insight?

edit: further info, the controller has two getter methods that return a List of SelectOption one for each picklist.

edit #2: i found out the problem, apparently VisualForce Pages invoke getter methods "randomly" so my bad I had a side effect which initialized the placeholder variable in one of these methods.

Thank You.

  • In your first list, you have the following added to your code: onChange="updateModelList(); So when you make a change to {!brandObjects} there should be a corresponding change to {!modelObjects}. Perhaps if you posted the code for your controller, something might be apparent as the cause. – crmprogdev May 8 '13 at 17:09
2

Ok after debug logs inspection I found out the main reason and the solution. Visualforce invokes getter methods of your controllers "randomly" so whenever you implement a getter method which is used in a Visualforce page you should always avoid side effects on the controller variables, (i.e. initializations, etc..).

My controller code accidentally had a selectedBrand String initialization to '' (empty string) in the getBrandObjects method, this caused the strange behaviour.

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.