使用Map无效的分页

时间:2016-03-10 08:49:25

标签: salesforce visualforce apex

我试图在我的Visualforce页面中使用Map<Question, List<Option>>进行分页。我已将pageSize设置为1.首先它只显示1个问题及其各自的选项,但是当我按下Next(&gt;)按钮时,它会显示上一个问题以及下一个问题。这意味着,它显示了两个问题及其各自的选项。然后当我再次按下它时,它会显示3个问题及其选项。

我想在每次点击时只显示一个问题及其选项。接下来它应该显示下一个问题,而在上一个(&lt;)它应该显示最后一个问题。

这是我的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>

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为您没有在方法getQuestions()中重新初始化地图变量“questions”。您必须在方法getQuestions()中添加一行来重新初始化地图变量“questions”。这将使其为空,之后只会添加一个问题。请参阅以下更正的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;  

}