在分页期间保留所选复选框

时间:2017-10-02 08:27:10

标签: checkbox pagination salesforce visualforce apex

任何知道如何在分页期间保留所选复选框以及使用下面的代码在选择列表中选择其他值的人。谢谢! :)

这是我使用的扩展程序。它包括表格,选择值和分页。

 //for season status
public void Stats(){               
    this.RaceEntries= 0;
    this.TotDis= 0;
    this.hasError = false;

    for(TrackWrapper tw : twList){           
            if(tw.isChecked){               
                    if(this.RaceEntries<=12 && this.TotDis<=300){
                        this.RaceEntries++;
                        this.TotDis += tw.tra.Length__c;
                    }

                    if(this.RaceEntries > 12){
                        this.hasError = true;
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Race Entries exceeded. 12 tracks per season only.'));
                    }
                    if(this.TotDis > 300){
                        this.hasError = true;
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Distance exceeded. 300KM per season only.'));
                    }                                  
              }  

              //List<TrackWrapper> tempList = new List<TrackWrapper>();


    }

    this.RemRace= 12 - this.RaceEntries;
    this.RemDis= 300 - this.TotDis;              
   }

  //Populate the table from the Selected value in selectlist
  public void searchRecords(){
    trackList= new List<Track__c>();
    if( selectValue != null){ //If the selectvalue has a value   
        OffsetSize = 0;                                 
        this.twList= new List<TrackWrapper>(); 
        trackList= Database.query('SELECT Name, Location__c, Difficulty__c, Length__c from Track__c WHERE Difficulty__c LIKE : selectValue LIMIT :LimitSize OFFSET :OffsetSize');             


        for(Track__c t : this.trackList) {
            TrackWrapper tw = new TrackWrapper();
            tw.tra = t;
            tw.isChecked = false;
            this.twList.add(tw);
            }

        //for TotalPage Numbers per selected value 
        Integer totalSelect = database.countQuery('select count() from Track__c WHERE Difficulty__c LIKE : selectValue');   
        Integer totDiv = totalSelect /5;  
        Integer remain = math.mod(totalSelect, 5);   

          if(remain!= 0){
               this.totalPage = totDiv + 1;                
           } else{
               this.totalPage = totDiv;
           }

           currentPage = 1;                  
           buttonDisable();                

    }else if(selectValue == null){ // If the selectvalue is null                             
        this.twList= new List<TrackWrapper>();
        trackList= Database.query('SELECT Name, Location__c, Difficulty__c, Length__c from Track__c LIMIT :LimitSize OFFSET :OffsetSize');          

        for(Track__c t : this.trackList) {
            TrackWrapper tw = new TrackWrapper();
            tw.tra = t;
            tw.isChecked = false;
            this.twList.add(tw);               
         }               

          //for TotalPage Numbers per selected value 
         Integer total = database.countQuery('select count() from Track__c');  
         Integer tot = total/5;  
         Integer rem = math.mod(total, 5);   

          if(rem!= 0){
                this.totalPage = tot + 1;                
           }else{
               this.totalPage = tot;
           }     
           buttonDisable(); 
    }           
    }         

 //for pagination   
public integer totalRecs;
public integer OffsetSize{get;set;}
public integer LimitSize= 5;    

public boolean isFirstPage {get;set;}
public boolean isLastPage {get;set;}    

public Integer currentPage {get;set;}
public Integer totalPage {get;set;}          
public Integer remainder {get;set;}

public void buttonDisable(){
    this.isFirstPage = false;
    this.isLastPage = false;

    if(currentPage == 1){
        this.isFirstPage = true;
    }

    if(currentPage == totalPage) {
        this.isLastPage = true;
    }  
   }

  // PAGINATION  
   public void FirstPage(){
    OffsetSize = 0;
    searchRecords();   
    this.currentPage = 1;      
    buttonDisable();   
   }      

   public void previous(){
    OffsetSize = OffsetSize - LimitSize;       
    searchRecords(); 
    this.currentPage--;    
    buttonDisable();           
   }

   public void next(){        
    OffsetSize = OffsetSize + LimitSize;      
    searchRecords();
    this.currentPage++;                
    buttonDisable();        
    }  

  public void LastPage(){
    totalRecs = [select count() from track__c];
    OffsetSize = totalrecs - math.mod(totalRecs,LimitSize);
    searchRecords();
    this.currentPage = this.totalPage;
    buttonDisable();     
   }  

