0

how would this work with a dynamic search page I have a visual force search page and I have used the stadardsetcontroller for pagination. The initial query during page load is working for me. Subsequent queries are not executed. How do we rerun the query in the controller. I thought we could just use the getAccounts() method after setting the controller object to null.

Apex

public with sharing class Pagination {
    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}
    public Account filterAccount{get;set;}


    public pagination()
    {
        filterAccount = new Account();

    }
    public ApexPages.StandardSetController setCon {
        get{
            if(setCon == null){
                size = 10;
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator([Select Name, 
                    Type, Website, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry  
                    from Account Limit 1000]));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();            
            }
            return setCon;
        }set;
    }


    Public List<Account> getAccounts(){
        List<Account> accList = new List<Account>();
        for(Account a : (List<Account>)setCon.getRecords())
            accList.add(a); 

        return accList;
    }

    public pageReference refresh() {
        setCon = null;
        getAccounts();
        setCon.setPageNumber(1);
        return null;
    }

    public Boolean hasNext {
        get {
            return setCon.getHasNext();
        }
        set;
    }
    public Boolean hasPrevious {
        get {
            return setCon.getHasPrevious();
        }
        set;
    }

    public Integer pageNumber {
        get {
            return setCon.getPageNumber();
        }
        set;
    }

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

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

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

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

VF:

<apex:page controller="Pagination" sidebar="false">
 <apex:form >



        <apex:outputPanel style="display: block" id="pwPanel">

          <apex:pageBlock >

               <apex:inputfield value="{!filterAccount.Type}" label="Account Type"> 
                 <apex:actionsupport event="onchange" action="{!refresh}" rerender="pwPanel"/>                  
               </apex:inputfield>

          <apex:outputPanel rendered="{!accounts.size>0}" id="list">

            <apex:pageBlockTable value="{!Accounts}" var="rl">
                            <apex:column headerValue="Account Name" value="{!rl.Name}">           
                            </apex:column>

                            <apex:column headerValue="Type">
                                <apex:outputField value="{!rl.Type}"/>
                            </apex:column>

                            <apex:column headerValue="Website">
                                <apex:outputField value="{!rl.Website}"/>
                            </apex:column>

                            <apex:column headerValue="Billing Street">
                                <apex:outputField value="{!rl.BillingStreet}"/>
                            </apex:column>

                            <apex:column headerValue="Billing City">
                                <apex:outputField value="{!rl.BillingCity}"/>
                            </apex:column>

                            <apex:column headerValue="Billing State">
                                <apex:outputField value="{!rl.BillingState}"/>
                            </apex:column>

                            <apex:column headerValue="Billing Zip/Postal Code">
                                <apex:outputField value="{!rl.BillingPostalCode}"/>
                            </apex:column>

                            <apex:column headerValue="Billing Country">
                                <apex:outputField value="{!rl.BillingCountry}" />
                            </apex:column>

            </apex:pageBlockTable>
            <apex:panelGrid columns="7">
                <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!first}" disabled="{!!hasPrevious}" title="First Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!previous}" disabled="{!!hasPrevious}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!next}" disabled="{!!hasNext}" title="Next Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">|" action="{!last}" disabled="{!!hasNext}" title="Last Page"/>
                <apex:outputText >{!(pageNumber * size)+1-size}-{!IF((pageNumber * size)>noOfRecords, noOfRecords,(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:outputPanel>
        </apex:pageBlock>
      </apex:outputPanel>
    </apex:form>


</apex:page>
0

The problem is that when you clear the StandardSetController (setCon = null, line 36) and then access it again (setCon.getRecords(), line 29), the same initial query is executed again (line 16). This is not what you intended; you should initialize the standard controller, then initialize it again when you call getAccounts(). Also, your "reRender" attributes don't reference a valid element, so nothing is reRendered. Finally, your code can be heavily optimized. My optimizations are below.

Notes

  • apex:column is smart enough to use a field label when referencing a field.
  • setCon methods can be called directly, avoiding the need to do so in Apex.
  • rerender attribute must point to a valid ID, or nothing will be updated.

Apex

public class pagination {
    public Account[] accounts { get { return setCon.getRecords(); } }
    public ApexPages.StandardSetController setCon { get; set; }
    public Account filterAccount { get; set; }

    // Default constructor
    public pagination() {
        filterAccount = new Account();
        refresh();
    }
    public void refresh() {
        if(filterAccount.Type == null) {
            // No type selected
            setCon = new ApexPages.StandardSetController([
                SELECT Name, Type, Website, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry
                FROM Account
                LIMIT 10000
            ]);
        } else {
            // Find records with given type
            setCon = new ApexPages.StandardSetController([
                SELECT Name, Type, Website, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry
                FROM Account
                WHERE Type = :filterAccount.Type
                LIMIT 10000
            ]);
        }
        setCon.setPageSize(10);
    }

}

Visualforce

<apex:page controller="pagination" sidebar="false">
    <apex:form>
        <apex:outputPanel style="display: block" id="pwPanel">
            <apex:pageBlock>
                <apex:inputfield value="{!filterAccount.Type}" label="Account Type">
                    <apex:actionsupport event="onchange" action="{!refresh}" rerender="pwPanel" />
                </apex:inputfield>
                <apex:outputPanel rendered="{!setCon.pageSize>0}" id="list">
                    <apex:pageBlockTable value="{!accounts}" var="rl">
                        <apex:column value="{!rl.Name}" />
                        <apex:column value="{!rl.Type}" />
                        <apex:column value="{!rl.Website}" />
                        <apex:column value="{!rl.BillingStreet}" />
                        <apex:column value="{!rl.BillingCity}" />
                        <apex:column value="{!rl.BillingState}" />
                        <apex:column value="{!rl.BillingPostalCode}" />
                        <apex:column value="{!rl.BillingCountry}" />
                    </apex:pageBlockTable>
                    <apex:panelGrid columns="7">
                        <apex:commandButton status="fetchStatus" reRender="pwPanel" value="|<" action="{!setCon.first}"
                            disabled="{!!setCon.hasPrevious}" title="First Page" />
                        <apex:commandButton status="fetchStatus" reRender="pwPanel" value="<"
                            action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page" />
                        <apex:commandButton status="fetchStatus" reRender="pwPanel" value=">" action="{!setCon.next}"
                            disabled="{!!setCon.hasNext}" title="Next Page" />
                        <apex:commandButton status="fetchStatus" reRender="pwPanel" value=">|" action="{!setCon.last}"
                            disabled="{!!setCon.hasNext}" title="Last Page" />
                        <apex:outputText>
                            {!(setCon.pageNumber * setCon.pageSize)+1-setCon.pageSize}
                            -
                            {!MIN(setCon.pageNumber * setCon.pageSize, setCon.resultSize)} of {!setCon.resultSize}
                        </apex:outputText>
                        <apex:commandButton status="fetchStatus" reRender="pwPanel" 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:outputPanel>
            </apex:pageBlock>
        </apex:outputPanel>
    </apex:form>
</apex:page>
  • Thanks for the help! The reason I used outputField is because inlineEdit does not work on columns:/ – Tyler Aug 2 '19 at 14:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.