内部<apex:repeat> </apex:repeat> </apex:selectradio>时无法获取<apex:selectradio>的值

时间:2013-07-05 00:42:06

标签: salesforce apex-code visualforce

当我设置一个内部重复的<apex:selectRadio>的visualforce页面时,我似乎无法将所选值返回到控制器中。如果我将它移出重复,它可以正常工作。有什么想法吗?

控制器:

public with sharing class COPE_TestsExt {
public id tid {get;set;}
public boolean showTestSelect {get;set;}
public list<COPE_Tests__c> tests {get;set;}
public list<COPE_questions__c> questions {get;set;}
public list<List<SelectOption>> options {get;set;}
public COPE_results__c results {get;set;}
public list<string> rid {get;set;}   

public COPE_TestsExt(ApexPages.StandardController controller) {   

    results = new COPE_results__c();
    showTestSelect = true;
    tests = [select id, name from COPE_Tests__c];
    tid = null; 

}

public PageReference setTid(){

    integer ridCount = 0;       
    showTestSelect = false;
    tid = ApexPages.CurrentPage().getParameters().get('tid');
    results.cope_test__c = tid;
    questions = [select id, name, question_body__c, (select id, name, option_body__c from COPE_options__r order by name ASC) from COPE_questions__c ];
    options = new List<List<SelectOption>>();
    for(COPE_Questions__C q : questions){
        ridCount++;
        List<SelectOption> l = new List<SelectOption>();
        for(COPE_options__c op : q.COPE_options__r){
            l.add(new SelectOption(string.valueof(op.id), op.option_body__c));
        }
        options.add(l);
    }
    rid = new string[ridCount];
    integer tempCount = 0;
    while(tempCount < ridCount){
    rid[tempCount] = '';
    tempCount++;
    }

    return null;
}      
public pagereference submit(){
  return null;
}       
}

页:

    <apex:page showHeader="false" standardController="COPE_Tests__c" extensions="COPE_TestsExt" >
<apex:form id="theForm" >
<apex:pageblock >
    <apex:outputPanel id="testSelect">

    <apex:pageBlockTable value="{!tests}" var="t" rendered="{!showTestSelect}"  >
    <apex:column headerValue="Please select a test:" ><apex:commandLink reRender="testSelect,testPage" action="{!setTID}"  >{!t.name}<apex:param name="tid" value="{!t.id}"/></apex:commandLink></apex:column>
    </apex:pageBlockTable>
    </apex:outputPanel>
    <apex:outputPanel id="testPage">
    <apex:outputText rendered="{!not(showTestSelect)}">
    <apex:pageBlockSection collapsible="false" title="User Information" columns="1">
        <apex:inputField label="What is your full name?" value="{!results.name}" required="false"/>
        <apex:inputField label="What is your email address?" value="{!results.Email__c}" required="false"/>
        <apex:inputField label="What is your OE Tracker #?" value="{!results.OE_Tracker__c}" required="false"/>
    </apex:pageBlockSection>
    <apex:pageBlockSection collapsible="false" title="Test Questions" columns="1">
    <apex:variable var="count" value="{!0}" />
    <apex:repeat value="{!questions}" var="q">
    {!count+1}. {!q.Question_Body__c}
    <apex:repeat first="{!count}" rows="1" value="{!options}" var="op">
    <apex:selectRadio value="{!rid[count]}" >
    <apex:selectOptions value="{!op}" />
    </apex:selectRadio>
    </apex:repeat>
<apex:variable var="count" value="{!count+1}"/>
</apex:repeat>
<apex:commandButton reRender="check" action="{!submit}" value="Submit"/>
</apex:pageBlockSection>
</apex:outputtext>
</apex:outputPanel>
<apex:outputPanel id="check">
{!rid[0]}
</apex:outputPanel>
</apex:pageblock>
</apex:form>
</apex:page>

1 个答案:

答案 0 :(得分:0)

如果我说得对,你想存入

public list<List<SelectOption>> options {get;set;}

可能的答案。因此,更好的方法可能是将它存储在从Question.Id到List的地图中,就像这样

public Map<Id,List<SelectOption>> options {get;set;}
...    
for(COPE_Questions__C q : questions){
    ridCount++;
    List<SelectOption> l = new List<SelectOption>();
    for(COPE_options__c op : q.COPE_options__r){
        l.add(new SelectOption(string.valueof(op.id), op.option_body__c));
    }
    options.put(q.Id,l);
}

然后在VF页面中,您可以执行类似

的操作
<apex:repeat value="{!questions}" var="q">
  {!count+1}. {!q.Question_Body__c}
<apex:selectRadio value="{!rid[count]}" >
<apex:selectOptions value="{!options[q.Id]}" />
</apex:selectRadio>
<apex:variable var="count" value="{!count+1}"/>
</apex:repeat>

希望它有所帮助。

P.S。我可能会误解你的代码(他不太可读)如果不生气的话:) :( / p>)