function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rzsrzs 

Get all selected as well as unselected items of a Select List in a Controller

Hello,

 

I have a select list defined as follows :

 

 

<apex:selectList id="member_list" multiselect="true" value="{!selectedMembers}" size="10" >
          <apex:selectOptions value="{!MemberNames}"/>
</apex:selectList>

 Now, {!selectedMembers} is a the list of all selected items in the select list. However,is it possible to get a list of all items (selected as well as unselected) of the above select list in my Controller, preferably as a List ?

 

 

Thank You.

 

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Well, think it through...

 

Once the page has been rendered, the Controller is done with it until it needs to be re-rendered.

 

So, if you make changes via Javascript, the only way the Controller will know anything about it is if you go and tell it.

 

So you could use actionFunctions invoked from Javascript to call Controller methods to update the controllers knowledge of the SelectList components as you muck around with them in Javascript.  That way you're keeping the controller updated as you go.  But you have to maintain it yourself.

 

You also could write one Action which takes the entire select list as passed from Javascript (via Param), and stores it in the Controller.  So, instead of notifying the controller as you make changes, you could instead just update it all at once.

 

 

However, one must also ask the question, why are you building a select list in Apex, screwing around with it in Javascript, and then wanting the results back in Apex.  Why not do it all within the Controller in the first place.

 

Best, Steve.

 

 

All Answers

SteveBowerSteveBower

Well, MemberNames is already in your controller, and it already is the complete list of possible options.  selectedMembers is the list of the ones that are selected.

 

So, couldn't you just build that list yourself by iterating through the lists and constructing the list you want as you go?

 

Best, Steve.

rzsrzs

Thanks for the reply Steve. Well, you are right that MemberNames is the list of all possible options. But, im sorry i forgot to mention initially that this list changes. I have written some javascript that removes and adds elements to this selectList. 

So,though the select list contains all the options specified in MemberNames initially, this set of options then change.

Now, is there a way to get these new set of values present in my select list in the controller ?

 

Thank You.

SteveBowerSteveBower

Well, think it through...

 

Once the page has been rendered, the Controller is done with it until it needs to be re-rendered.

 

So, if you make changes via Javascript, the only way the Controller will know anything about it is if you go and tell it.

 

So you could use actionFunctions invoked from Javascript to call Controller methods to update the controllers knowledge of the SelectList components as you muck around with them in Javascript.  That way you're keeping the controller updated as you go.  But you have to maintain it yourself.

 

You also could write one Action which takes the entire select list as passed from Javascript (via Param), and stores it in the Controller.  So, instead of notifying the controller as you make changes, you could instead just update it all at once.

 

 

However, one must also ask the question, why are you building a select list in Apex, screwing around with it in Javascript, and then wanting the results back in Apex.  Why not do it all within the Controller in the first place.

 

Best, Steve.

 

 

This was selected as the best answer