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
keshin okawakeshin okawa 

N>Help Removing An Option on Other Dynamic SelectOptions

I have some code that displays dynamic values in a selectlist

                <apex:pageBlock >
                    <apex:pageBlockTable value="{!Sample}" var="field" styleClass="table table-striped">
                        <apex:column value="{!Sample[field][0]}" headerValue="Salesforce Field"/>
                        <apex:column headerValue="CSV Field">
                             <apex:selectList value="{!Sample[field][1]}" styleClass="form-control" size="1">
                                 <apex:selectOptions value="{!csvField}"/>
                             </apex:selectList>
                         </apex:column>
                         <apex:column >
                         </apex:column>
                    </apex:pageBlockTable>
                </apex:pageBlock>

the problem is I want to remove the value that one selectlist has chosen
ex. options = Name, Gender, Age
I want to remove Name from other selectlist if one of the selectlist chooses it. It's my first time so I don't know how to do it.
The dynamic values in the selectlist are from an uploaded csv file.
Best Answer chosen by keshin okawa
EnreecoEnreeco
Hi, 
you have to change your code according to your other part of the class.
There are few errors I made when initializing the list of List<SelectOption>:
public List<List<SelectOption>> csvFields = new List<List<SelectOption>>();

//this method should be called on load of the page as well
public void reloadPicklists(){
    //iterate through the picklists selected values
    //use the original list of SelectOptions to create all the List<SelectOption> for each picklist
    csvFields = new List<List<SelectOption>>();
    
	for(Integer i = 0; i < [numberOfPicklists]; i++){
		csvFields.add(new List<SelectOption>());
	}
	
    //copy all the values on the picklists
    for(Integer i = 0; i < [numberOfPicklists]; i++){
        csvFields[i].addAll(csvOriginal.clone());
    }

    //now removes all the "duplicate" values
    for(Integer i = 0; i < [numberOfPicklists]; i++){
        String selectedValue = picklistListValue[i];//this should be {!Sample[field][i]}
        
        //iterate through the other picklists
        for(Integer j = 0; j < [numberOfPicklists]; j++){
            if(i == j) continue;//skips current picklist
            
            List<SelectOption> actualPickListValues = csvFields[j];
            for(Integer k = actualPickListValues.size()-1; k >= 0; k--){
                //remove the "k" indexed value of the current picklist
                if(actualPickListValues[k].getValue() == selectedValue) actualPickListValues.remove(k);
            }
        }
    }
    
    //now you have all the picklists correctly populated
}

Remember that:
  • the csvOriginal List is the one that hosts all the values that comes from the CSV
  • in the VF page you have to use for each picklist something like "<apex:selectOptions value="{!csvFields[X]}" />"

 

All Answers

EnreecoEnreeco

Hi miczster,
I can imagine you have more than 1 selectlist defined as your script in the page.
You are asking something that could be done both via javascript and via Apex.
I will show you how to handle this via Apex (because via Javascript could be done in different ways and may be more complex).
N.B. This is only an hint, it is "not tested" code.
 
<apex:selectList value="{!Sample[field][1]}" styleClass="form-control" size="1">
   <apex:actionsupport rerender="theForm" action="{!reloadPicklists}" event="onchange" />
   <apex:selectOptions value="{!csvField}"/>
</apex:selectList>


In the controller:
public List<List<SelectOption>> csvFields = new List<List<SelectOption>>();

//this method should be called on load of the page as well
public void reloadPicklists(){
    //iterate through the picklists selected values
    //use the original list of SelectOptions to create all the List<SelectOption> for each picklist
    csvFields = new List<List<SelectOption>>();
    
    //copy all the values on the picklists
    for(Integer i = 0; i < [numberOfPicklists]; i++){
        csvFields.addAll(csvOriginal.clone());
    }

    //now removes all the "duplicate" values
    for(Integer i = 0; i < [numberOfPicklists]; i++){
        String selectedValue = picklistListValue[i];//this should be {!Sample[field][i]}
        
        //iterate through the other picklists
        for(Integer j = 0; j < [numberOfPicklists]; j++){
            if(i == j) continue;//skips current picklist
            
            List<SelectOption> actualPickListValues = csvFields[j];
            for(Integer k = actualPickListValues.size()-1; k >= 0; k--){
                //remove the "k" indexed value of the current picklist
                if(actualPickListValues[k].getValue() == selectedValue) actualPickListValues.remove(k);
            }
        }
    }
    
    //now you have all the picklists correctly populated
}
Where:
  • csvFields is a new List of picklist values (one for each picklists)
  • [numberOfPicklists] if the total number of picklists
  • csvOriginal is the original List<SelectOption> with all the csv values (to be used as the origin list, not to be used inside the VF page)

May the Force.com be with you!
keshin okawakeshin okawa
Thx for the code Enreeco.
I worked with the code you gave me and I encountered few errors and solved them and I stopped with an error
Variable does not exist: csvOriginal
I tried declaring it on my own and I get different errors all the time. May I ask how to do it?
 
EnreecoEnreeco
Hi, 
you have to change your code according to your other part of the class.
There are few errors I made when initializing the list of List<SelectOption>:
public List<List<SelectOption>> csvFields = new List<List<SelectOption>>();

//this method should be called on load of the page as well
public void reloadPicklists(){
    //iterate through the picklists selected values
    //use the original list of SelectOptions to create all the List<SelectOption> for each picklist
    csvFields = new List<List<SelectOption>>();
    
	for(Integer i = 0; i < [numberOfPicklists]; i++){
		csvFields.add(new List<SelectOption>());
	}
	
    //copy all the values on the picklists
    for(Integer i = 0; i < [numberOfPicklists]; i++){
        csvFields[i].addAll(csvOriginal.clone());
    }

    //now removes all the "duplicate" values
    for(Integer i = 0; i < [numberOfPicklists]; i++){
        String selectedValue = picklistListValue[i];//this should be {!Sample[field][i]}
        
        //iterate through the other picklists
        for(Integer j = 0; j < [numberOfPicklists]; j++){
            if(i == j) continue;//skips current picklist
            
            List<SelectOption> actualPickListValues = csvFields[j];
            for(Integer k = actualPickListValues.size()-1; k >= 0; k--){
                //remove the "k" indexed value of the current picklist
                if(actualPickListValues[k].getValue() == selectedValue) actualPickListValues.remove(k);
            }
        }
    }
    
    //now you have all the picklists correctly populated
}

Remember that:
  • the csvOriginal List is the one that hosts all the values that comes from the CSV
  • in the VF page you have to use for each picklist something like "<apex:selectOptions value="{!csvFields[X]}" />"

 
This was selected as the best answer