[SalesForce] Compare two lists to form a third SelectOption list

I need to take two SelectOption lists and create a third one which will be composed of the values which are in both lists.

I'm having trouble figuring out how to properly compare them in this code I have.

I've left out a good portion of the code in order to try and simplify it.

I need to compare the selectedSubs2 list to the getkeyTechValues list.

Here is the code :

selectedSubs2 = new list<SelectOption>();
String[] selectedvalues = selectedkeys.split(',');

        for (String selectedvalue: selectedvalues) 
            {
            selectedSubs2.add(new SelectOption(selectedvalue,selectedvalue));
            }


  public List<SelectOption> getkeyTechValues()
    {
        List<SelectOption> options=new List<SelectOption>();
            options.add(new SelectOption('--None--','--None--'));
            String[] picklistlines =new String[]{};

            for (String item : selectedMulPickKeyTech.split(',') )
             {

            if (!selectedItems.contains(item)) 
                {
                //add it to your iist
                options.add(new SelectOption(item,item));
                }
            }
        return options;
        }
    } 

    //somehow I need to compare the selectedSubs2 list to the getkeyTechValues 
    //and make a 3rd SelectOption list of only the values which are in both

Thank you very much for your help. I really appreciate it.

Best Answer

Your controller will need to a lot of the work in terms of recalculating the third dropdown. I'm thinking the first and second select lists will call a method in your controller to clear out and re-generate the third dropdown, effectively creating a cascading or dependent drop down. See my controller example below.

public class MySelectListController{
    public List<String> firstSelections {get;set;}
    public List<String> secondSelections {get;set;}
    public List<String> thirdSelections {get;set;}

    public List<SelectOption> availableOptionsFirstList {get;set;}
    public List<SelectOption> availableOptionsSecondList {get;set;}
    public List<SelectOption> availableOptionsThirdList {get;set;}

    //Might have to modify if it's an extension, for instance
    public MySelectListController(){
        firstSelections = new List<String>();
        secondSelections = new List<String>();
        thirdSelections = new List<String>();

        //Populate your options lists here
        availableOptionsFirstList = new List<SelectOption>();
        availableOptionsFirstList.add(new SelectOption('A','A'));
        availableOptionsFirstList.add(new SelectOption('B','B'));
        availableOptionsFirstList.add(new SelectOption('C','C'));
        availableOptionsFirstList.add(new SelectOption('D','D'));


        availableOptionsSecondList = new List<SelectOption>();
        availableOptionsSecondList.add(new SelectOption('E','E'));
        availableOptionsSecondList.add(new SelectOption('F','F'));
        availableOptionsSecondList.add(new SelectOption('G','G'));
        availableOptionsSecondList.add(new SelectOption('H','H'));

        availableOptionsThirdList = new List<SelectOption>();        
    }

    public PageReference handleSelections(){
        availableOptionsThirdList.clear();
        addSelectedOptions(
            availableOptionsThirdList,
            availableOptionsFirstList,
            firstSelections);

        addSelectedOptions(
            availableOptionsThirdList,
            availableOptionsSecondList,
            secondSelections);

        //rerender current page
        return null;
    }

    private void addSelectedOptions(List<SelectOption> childOptions, List<SelectOption> parentOptions,List<String> selections){
         for(String current : selections){
            for(SelectOption opt : parentOptions){
                if(opt.getValue() == current){
                    childOptions.add(new SelectOption(opt.getValue(),opt.getLabel()));
                }
            }
        }
    }
}

Furthermore, you'll need to use an apex:actionFunction or call into the controller and rerender the visualforce page to pick up the changes incurred by calling the handleSelections method.

<apex:page controller="MySelectListController">
<apex:form id="theform">

    <apex:selectList value="{!firstSelections}" multiselect="true">
        <apex:selectOptions value="{!availableOptionsFirstList}"/>
        <apex:actionSupport action="{!handleSelections}" rerender="theform" event="onchange"/>
    </apex:selectList>
    <apex:selectList value="{!secondSelections}" multiselect="true">
        <apex:selectOptions value="{!availableOptionsSecondList}" />
        <apex:actionSupport action="{!handleSelections}" rerender="theform" event="onchange"/>
    </apex:selectList>
    <apex:selectList value="{!thirdSelections}" multiselect="true">
        <apex:selectOptions value="{!availableOptionsThirdList}" />
        <apex:actionSupport action="{!handleSelections}" rerender="theform" event="onchange"/>
    </apex:selectList>
</apex:form>
</apex:page>

Hope this helps get you on the right track.