0

I would like to implement pagination on my searchResults table but I have problems with changing the page, because default StandardSetController next(), last() etc. functions don't work...

Could you look at this and help me resolve this problem?

Below is my code:

public with sharing class SMS_ShopSearchingController {

public Integer noOfRecords { get; set; }
public Integer size { get; set; }
public SMS_Shop__c smsShop {
    get {
        if (this.smsShop == null) {
            this.smsShop = new SMS_Shop__c();
        }return smsShop;
    }
    set;
}

public ApexPages.StandardSetController setCon {
    get {
         size = 3;
        string queryString = SMS_ShopSearchUtils.prepareQuery(this.smsShop);
         setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
        setCon.setPageSize(size);
        noOfRecords = setCon.getResultSize();
        return setCon;
    }
    set;
}

public void setShop(){
    //only rerenders records
}

public List<SMS_Shop__c> getSmsShops() {
    List<SMS_Shop__c> smsShops = new List<SMS_Shop__c>();
if(setCon.getRecords()!= null)
    for(SMS_Shop__c shop : (List<SMS_Shop__c>)setCon.getRecords())
        smsShops.add(shop);
    System.debug('Invoked by button==>' + setCon.getRecords());
    return smsShops;
}


public pageReference refresh() {
    setCon = null;
    return null;
}

public void first() {
    setCon.first();
}

public void last() {
    setCon.last();
}

public void previous() {
    setCon.previous();
}

public void next() {
    setCon.next();
     }
 }

SMS_ShopSearchUtils.prepareQuery(smsShop) only prepares the query, which is returning right result (I'd checked that).

VF PAGE:

 <apex:page id="SMS_ShopSearching" controller="SMS_ShopSearchingController">
    <apex:form>
        <apex:pageBlock id="pb">

<apex:page id="SMS_ShopSearching" controller="SMS_ShopSearchingController">
    <apex:form>
        <apex:pageBlock id="pb">
            <apex:pageBlockSection id="formSection" title="{!$Label.Search_Form}" columns="2">
                <apex:inputField id="nameInput" value="{!smsShop.Previous_Name__c}"/>
                <apex:inputField value="{!smsShop.City__c}"/>
                <apex:inputField value="{!smsShop.Email__c}"/>
                <apex:inputField value="{!smsShop.Country__c}" required="true"/>
                <apex:commandButton action="{!setShop}" value="{!$Label.Search}" rerender="shopTable, paginationPanel, pb"/>
            </apex:pageBlockSection>

            <apex:pageBlockSection id="results" columns="1">
                <apex:pageBlockTable value="{!smsShops}" var="a" id="shopTable">
                    <apex:column value="{!a.Previous_Name__c}"/>
                    <apex:column value="{!a.Email__c}"/>
                    <apex:column value="{!a.Country__c}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:panelGrid columns="7" id="paginationPanel">
                <apex:commandButton status="fetchStatus" reRender="shopTable" value="|<" action="{!first}"
                                    disabled="{!!setCon.hasPrevious}" title="First Page"/>
                <apex:commandButton status="fetchStatus" reRender="shopTable" value="<" action="{!previous}"
                                    disabled="{!!setCon.hasPrevious}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="shopTable, paginationPanel, pb" value=">"
                                    action="{!next}"
                                    disabled="{!!setCon.hasNext}" title="Next Page"/>
                <apex:commandButton status="fetchStatus" reRender="shopTable, results" value=">|" action="{!last}"
                                    disabled="{!!setCon.hasNext}" title="Last Page"/>
                <apex:outputText>{!(setCon.pageNumber * size)+1-size}
                    -{!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,(setCon.pageNumber * size))}
                    of {!noOfRecords}</apex:outputText>
                <apex:commandButton status="fetchStatus" reRender="pb" value="Refresh" action="{!refresh}"
                                    title="Refresh Page"/>
                <apex:outputPanel style="color:#4AA02C;font-weight:bold">
                    <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
                </apex:outputPanel>
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.