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
Abdul Mujeeb ShaikAbdul Mujeeb Shaik 

I need SYNTAX to read the SelectedValues (selectList value) in picklist (selectoptions) which is in <APEX:REPEAT>

I need SYNTAX to read the SelectedValues (selectList value) 
in picklist (selectoptions) which is in <APEX:REPEAT>
===
objectivetype: "yes, no, Maybe, dontknow"
===
My VF code:
-------------------
<table border="1" width="100%">
<apex:REPEAT value="{!ques}" var="singleQuestion">
<tr>
<td> {!singleQuestion} </td>

<td>
<apex:selectList value="{!Selectedvalues}" size="1">
<apex:selectoptions value="{!objectivetype}" />
</apex:selectList>
</td>
</tr>
</apex:repeat>
</table>
=====================
In Controller:
--------------
public List<String> Selectedvalues{get; set;}


 
Best Answer chosen by Abdul Mujeeb Shaik
Anirudh SinghAnirudh Singh
Hi Abdul,

In the controller, if you need to access the value in a method, directly use Selectedvalues there. Also, Selectedvalues gives you a String value, not a List<String>, so the SelectValues variable should be defined as:

public String Selectedvalues{get; set;}

But I can see here, you are repeating some questions and the list of answers and you must be wanting a Question Answer pair.
I have created a Visualforce Page and controller. On clicking the Submit Button on the page, you can see a Map of Question and its answer.
You can use this and do the further processing.


Please find below the Page:

<apex:page controller="Questions_Controller">
    <apex:form >
        <table border="1" width="100%">
            <apex:repeat value="{!ques}" var="singleQuestion">
                <tr>
                    <td>{!singleQuestion}</td>
                    
                    <td>
                        <apex:actionRegion >
                            <apex:selectList value="{!Selectedvalues}" size="1">
                                <apex:selectoptions value="{!objectivetype}" />
                                    <apex:actionSupport event="onchange" action="{!populateAnswers}" reRender="dummy">
                                        <apex:param assignTo="{!answeredQuestion}" value="{!singleQuestion}" name="answeredQuestion"/>
                                    </apex:actionSupport>
                            </apex:selectList>
                        </apex:actionRegion>
                    </td>
                </tr>
            </apex:repeat>
        </table>
        <apex:commandButton value="Submit" action="{!submitAnswers}" rerender="dummy"/>
    </apex:form>
</apex:page>

Please find below the controller:

public class Questions_Controller
{
    public List<SelectOption> objectivetype{get; set;}
    
    public String Selectedvalues{get; set;}
    
    public List<String> ques{get; set;}
    
    private Map<String, String> questionAnswerMap;
    
    public String answeredQuestion{get; set;}
    
    public Questions_Controller()
    {
        ques=new List<String>();
        ques.add('Question 1');
        ques.add('Question 2');
        
        objectivetype=new List<SelectOption>();
        objectivetype.add(new SelectOption('yes', 'yes'));
        objectivetype.add(new SelectOption('no', 'no'));
        objectivetype.add(new SelectOption('Maybe', 'Maybe'));
        objectivetype.add(new SelectOption('dontknow', 'dontknow'));
        
        questionAnswerMap=new Map<String, String>();
    }
    
    public void populateAnswers()
    {
        questionAnswerMap.put(answeredQuestion, Selectedvalues);
        Selectedvalues=Null;
    }
    
    public void submitAnswers()
    {
        system.debug('questionAnswerMap---->'+questionAnswerMap);
        Selectedvalues=Null;
    }
}

Please let me know if this helps. If yes, please mark the answer as solved.

Thanks and Regards,
Anirudh Singh

All Answers

Anirudh SinghAnirudh Singh
Hi Abdul,

In the controller, if you need to access the value in a method, directly use Selectedvalues there. Also, Selectedvalues gives you a String value, not a List<String>, so the SelectValues variable should be defined as:

