将多个单选按钮值从Visual force页面传递给控制器

时间:2016-01-18 08:07:40

标签: radio-button salesforce visualforce

我在visual force页面上使用Page block table,每行都有单选按钮,我想将每个选中的单选按钮的值从visual force页面的每一行传递给apex Controller类。

现在,我只得到最后一行的一个值,我需要所选单选按钮的每个值。

Visual Force Page

    <apex:pageBlockTable value="{!SurveyQuestions}" var="sq">
        <apex:column value="{!sq.Id}"/>
        <apex:column value="{!sq.Name}"/>
        <apex:column value="{!sq.QuestionID__r.Question__c}"/>
        <apex:column value="{!sq.QuestionID__r.Id}" headerValue="Question ID"/>
        <apex:column headerValue="HiddenQuestion Id">
         <apex:inputHidden value="{!sq.QuestionID__r.Id}" id="HdnQuestionID"/> 
        </apex:column>  
        <apex:column headerValue="Question options">
            <apex:selectRadio value="{!selectedAnswer}">
              <apex:selectOptions value="{!items}"/>
            </apex:selectRadio> 
        </apex:column>   
    </apex:pageBlockTable>

Apex控制器类

public with sharing class GbPracticeController {

public tblSurvey__c SurveyObj {get;set;}

public string SelectedAnswer                 {get;set;}
public list<tblSurveyQuestion__c> SurveyQuestions {get;set;}

public GbPracticeController(ApexPages.StandardController controller) {
        SurveyObj = new tblSurvey__c(); 

}


public PageReference SaveSurvey() {  
    insert SurveyObj;
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Survey Submitted Successfully'));

    return null;
}

//Listing Survey Related Questions
public ApexPages.StandardSetController setSurveyQuestions {
    get {
        if(setSurveyQuestions == null) {
            setSurveyQuestions = new ApexPages.StandardSetController(Database.getQueryLocator(
                [SELECT Id,Name,fo__QuestionID__c,fo__SurveyID__c, fo__QuestionID__r.Id,fo__QuestionID__r.Name,fo__QuestionID__r.fo__Question__c, fo__QuestionID__r.fo__SelectedAnswer__c  FROM fo__tblSurveyQuestion__c WHERE fo__SurveyID__c = 'a0R28000000FfXaEAK']));
        }
        return setSurveyQuestions;
    }
    set;
}
// Initialize getSurveyQuestions and return a list of records
public List<tblSurveyQuestion__c> getSurveyQuestions() {
    return (List<tblSurveyQuestion__c>) setSurveyQuestions.getRecords();
}

public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>(); 
    options.add(new SelectOption('yes','Yes')); 
    options.add(new SelectOption('no','No')); 
    options.add(new SelectOption('Maybe','maybe')); 
    return options; 
}

public PageReference SubmitResult() {

// Multiple Checkbox value Logic Goes Here
for (SurveyQuestionsType sqItem : SurveyQuestions) {
        if(sqItem.SelectedAnswer__c == 'yes') {

               //Inserting The Survey Result        
               tblSurveyResult__c SurveyResult = new tblSurveyResult__c(
                      Name = sqItem.SelectedAnswer__c,
                      Answer__c = 1
                     );
               insert SurveyResult;


            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Survey Submitted Successfully'));

        } else if (sqItem.SelectedAnswer__c == 'no'){

            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,sqItem.SelectedAnswer__c));

        } else {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Select Atleast one value'));
        }
    }
    return null;
}
// qwerty
}

2 个答案:

答案 0 :(得分:1)

回答我自己的问题,可能对其他人有帮助。
VF页面

<apex:pageBlockTable value="{!SurveyQuestions}" var="sq">
        <apex:column value="{!sq.Id}"/>
        <apex:column value="{!sq.Name}"/>
        <apex:column value="{!sq.QuestionID__r.Question__c}"/>
        <apex:column value="{!sq.QuestionID__r.Id}" headerValue="Question ID"/>
        <apex:column headerValue="HiddenQuestion Id">
          <apex:inputHidden value="{!sq.QuestionID__r.Id}"/>
        </apex:column>  
        <apex:column headerValue="Question options">
            <apex:selectRadio value="{!sq.QuestionID__r.SelectedAnswer__c}">
              <apex:selectOptions value="{!items}"/>
            </apex:selectRadio> 
        </apex:column>  
    </apex:pageBlockTable>

    <apex:commandButton value="Submit Survey Result" action="{!SubmitSurvey}" rerender="StartSurveyForm"/>


Apex类代码

public PageReference SubmitSurvey() {

// Multiple Checkbox value Logic Goes Here

for (tblSurveyQuestion__c sqItem : getSurveyQuestions()) {
    if(sqItem.QuestionID__r.SelectedAnswer__c == 'yes'){

               //Inserting The Survey Result

               tblSurveyResult__c SurveyResult = new tblSurveyResult__c(
                      Name = sqItem.QuestionID__r.SelectedAnswer__c,
                      Answer__c = decimal.valueof('1'),
                      QuestionID__c = sqItem.QuestionID__r.Id,
                      SurveyID__c= surveyId
                     );
               insert SurveyResult;

        }  else{

              ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Select Atleast one value'));  
        }
    }
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,’Survey Result Successfully Submitted'));  
    return null;}

答案 1 :(得分:0)

我建议您添加一个新的自定义字段,例如SelectedAnswer,更改

apex:selectRadio value="{!selectedAnswer}">

apex:selectRadio value="{!sq.SelectedAnswer__c}">

和控制器更改为

public PageReference SubmitSurvey() {

// Multiple Checkbox value Logic Goes Here
foreach (SurveyQuestionsType sqItem : getSurveyQuestions()) {
if(sqItem.SelectedAnswer__c == 'yes'){

           //Inserting The Survey Result

           tblSurveyResult__c SurveyResult = new tblSurveyResult__c(
                  Name = sqItem.SelectedAnswer__c,
                  Answer__c = 1
                 );
           insert SurveyResult;


    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Survey Submitted Successfully'));

} else if (sqItem.SelectedAnswer__c == 'no'){

    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,sqItem.SelectedAnswer__c));

} else {
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Select Atleast one value'));
}
}
return null;
}

或者,您可以使用新的SelectedAnswer字段创建此对象的DTO,而不是仅向自定义对象添加字段,并使用DTO。

此外,在错误输出期间,我会添加错误答案的指示。