ads

Wednesday, 17 August 2016

Session Maintain During Pagination In VisualForce Page


Controller Code :-


public with sharing class Session_Maintain_During_Pagination{
 
    /*
    *   item in context from the page
    */
    public String contextItem{get;set;}
 
    /*
    *   set controller
    */
    private ApexPages.StandardSetController setCon;
 
    /*
    *   the opportunity ids selected by the user
    */
    private Set<Id> selectedopportunityIds;
 
    /*
    *   constructor
    */
    public Session_Maintain_During_Pagination()
    {
        //init variable
        this.selectedopportunityIds= new Set<Id>();
 
        //gather data set
        this.setCon= new ApexPages.StandardSetController( [SELECT Id, Name, StageName,probability FROM opportunity] );
        this.setCon.setpageNumber(1);
        this.setCon.setPageSize(10);
 
    }
 
    /*
    *   handle item selected
    */
    public void doSelectItem(){
 
        this.selectedopportunityIds.add(this.contextItem);
 
    }
 
    /*
    *   handle item deselected
    */
    public void doDeselectItem(){
 
        this.selectedopportunityIds.remove(this.contextItem);
 
    }
 
    /*
    *   return count of selected items
    */
    public Integer getSelectedCount(){
 
        return this.selectedopportunityIds.size();
 
    }
 
    /*
    *   advance to next page
    */
    public void doNext(){
 
        if(this.setCon.getHasNext())
            this.setCon.next();
 
    }
 
    /*
    *   advance to previous page
    */
    public void doPrevious(){
 
        if(this.setCon.getHasPrevious())
            this.setCon.previous();
 
    }
 
    /*
    *   return current page of groups
    */
    public List<CCWRowItem> getopportunity(){
 
        List<CCWRowItem> rows = new List<CCWRowItem>();
 
        for(sObject r : this.setCon.getRecords()){
            opportunity c = (opportunity)r;
 
            CCWRowItem row = new CCWRowItem(c,false);
            if(this.selectedopportunityIds.contains(c.Id)){
                row.IsSelected=true;
            }
            else{
                row.IsSelected=false;
            }
            rows.add(row);
        }
 
        return rows;
 
    }
 
    /*
    *   return whether previous page exists
    */
    public Boolean getHasPrevious(){
 
        return this.setCon.getHasPrevious();
 
    }
 
    /*
    *   return whether next page exists
    */
    public Boolean getHasNext(){
 
        return this.setCon.getHasNext();
 
    }
 
    /*
    *   return page number
    */
    public Integer getPageNumber(){
 
        return this.setCon.getPageNumber();
 
    }
 
    /*
    *    return total pages
    */
    Public Integer getTotalPages(){
 
        Decimal totalSize = this.setCon.getResultSize();
        Decimal pageSize = this.setCon.getPageSize();
 
        Decimal pages = totalSize/pageSize;
 
        return (Integer)pages.round(System.RoundingMode.CEILING);
    }
 
    /*
    *   helper class that represents a row
    */
    public with sharing class CCWRowItem{
 
        public opportunity topportunity{get;set;}
        public Boolean IsSelected{get;set;}
 
        public CCWRowItem(opportunity c, Boolean s){
            this.topportunity=c;
            this.IsSelected=s;
        }
 
    }
}


Visualforce Page :-



<apex:page controller="Session_Maintain_During_Pagination">
 
    <script type="text/javascript">
 
        /*
        *    function to handle checkbox selection
        */
        function doCheckboxChange(cb,itemId){
 
            if(cb.checked==true){
                aSelectItem(itemId);
            }
            else{
                aDeselectItem(itemId);
            }
 
        }
 
    </script>
 
    <apex:form >
 
        <!-- handle selected item -->
        <apex:actionFunction name="aSelectItem" action="{!doSelectItem}" rerender="mpb">
            <apex:param name="contextItem" value="" assignTo="{!contextItem}"/>
        </apex:actionFunction>
 
        <!-- handle deselected item -->
        <apex:actionFunction name="aDeselectItem" action="{!doDeselectItem}" rerender="mpb">
            <apex:param name="contextItem" value="" assignTo="{!contextItem}"/>
        </apex:actionFunction>
 
        <apex:pageBlock title="Session Maintain During Pagination" id="mpb">
 
            <!-- table of data -->
            <apex:pageBlockTable title="opportunity" value="{!opportunity}" var="c">
                <apex:column >
                    <apex:facet name="header">Action</apex:facet>
                    <apex:inputCheckbox value="{!c.IsSelected}" onchange="doCheckboxChange(this,'{!c.topportunity.Id}')"/>
                </apex:column>
                <apex:column value="{!c.topportunity.Name}"/>
                <apex:column value="{!c.topportunity.StageName}"/>
                <apex:column value="{!c.topportunity.Probability}"/>
              
            </apex:pageBlockTable>
 <br></br><br></br>
            <!-- count of selected items -->
          <b>  <font size="7" color="#00ff00"> <apex:outputLabel value="[{!selectedCount} records selected]" /></font></b>
 
            <br/><br></br><br></br>
 
            <!-- next, previous and page info -->
            <apex:commandLink action="{!doPrevious}" rendered="{!hasPrevious}" value="Previous" />
            <apex:outputLabel rendered="{!NOT(hasPrevious)}" value="Previous" />
 
            <apex:outputLabel value=" (page {!pageNumber} of {!totalPages}) " />
 
            <apex:commandLink action="{!doNext}" rendered="{!hasNext}" value="Next" />
            <apex:outputLabel rendered="{!NOT(hasNext)}" value="Next" />
 
        </apex:pageBlock>
 
    </apex:form>
 
</apex:page>

Output Screen 1 :-



Output Screen 2 :-



Output Screen 3 :-


No comments:

Post a Comment