public String Selectedvalues{get; set;}

But I can see here, you are repeating some questions and the list of answers and you must be wanting a Question Answer pair.
I have created a Visualforce Page and controller. On clicking the Submit Button on the page, you can see a Map of Question and its answer.
You can use this and do the further processing.


Please find below the Page:

<apex:page controller="Questions_Controller">
    <apex:form >
        <table border="1" width="100%">
            <apex:repeat value="{!ques}" var="singleQuestion">
                <tr>
                    <td>{!singleQuestion}</td>
                    
                    <td>
                        <apex:actionRegion >
                            <apex:selectList value="{!Selectedvalues}" size="1">
                                <apex:selectoptions value="{!objectivetype}" />
                                    <apex:actionSupport event="onchange" action="{!populateAnswers}" reRender="dummy">
                                        <apex:param assignTo="{!answeredQuestion}" value="{!singleQuestion}" name="answeredQuestion"/>
                                    </apex:actionSupport>
                            </apex:selectList>
                        </apex:actionRegion>
                    </td>
                </tr>
            </apex:repeat>
        </table>
        <apex:commandButton value="Submit" action="{!submitAnswers}" rerender="dummy"/>
    </apex:form>
</apex:page>

Please find below the controller:

public class Questions_Controller
{
    public List<SelectOption> objectivetype{get; set;}
    
    public String Selectedvalues{get; set;}
    
    public List<String> ques{get; set;}
    
    private Map<String, String> questionAnswerMap;
    
    public String answeredQuestion{get; set;}
    
    public Questions_Controller()
    {
        ques=new List<String>();
        ques.add('Question 1');
        ques.add('Question 2');
        
        objectivetype=new List<SelectOption>();
        objectivetype.add(new SelectOption('yes', 'yes'));
        objectivetype.add(new SelectOption('no', 'no'));
        objectivetype.add(new SelectOption('Maybe', 'Maybe'));
        objectivetype.add(new SelectOption('dontknow', 'dontknow'));
        
        questionAnswerMap=new Map<String, String>();
    }
    
    public void populateAnswers()
    {
        questionAnswerMap.put(answeredQuestion, Selectedvalues);
        Selectedvalues=Null;
    }
    
    public void submitAnswers()
    {
        system.debug('questionAnswerMap---->'+questionAnswerMap);
        Selectedvalues=Null;
    }
}

Please let me know if this helps. If yes, please mark the answer as solved.

Thanks and Regards,
Anirudh Singh
This was selected as the best answer
Abdul Mujeeb ShaikAbdul Mujeeb Shaik
Thnak you so much Anirudh Singh, i asked the syntax nd u gave me the working live example, thank you so much ...!! :-) 
Abdul Mujeeb ShaikAbdul Mujeeb Shaik

Hello Anirudha, 

Can  i set default values for each selectlsit value....in a repeat tag.

I mean, in detail..,
can i set 'YES' fro some fileds and 'NO' for some fileds and 'May be' and remaining 'DonKnow'

based on some condion ....for example
if  'singleQuesthion' ==' do_u_like_this_event '.....then for that 
 <apex:selectList value="{!SELECTEDVALUES}" size="1"> should be'YES' "default values should be this.(in apex repeat tag)
if 'singleQuesthion' ==' do_u_like_to_appy_now '.....then fro that 
 SELECTEDVALUES should be 'NO'

if 'singleQuesthion' ==' r_u_intrested_in_doing_business '.....then fro that 
 SELECTEDVALUES should be 'May be'
.

.

.

Like this...based on some condition of 'Single Question' automatically corresponding default values must appear in 
apex:selectlsit value.....which is in apex:repeat tag.

Anirudh SinghAnirudh Singh
Hi Abdul,

I hope you are still looking for the answer.

Yes, it is possible to set the default values. But it requires JavaScript.

