2016-03-10 19 views
0

我想在我的Visualforce頁面中使用Map<Question, List<Option>>進行分頁。我將頁面大小設置爲1.首先,它只顯示1個問題及其各自的選項,但是當我按Next(>)按鈕時,它顯示前一個問題以及下一個問題。這意味着,它會顯示兩個問題及其各自的選項。然後當我再次按下,它會顯示3個問題及其選項。使用地圖不工作的分頁

我想在每次點擊時只顯示1個問題及其選項。在接下來它應該顯示下一個問題,並在上​​一個(<)它應該顯示最後一個問題。

這是我的Apex類:

public with sharing class SurveyExtension{ 
Survey__c survey{get; set;} 
List<Survey_Question__c> ques; 
public Map<Survey_Question__c, List<SelectOption>> questions= new Map<Survey_Question__c, List<SelectOption>>(); 
public String answer{get;set;} 

public SurveyExtension(ApexPages.StandardController con){ 
    survey = (Survey__c) con.getRecord(); 
}  
public ApexPages.StandardSetController setCon { 
    get{ 
     if(setCon == null){ 
      setCon = new ApexPages.StandardSetController([select Question__c,Option1__c,Option2__c,Option3__c,Option4__c,Option5__c,Option6__c from Survey_Question__c where Survey__r.id= :survey.id]); 
      setCon.setPageSize(1); 
      //noOfRecords = setCon.getResultSize(); 
     } 
     return setCon; 
    }set; 
} 
public Map<Survey_Question__c, List<SelectOption>> getQuestions(){ 
    ques = (List<Survey_Question__c>) setCon.getRecords(); 
    for(Survey_Question__c que: ques){ 
     List<SelectOption> olist = new List<SelectOption>(); 
     olist.add(new SelectOption('a',que.Option1__c)); 
     olist.add(new SelectOption('b',que.Option2__c)); 
     olist.add(new SelectOption('c',que.Option3__c)); 
     olist.add(new SelectOption('d',que.Option4__c)); 
     olist.add(new SelectOption('e',que.Option5__c)); 
     olist.add(new SelectOption('f',que.Option6__c)); 
     questions.put(que, olist); 
    } 
    return questions; 
} 
public void first() { 
    setCon.first(); 
} 

public void last() { 
    setCon.last(); 
} 

public void previous() { 
    setCon.previous(); 
} 

public void next() { 
    setCon.setPageSize(1); 
    setCon.next(); 
} 
public Boolean hasNext { 
    get { 
     return setCon.getHasNext(); 
    } 
    set; 
} 
public Boolean hasPrevious { 
    get { 
     return setCon.getHasPrevious(); 
    } 
    set; 
} 
} 

,這是我的Visualforce頁:

<apex:page standardController="Survey__c" extensions="SurveyExtension"> 
<apex:form > 
    <apex:pageBlock title="Survey Title" id="queBlock">              
      <apex:repeat value="{!questions}" var="que"> 
       Question : {!que.Question__c} <br/><br/> 
       Options : 
       <apex:repeat value="{!questions[que]}" var="opt"> 
        <apex:selectRadio value="{!answer}">       
         <apex:selectOptions value="{!opt}" />       
        </apex:selectRadio> 
       </apex:repeat> 
      </apex:repeat> 
      <apex:panelGrid columns="5"> 
       <apex:commandButton status="fetchStatus" reRender="queBlock" value="|<" action="{!first}" disabled="{!!hasPrevious}" title="First Page"/> 
       <apex:commandButton status="fetchStatus" reRender="queBlock" value="<" action="{!previous}" disabled="{!!hasPrevious}" title="Previous Page"/> 
       <apex:commandButton status="fetchStatus" reRender="queBlock" value=">" action="{!next}" disabled="{!!hasNext}" title="Next Page"/> 
       <apex:commandButton status="fetchStatus" reRender="queBlock" value=">|" action="{!last}" disabled="{!!hasNext}" title="Last Page"/> 
       <apex:outputPanel style="color:#4AA02C;font-weight:bold"> 
        <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/> 
       </apex:outputPanel> 
      </apex:panelGrid> 
    </apex:pageBlock> 
</apex:form> 

回答

0

發生這種情況,因爲你不重新初始化您的地圖變量 「問題」在getQuestions()方法中。您必須在方法getQuestions()中添加一行以重新初始化映射變量「questions」。這將使其爲空,並且之後只會添加1個問題。見getQuestions()方法的下面校正代碼 - :

public Map<Survey_Question__c, List<SelectOption>> getQuestions(){ 
//---------You must add this below line--------------------------- 
questions= new Map<Survey_Question__c, List<SelectOption>>(); 
ques = (List<Survey_Question__c>) setCon.getRecords(); 
for(Survey_Question__c que: ques){ 
    List<SelectOption> olist = new List<SelectOption>(); 
    olist.add(new SelectOption('a',que.Option1__c)); 
    olist.add(new SelectOption('b',que.Option2__c)); 
    olist.add(new SelectOption('c',que.Option3__c)); 
    olist.add(new SelectOption('d',que.Option4__c)); 
    olist.add(new SelectOption('e',que.Option5__c)); 
    olist.add(new SelectOption('f',que.Option6__c)); 
    questions.put(que, olist); 
} 
return questions; 

}