这是我用过的VisualForce。它还包括表格,选择列表和分页。

 <apex:pageBlockSection title="Season Stats" id="stat">                                                      
            <apex:outputText label="Race Entries" value="{!RaceEntries}" /> 
            <apex:outputText label="Total Distance" value="{!TotDis}"/>
            <apex:outputText label="Remaining Race Entries" value="{!RemRace}"/>
            <apex:outputText label="Remaining Distance" value="{!RemDis}"/>        
        </apex:pageBlockSection>      

        <apex:actionRegion >          
        <apex:pageBlockSection id="details" title="Tracks" columns="1">    
           <apex:outputLabel value="Track Difficulty Level Filter" />         
                <apex:selectList size="1" value="{!selectValue}">                 
                    <apex:selectOption itemValue=""  itemLabel="All" />                                               
                    <apex:selectOptions value="{!statusOptions}" />                                    
                   <apex:actionSupport event="onchange" action="{!searchRecords}" rerender="details" status="waitStatus"/>
                </apex:selectList>    

                <apex:outputPanel id="out">
                    <apex:actionStatus id="waitStatus">    
                        <apex:facet name="start">
                            <apex:outputPanel >
                                <apex:outputText >Updating...</apex:outputText>
                                <apex:image value="https://shop.usa.canon.com/estore/marketing/maxify/redesign/maxify-loader.gif" title="Processing..." height="20px"/>         
                            </apex:outputPanel>
                        </apex:facet>
                    </apex:actionstatus>
                </apex:outputPanel>                          

                <apex:outputPanel ><br/>
                    <apex:pageBlocktable value="{!twList}" var="track" id="table">               
                        <apex:column HeaderValue="Select">                        
                            <apex:inputCheckbox value="{!track.isChecked}">    
                            <apex:actionSupport event="onchange" rerender="stat, messages, btnsave" action="{!Stats}" status="waitStatus"/>
                            </apex:inputCheckbox>                                                                                                                                                                                                                    
                        </apex:column>   
                        <apex:column value="{!track.tra.Name}" HeaderValue="Track Name"/>
                        <apex:column value="{!track.tra.Location__c}" HeaderValue="Location"/>
                        <apex:column value="{!track.tra.Difficulty__c}" HeaderValue="Difficulty"/>
                        <apex:column value="{!track.tra.Length__c}" HeaderValue="Circuit Length (KM)"/> 
                 </apex:pageBlocktable>
               </apex:outputPanel>  

       <!-- Pagination -->
           <center>
              <apex:pageBlockSection columns="1">  
                 <apex:panelGrid columns="6">                
                      <center>
                       <apex:commandButton value="First Page" rerender="details" action="{!FirstPage}" disabled="{!isFirstPage}" />
                        <apex:commandButton value="Previous" rerender="details" action="{!previous}" disabled="{!isFirstPage}"/>
                        Page {!currentPage} of {!totalPage}
                        <apex:commandButton value="Next" rerender="details" action="{!next}" disabled="{!isLastPage}" />
                        <apex:commandButton value="Last Page" rerender="details" action="{!LastPage}" disabled="{!isLastPage}" /> 
                        </center>
                </apex:panelGrid> 
              </apex:pageBlockSection> 
           </center> 
        </apex:pageBlockSection>             
      </apex:actionRegion>             

1 个答案:

答案 0 :(得分:0)

您需要使用Map在分页之前将当前值存储在内存中,然后在分页后从地图中检索值。例如:

Map<Id,trackWrapper> trackMap = new Map<Id,trackWrapper>();

public next(){
  for ( trackWrapper t : twList ){
    trackMap.put( t.tra.id, t);
  }
  //paginate method
  for ( trackWrapper t : twList ){
    if ( trackMap.containsKey(t.tra.id) ){
      t.isChecked = trackMap.get(t.tra.id).isChecked;
    }
  }
}