The things to notice are below:
1. The submitAnswers method is now being called from JavaScript, and it is a RemoteAction static method.
You will need to do the further processing inside the method itself. 
Also, since the method is static, you will not be able to use any non-static variables or methods inside it, and you will have to use only static methods or variables inside it.

2. Also, the 'ques' is now a Map instead of List, it is used to hold the Question as key and the default answer as value.
Don't forget to put all your questions like this only and give defualt answer values for all the questions. So, if you have 10 questions, put all the 10 and their default values.
<apex:page controller="Questions_Controller">
    <script type="text/javascript">
    	var questionsList=new Array();
    	var answersList=new Array();
    	
    	document.addEventListener('DOMContentLoaded', 
            function()
            {
                var defaultAnswerElements=document.getElementsByClassName("defaultAnswer");
                
                for(var i=0; i<defaultAnswerElements.length; i++)
                {
                    questionsList.push(defaultAnswerElements[i].previousElementSibling.value);
                    
                    var children=defaultAnswerElements[i].nextSibling.childNodes;
                    for(var j=0; j<children.length; j++)
                    {
                        if(children[j].value!=undefined && children[j].value==defaultAnswerElements[i].value)
                        {
                            children[j].setAttribute("selected", true);
                            answersList.push(children[j].value);
                        }
                    }
                }
            }
        );
    	
    	function populateAnswer(element)
        {
            questionPosition=questionsList.indexOf(element.previousElementSibling.previousElementSibling.value);
            answersList[questionPosition]=element.value;
        }
    	
    	function submitQuestionAnswers()
    	{
            Questions_Controller.submitAnswers(questionsList, answersList, function(result, event){});
        }
    </script>
    
    <apex:form >
        <table border="1" width="100%">
            <apex:repeat value="{!ques}" var="singleQuestion">
                <tr>
                    <td>{!singleQuestion}</td>
                    <td>
                        <apex:actionRegion >
                            <apex:selectList value="{!Selectedvalues}" size="1" id="answer" onchange="populateAnswer(this);">
                                <apex:selectoptions value="{!objectivetype}" />
                                <input type="hidden" value="{!singleQuestion}" id="question" class="question"/>
                                <input type="hidden" value="{!ques[singleQuestion]}" id="defaultAnswer" class="defaultAnswer"/>
                            </apex:selectList>
                        </apex:actionRegion>
                    </td>
                </tr>
            </apex:repeat>
        </table>
        <apex:commandButton value="Submit" onclick="submitQuestionAnswers();" rerender="dummy"/>
    </apex:form>
</apex:page>
 
public class Questions_Controller
{
    public List<SelectOption> objectivetype{get; set;}
    
    public String Selectedvalues{get; set;}
    
    public Map<String, String> ques{get; set;}
    
    private static Map<String, String> questionAnswerMap;
    
    public String answeredQuestion{get; set;}
    
    public Questions_Controller()
    {
        ques=new Map<String, String>();
        ques.put('Question 1', 'dontknow');
        ques.put('Question 2', 'no');
        ques.put('Question 3', 'yes');
        ques.put('Question 4', 'Maybe');
        
        objectivetype=new List<SelectOption>();
        objectivetype.add(new SelectOption('yes', 'yes'));
        objectivetype.add(new SelectOption('no', 'no'));
        objectivetype.add(new SelectOption('Maybe', 'Maybe'));
        objectivetype.add(new SelectOption('dontknow', 'dontknow'));
    }
    
    @RemoteAction
    public static void submitAnswers(List<String> questionsList, List<String> answersList)
    {
        questionAnswerMap=new Map<String, String>();
        if(questionsList.size()>0)
        {
            for(Integer i=0; i<questionsList.size(); i++)
            {
                questionAnswerMap.put(questionsList[i], answersList[i]);
            }
        }
        system.debug('questionAnswerMap---->'+questionAnswerMap);
    }
}
Please let me know if this helps.

Thanks and Regards,
Anirudh Singh