lundi 2 octobre 2017

Retain the selected checkbox during pagination

Anyone who have an idea how to retain the selected checkbox during pagination and in selecting another value in selectlist using my codes below. Thank You! :)

Here is the extension I used. It includes the table, selecting values and pagination.

 //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();     
   }  

And this is the VisualForce I used. It also includes the table, selectlist, and pagination.

 <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="http://ift.tt/2yCTaGq" 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>             




Aucun commentaire:

Enregistrer un commentaire