function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex YhapAlex Yhap 

Update a non Sobject List from Apex class

I have a dropbox of numbers of records per page. When I select a value the assignPerPage PageReference method is called. If you look at the Visualforce page code, the apex:pageBlockTable calls the currentPage List<wrapPayment>. The desired outcome is when the assignPerPage is changed the pageBlockTable is to be updated, changing the pageSize from 10 to whatever is selected. 

My question is how would I update the list or reevaluate the list to output the correct number of records per page.

User-added image
<apex:tab label="To Be Claimed" name="name1" id="tabOne">
            <apex:pageMessages />
            <apex:form>
                <apex:pageBlock id="toBeID" title="Balance: £{!sumToBeClaimed}" >
                    <apex:pageBlockButtons location="top">
                        <apex:commandButton value="Process Selected" action="{!processSelected}" style="float:right;" rendered="{!IF(toBeTotalRecs==0,false,true)}"/>
                    </apex:pageBlockButtons>
                    <apex:pageBlock>
                        <div style="display:inline;">
                            <apex:outputText value=" {!pageNumber * pageSize} - {!(pageNumber * pageSize) + pageSize} of {!toBeTotalRecs} Results"/>
                        </div>
                        <div style="display:inline; float:right;">
                           Results per page: 
                            <apex:selectList value="{!perPageSelection}" size="1">
                                <apex:selectOptions value="{!perPageNumbers}"></apex:selectOptions>
                                <apex:actionSupport event="onchange" action="{!assignPerPage}" reRender="toBeID"/>
                            </apex:selectList>
                        </div>
                    </apex:pageBlock>

                    <apex:pageBlockTable value="{!currentPage}" var="claimed" id="theaddrs" styleClass="tablesorter" headerClass="header">
                        <apex:column >
                            <apex:facet name="header">
                                <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                            </apex:facet>
                                <apex:inputCheckbox id="inputId" value="{!claimed.selected}"/>
                        </apex:column>
                        <div id="{!claimed.pay.Id}">
                            <apex:column headerValue="Payment No">
                                <apex:outputLink value="{!$Site.Domain}/{!claimed.pay.Id}">{!claimed.pay.Name}</apex:outputLink>
                            </apex:column>
                            <apex:column value="{!claimed.pay.causeview__Constituent__c}" headerValue="Donor"/>
                            <apex:column value="{!claimed.pay.causeview__Date__c}" headerValue="Gift Date" styleClass="header"/>
                            <apex:column value="{!claimed.pay.Gift_Aid_Amount__c}" headerValue="Gift-Aid Amount"/>
                            <apex:column value="{!claimed.pay.Gift_Aid_Declaration__c}" headerValue="Gif-Aid Declaration"/>
                        </div>
                    </apex:pageBlockTable>
                    <apex:pageBlockButtons location="bottom" rendered="{!IF(toBeTotalRecs==0,false,true)}">
                        <apex:commandButton value="First" action="{!first}" rendered="{!isFirst}" rerender="toBeID"/>
                        <apex:commandButton value="Previous" action="{!previousPage}" rendered="{!hasPrevious}" rerender="toBeID"/>
                        <apex:commandButton value="Process Selected" action="{!processSelected}" rendered="false" rerender="toBeID"/>
                        <apex:commandButton value="Next" action="{!nextPage}" rendered="{!hasNext}" rerender="toBeID"/>
                        <apex:commandButton value="Last" action="{!last}" rendered="{!isLast}" rerender="toBeID"/>
                    </apex:pageBlockButtons>

                </apex:pageBlock>
            </apex:form>
        </apex:tab>

public Integer pageSize { get; set; }

    public List<wrapPayment> wrapPaymentList        //  Our collection of the class/wrapper objects wrapPayment
    {
        get
        {
            if ( wrapPaymentList == null )
            {
                wrapPaymentList = new List<wrapPayment>();
                //for ( causeview__Payment__c p : [SELECT Id, causeview__Constituent__c, Gift_Aid_Declaration__c, Name, causeview__Date__c, Gift_Aid_Amount__c, Gift_Aid_Claim_Status__c FROM causeview__Payment__c WHERE Gift_Aid_Claim_Status__c = 'Not claimed' AND Gift_Aid_Elegible__c = true ORDER BY causeview__Date__c DESC] )
                for ( causeview__Payment__c p : [SELECT ID, Name, Gift_Aid_Claim_Status__c, House_Number__c, First_Name__c, Last_Name__c, Postal_Code__c, causeview__Constituent__c, Gift_Aid_Declaration__c, causeview__Date__c, Gift_Aid_Amount__c FROM causeview__Payment__c WHERE Gift_Aid_Claim_Status__c = 'Not claimed' AND Gift_Aid_Elegible__c = true ORDER BY causeview__Date__c DESC] )

                {   //  As each contact is processed we create a new cContact object
                    //  and add it to the contactList
                    wrapPaymentList.add( new wrapPayment( p ) );
                }
            }
            return wrapPaymentList;
        }
        private set;
    }

