2

I have Wraper class list which holds list of Records form 2 objects. This list Iam using in Page block table to show the data from both objects. Now I have to work on Pagination. For Pagination we have to use Apex StandardSetController in order to use its predefine methods. Problem: But While instantinating StandardSetController we need to pass SObject List. Butt in my case its Wrapper class list. Can any one suggest on this problem witn pagination?

2

1 Answer 1

3

You could create your own paginatation, this blog here walks you through writing your own pagination class:

/**This Paginate Class is the class that keeps track of all the pagination information */
    public class Paginate {
    public Integer index {get;set;}
    public Integer pageNum {get;set;}
    public Integer totalPage {get;set;}
    public Integer totalResults {get;set;}
    public List<Pager> pageList {get;set;}
    public Boolean showPrevious {get;set;}
    public Boolean showNext {get;set;}
    public Boolean isAll {get;set;}

    public Paginate(Integer listSize, Integer pageSize){
        this.totalResults = listSize;
        pageSize = (pageSize > 0)?pageSize:10;
        this.totalPage = (Integer)Math.ceil((Double)listSize / (Double)pageSize);
        this.pageNum = 1;
        this.index = 0;
        this.isAll = (listSize == pageSize)?true:false;
        updateNumbers();
    }

    public void updateNumbers(){
        this.pageList = new List<Pager>();
        for(Integer i = 1; i < (this.totalPage + 1); i++){
            Boolean b = (i == pageNum)?true:false;
            this.pageList.add(new Pager(i, b));
        }
        this.showPrevious = (this.pageNum == 1)?false:true;
        this.showNext = (this.pageNum == this.totalPage)?false:true;
        this.index = this.pageNum - 1;
    }

    public void decrement(){
        this.pageNum = this.pageNum - 1;
        updateNumbers();
    }

    public void increment(){
        this.pageNum++;
        updateNumbers();
    }

    public class Pager{
        public Integer pageNum {get;set;}
        public Boolean isActive {get;set;}

        public Pager(Integer num, Boolean active){
            this.pageNum = num;
            this.isActive = active;
        }
    }
}  

controller example:

/**This is a sample controller with sample data of how to use the
    Paginate class*/
public with sharing class PaginationDemoController {
    public Paginate paginater {get;set;}
    public List<CustomClass> genericList{get;set;}
    public List<List<CustomClass>> fullGenericList{get;set;}

    public PaginationDemoController(){
        //Get the data we need to paginate
        List<CustomClass> resultsList = populateData();

        //Set the page size
        Integer pageSize = 10;

        //Create a new instance of Paginate passing in the overall size of
        //the list of data and the page size you want
        this.paginater = new Paginate(resultsList.size(), pageSize);

        //These lists hold the data
        this.fullGenericList = new List<List<CustomClass>>();
        this.genericList = new List<CustomClass>();

        //Break out the full list into a list of lists
        if(resultsList.size() > 0){
            List<CustomClass> tempCC = new List<CustomClass>();        
            Integer i = 0;
            for(CustomClass cc : resultsList){
                tempCC.add(cc);
                i++;
                if(i == pageSize){
                    this.fullGenericList.add(tempCC);
                    tempCC = new List<CustomClass>();
                    i = 0;
                }
            }
            if(!tempCC.isEmpty()){
                this.fullGenericList.add(tempCC);
            }

            //Gets the correct list of data to show on the page
            this.genericList = this.fullGenericList.get(this.paginater.index);
        }
    }

    public PageReference previousPage(){
        this.paginater.decrement();
        return changeData();
    }

    public PageReference nextPage(){
        this.paginater.increment();
        return changeData();
    }

    public PageReference updatePage(){
        this.paginater.updateNumbers();
        return changeData();
    }

    public PageReference changeData(){
        this.genericList = this.fullGenericList.get(this.paginater.index);
        return null;
    }

    public List<CustomClass> populateData(){
        List<CustomClass> customClassList = new List<CustomClass>();
        for(Integer i = 1; i < 50; i++){
            customClassList.add(new CustomClass(i, 'Name:  ' + String.valueOf(i)));
        }
        return customClassList;
    }

    public class CustomClass{
        public Integer num{get;set;}
        public String name{get;set;}

        public CustomClass(Integer num, String name){
            this.num = num;
            this.name = name;
        }
    }
}  

page example:

<apex:page controller="PaginationDemoController">
    <apex:form >
        <apex:outputPanel id="mainPanel">
            <apex:outputPanel rendered="{!AND(IF(paginater.isAll, false, true), IF(paginater.totalPage == 1, false, true))}">
                <apex:outputText rendered="{!paginater.showPrevious}">
                    <apex:commandLink action="{!previousPage}" value="Prev" rerender="mainPanel" />
                </apex:outputText>
                <apex:repeat value="{!paginater.pageList}" var="pl">
                    <apex:outputText rendered="{!!pl.isActive}">
                        <apex:commandLink action="{!updatePage}" rerender="mainPanel">
                            {!pl.pageNum}                              
                            <apex:param name="selectedPage" value="{!pl.pageNum}" assignTo="{!paginater.pageNum}" />
                        </apex:commandLink>
                    </apex:outputText>
                    <apex:outputText rendered="{!pl.isActive}" value="{!pl.pageNum}" />

                </apex:repeat>
                <apex:outputText rendered="{!paginater.showNext}">
                    <apex:commandLink action="{!nextPage}" value="Next" rerender="mainPanel" />
                </apex:outputText>
            </apex:outputPanel>
            <br/>
            <apex:outputPanel id="dataPanel">
                <apex:repeat value="{!genericList}" var="gl">
                    {!gl.num}   <strong>{!gl.name}</strong><br/>            
                </apex:repeat>                       
            </apex:outputPanel>
        </apex:outputPanel>
    </apex:form>
</apex:page>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .