1

当我设置一个包含<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>
4

1 回答 1

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 Page 你可以做类似的事情

<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>

希望能帮助到你。

PS我可能误解了你的代码(他不是很可读)如果这样不要生气:)

于 2013-07-08T21:06:11.507 回答