public Integer pageNumber { get; set; }

public Integer numberOfPages { get; set; }

private List<List<wrapPayment>> list_Pages
    {
        get
        {
            if ( list_Pages == null )
            {
                list_Pages = new List<List<wrapPayment>>();
                Integer numInPage = pageSize;
                List<wrapPayment> thePage;

                if(wrapPaymentList.size() > 0) {
                    for ( wrapPayment pPay : wrapPaymentList )
                    {
                        if ( numInPage >= pageSize )
                        {
                            thePage = new List<wrapPayment>();
                            list_Pages.add( thePage );
                            numInPage = 0;
                        }
                        thePage.add( pPay );
                        numInPage++;
                    }
                }
            }
            if(list_Pages.size() >0){numberOfPages = list_Pages.size() - 1;}

            System.Debug('list_Pages: '+list_Pages);
            return list_Pages;
        }
        private set;
    }

    public List<wrapPayment> currentPage  { get {

        If(list_Pages!=null && list_Pages.size() > 0){
            return list_Pages[ pageNumber ];
        }
        else{
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'No record to be claimed.');
            ApexPages.addMessage(myMsg);
            return null;
        }

    }}

    public PageReference assignPerPage() {
        pageSize = perPageSelection;
        return null;
    }
Best Answer chosen by Alex Yhap
Alex YhapAlex Yhap
hey pcon et al,

the solution was to simply null the List holding the data, that would then reevalutae the List.
 
public void assignPerPage() {
        pageSize = Integer.valueOf(perPageSelection);
        wrapPaymentList = null;
        list_Pages  = null;
    }

pcon - thanks for your help!

All Answers

pconpcon
What you have should be correct.  You want to have a rerender on your table and trigger that rerender when the dropbox value is changed.  I do not see where in your controller you set and use your perpageselection variable.
Alexander YhapAlexander Yhap
Hi pcon,

I set this value in the default constructor...is this best practice?
public Controller_GiftAid(){
        Filetype = '';
        lstwrapper = new List<wrapper>();
        header = 'First Name, Last Name, House Number, Postal Code, Gift Date Amount, Payment Date\r\n';

        pageSize = 10;

        //To be claimed

        sumToBeClaimed = (double)[SELECT SUM(Gift_Aid_Amount__c)sum
                FROM causeview__Payment__c
                WHERE Gift_Aid_Claim_Status__c = 'Not claimed'][0].get('sum');

        toBeTotalRecs = 0;

        pageNumber = 0;

        toBeTotalRecs = [SELECT count() FROM causeview__Payment__c WHERE Gift_Aid_Claim_Status__c = 'Not claimed' AND Gift_Aid_Elegible__c = true];

        if(toBeTotalRecs < tempTotal){
            redirectApex();
        }

        System.debug(currentPage);
        wrapPaymentList = null;
        list_Pages  = null;

        if(toBeTotalRecs <= 10){
            perPageNumbers = new list<selectoption>();
            perPageNumbers.add(new selectoption(String.valueOf(toBeTotalRecs),'Showing All'));
        }
        else{
            perPageNumbers = new list<selectoption>();
            perPageNumbers.add(new selectoption('10','10'));
            perPageNumbers.add(new selectoption('25','25'));
            perPageNumbers.add(new selectoption('50','50'));
            perPageNumbers.add(new selectoption(String.valueOf(toBeTotalRecs),'Show All'));
        }

 
pconpcon
So you have the following line in your Visualforce page
 
<apex:selectList value="{!perPageSelection}" size="1">

When you change your selectList and pick a new number (let's say 25) the page size will be stored in that variable.  You are using a different variable pageSize to determine the number of results to return.  You should get the Integer.valueOf your perPageSelection variable and either store that in pageSize or just discard pageSize completely and only use the Integer.valueOf the perPageSelection variable everywhere.  You will need to cast it to a String in your constructor to set the default however.
Alex YhapAlex Yhap
hey pcon et al,

the solution was to simply null the List holding the data, that would then reevalutae the List.
 
public void assignPerPage() {
        pageSize = Integer.valueOf(perPageSelection);
        wrapPaymentList = null;
        list_Pages  = null;
    }

pcon - thanks for your help!
This was selected as the best answer