2

Hi I am trying to implement sorting and pagination in visualforce. I am using the standardsetcontroller but for some reason the standardsetcontroller methods do not fire the actions when clicked on. Below is my apex controller as well as my visualforce markup. Will be glad for any suggestion on this. Thanks.

 public with sharing class casesortingandpagination {
        public String[] countries = new String[]{};
        public String temp {get; set;}
        public list<case> temp1 {get; set;}
        public id uid;
        public id creator;
        public id actid;
        public String contIdChosen {get; set;}
        public String soql {get;set;}
        public List <case> caseList1 = New List <case>();
        public String soqlsort {get;set;}
        public List <case> caseList2 = New List <case>();
        public List <case> caseList3 = New List <case>();

        public casesortingandpagination(){
        this.uid = UserInfo.getUserid();
        this.actid = [select accountid from user where id = :uid][0].accountid;


         if (temp != null){
             this.temp = temp;

         }else{ 
            this.temp = 'e';
        }

        getItems();
        getCaseList2();

    }
    public List<SelectOption> getItems() {
                List<SelectOption> options = new List<SelectOption>();
                options.add(new SelectOption('a','All Closed Cases'));
                options.add(new SelectOption('b','All Open Case'));
                options.add(new SelectOption('c','My Closed Cases'));
                options.add(new SelectOption('d','My Open Case'));
                options.add(new SelectOption('e','Recently Viewed Cases'));
            return options;
    }

    public pageReference getCaseList2(){
        this.temp = temp;
        getCaseList();                      
        return null;
    }   


    // List used in to display the table in VF page.
    public List<case> getCaseList() {
        // Passing the values of list to VF page.                    
        return con.getRecords();
    }

    public ApexPages.StandardSetController con {
         get {

              if(temp == 'a') {
                  // String Query to have a list of cases for a respective End-user.
                 soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
                  System.debug('temp a is: ' + temp);
                 // Passing the String array to a list with Selected field sorting.
                 caseList1 = Database.query(soql + ' where status = \'Closed\' order by ' + sortField + ' ' + sortDir ); 

                 // setting values of List in StandardSetController.
                 con = new ApexPages.StandardSetController(caseList1);

                // sets the number of records in each page set

               }else if(temp == 'b') {
                // String Query to have a list of cases for a respective End-user.
                soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
                System.debug('temp b is: ' + temp);
               // Passing the String array to a list with Selected field sorting.
               caseList1 = Database.query(soql + ' where status != \'Closed\' order by ' + sortField + ' ' + sortDir ); 

               // setting values of List in StandardSetController.
               con = new ApexPages.StandardSetController(caseList1);

               // sets the number of records in each page set

           }else if(temp == 'c') {
            // String Query to have a list of cases for a respective End-user.
            soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
           System.debug('temp c is: ' + temp);
           // Passing the String array to a list with Selected field sorting.
           caseList1 = Database.query(soql + ' where CreatedById = :uid and status = \'Closed\' order by ' + sortField + ' ' + sortDir ); 

           // setting values of List in StandardSetController.
           con = new ApexPages.StandardSetController(caseList1);

           // sets the number of records in each page set

           }else if(temp == 'd') {
             // String Query to have a list of cases for a respective End-user.
             soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
            System.debug('temp d is: ' + temp);
           // Passing the String array to a list with Selected field sorting.
           caseList1 = Database.query(soql + ' where CreatedById =  :uid and status != \'Closed\' order by ' + sortField + ' ' + sortDir ); 

           // setting values of List in StandardSetController.
           con = new ApexPages.StandardSetController(caseList1);

           // sets the number of records in each page set

            }else if(temp == 'e') {
            // String Query to have a list of cases for a respective End-user.
        soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
            System.debug('temp e is: ' + temp);
            // Passing the String array to a list with Selected field sorting.
            caseList1 = Database.query(soql + ' where CreatedById =  :uid AND LastViewedDate != NULL order by LastViewedDate DESC, ' + sortField + ' ' + sortDir ); 

           // setting values of List in StandardSetController.
           con = new ApexPages.StandardSetController(caseList1);

           // sets the number of records in each page set

       }
       con.setPageSize(10);
       return con;
        }
        set;
    }

    public Boolean hasNext {
        get {

            return con.getHasNext();
        }
        set;
    }

    public Boolean hasPrevious {
        get {

            return con.getHasPrevious();
        }
        set;
    }
    public Integer pageSize {
        get {

            return con.getPageSize();
        }
        set;
    }
    Public Integer getTotalPages(){

        Decimal totalSize = this.Con.getResultSize();
        Decimal pageSize = this.Con.getPageSize();

        Decimal pages = totalSize/pageSize;

        return (Integer)pages.round(System.RoundingMode.CEILING);
    }
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }
    public void first() {
        this.con.first();
    }

    // returns the last page of records
    public void last() {
        this.con.last();        
    }

    // returns the previous page of records
    public void previous() { 
        this.con.previous();
    }

    // returns the next page of records
    public void next() {
       this.con.next();
    }

    // returns the PageReference of the original page, if known, or the home page.
    public void cancel() {
        con.cancel();
    } 
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';

                                // run the query again for sorting other columns
                                soqlsort = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case'; 

                                // Adding String array to a List array
                                caseList2 = Database.query(soqlsort + ' order by ' + sortField + ' ' + sortDir ); 

                                // Adding Caselist to Standard Pagination controller variable caselist2
                                con = new ApexPages.StandardSetController(caseList1);

                                // Set Page Size to 5
                                con.setPageSize(10);

    }
    public String sortDir {
        // To set a Direction either in ascending order or descending order.
                                get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;}
        set;
    }

    // the current field to sort by. defaults to last name
    public String sortField {
        // To set a Field for sorting.
                                get  { if (sortField == null) {sortField = 'CaseNumber'; } return sortField;  }
        set;
    } 
}

Visualforce markup:

<apex:page controller="casesortingandpagination" showHeader="false" sidebar="false" >
    <apex:form styleClass="w25" >
    <apex:selectList id="selected_list" value="{!temp}" required="false" size="1">
        <apex:selectOptions value="{!items}" />
        <apex:actionSupport event="onchange" reRender="form" action="{!getCaseList2}"     oncomplete="getCaseList2();" />    
    </apex:selectList>
   </apex:form>
   <apex:form id="form">

    <apex:pageBlock id="pgblock" >
        <apex:pageBlockSection title="Case Details -  Page #{!pageNumber}" columns="1" collapsible="false" rendered="{!TotalPages > 0}">   

            <apex:pageBlockTable value="{!CaseList}" var="CsList" title="Click Column Header for Sorting" headerClass="FCenter" >

                <apex:column >
                    <apex:facet name="header">
                       <apex:commandLink value="Case Number" action="{!toggleSort}" rerender="pgblock">
                            <!-- Value attribute should have field (API Name) to sort in asc or desc order -->
                            <apex:param name="sortField" value="CaseNumber" assignTo="{!sortField}"/>
                       </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!CsList.CaseNumber}"/>
                </apex:column> 

                <apex:column >
                    <apex:facet name="header">
                       <apex:commandLink value="Created Date" action="{!toggleSort}" rerender="pgblock">
                            <!-- Value attribute should have field (API Name) to sort in asc or desc order -->
                            <apex:param name="sortField" value="CreatedDate" assignTo="{!sortField}"/>
                       </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!CsList.CreatedDate}"/>
                </apex:column> 

                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Requested_By__c" action="{!toggleSort}" rerender="pgblock">
                            <apex:param name="sortField" value="Requested_By__c" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!CsList.Requested_By__c}"/>
                </apex:column>

                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Subject" action="{!toggleSort}" rerender="pgblock">
                            <apex:param name="sortField" value="Subject" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!CsList.Subject}"/>
                </apex:column>

                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Status" action="{!toggleSort}" rerender="pgblock">
                            <apex:param name="sortField" value="Status" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!CsList.Status}"/>
                </apex:column>

                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Priority" action="{!toggleSort}" rerender="pgblock">
                            <apex:param name="sortField" value="Priority" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!CsList.Priority}"/>
                </apex:column>

                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Owner Id" action="{!toggleSort}" rerender="pgblock">
                            <apex:param name="sortField" value="OwnerId" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!CsList.OwnerId}"/>
                </apex:column>

                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink value="Type" action="{!toggleSort}" rerender="pgblock">
                            <apex:param name="sortField" value="Type" assignTo="{!sortField}"/>
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputField value="{!CsList.Type}"/>
                </apex:column>

            </apex:pageBlockTable>
        </apex:pageBlockSection>

        <apex:outputtext styleclass="detailText" value="Page {!pageNumber} of {!TotalPages}" rendered="{!TotalPages > 0}"></apex:outputtext>
        <apex:panelGrid columns="4"> 
            <apex:commandLink action="{!first}" value="First"></apex:commandlink>
            <apex:commandLink action="{!previous}">Previous</apex:commandlink>
            <apex:commandLink action="{!next}">Next</apex:commandlink>
            <apex:commandLink action="{!last}" value="Last"></apex:commandlink>                
       </apex:panelGrid>

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

1

sw6, I think the modified code below will address the problem, primarily by fixing the issue with con being reset every time the property's getter is called.

casesortingandpagination class

public with sharing class casesortingandpagination {
    public String[] countries = new String[]{};
    public String temp {get; set;}
    public list<case> temp1 {get; set;}
    public id uid;
    public id creator;
    public id actid;
    public String contIdChosen {get; set;}
    public String soql {get;set;}
    public List <case> caseList1 = New List <case>();
    public String soqlsort {get;set;}
    public List <case> caseList2 = New List <case>();
    public List <case> caseList3 = New List <case>();

    public casesortingandpagination(){
        this.uid = UserInfo.getUserid();
        this.actid = [select accountid from user where id = :uid][0].accountid;

        if (temp != null){
            this.temp = temp;
        } else {
            this.temp = 'e';
        }

        refresh();
        getItems();
        getCaseList2();
    }

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('a','All Closed Cases'));
        options.add(new SelectOption('b','All Open Case'));
        options.add(new SelectOption('c','My Closed Cases'));
        options.add(new SelectOption('d','My Open Case'));
        options.add(new SelectOption('e','Recently Viewed Cases'));
        return options;
    }

    public pageReference getCaseList2(){
        this.temp = temp;
        getCaseList();                      
        return null;
    }   

    // List used in to display the table in VF page.
    public List<case> getCaseList() {
        // Passing the values of list to VF page.                    
        return con.getRecords();
    }

    public ApexPages.StandardSetController con {
        get;
        set;
    }

    public Boolean hasNext {
        get { return con.getHasNext(); }
        set;
    }

    public Boolean hasPrevious {
        get { return con.getHasPrevious(); }
        set;
    }

    public Integer pageSize {
        get { return con.getPageSize(); }
        set;
    }

    Public Integer getTotalPages(){

        Decimal totalSize = this.Con.getResultSize();
        Decimal pageSize = this.Con.getPageSize();

        Decimal pages = totalSize/pageSize;

        return (Integer)pages.round(System.RoundingMode.CEILING);
    }

    public Integer pageNumber {
        get { return con.getPageNumber(); }
        set;
    }
    public void first() {
        this.con.first();
    }

    // returns the last page of records
    public void last() {
        this.con.last();        
    }

    // returns the previous page of records
    public void previous() { 
        this.con.previous();
    }

    // returns the next page of records
    public void next() {
        this.con.next();
    }

    // returns the PageReference of the original page, if known, or the home page.
    public void cancel() {
        con.cancel();
    } 
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';

        // run the query again for sorting other columns
        //soqlsort = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case'; 

        // Adding String array to a List array
        //caseList2 = Database.query(soqlsort + ' order by ' + sortField + ' ' + sortDir ); 

        // Adding Caselist to Standard Pagination controller variable caselist2
        //con = new ApexPages.StandardSetController(caseList1);

        // Set Page Size to 5
        //con.setPageSize(10);

        refresh();
    }

    public String sortDir {
        // To set a Direction either in ascending order or descending order.
        get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;}
        set;
    }

    // the current field to sort by. defaults to last name
    public String sortField {
        // To set a Field for sorting.
        get  { if (sortField == null) {sortField = 'CaseNumber'; } return sortField;  }
        set;
    }

    /*
     * Refresh the data on the screen by re-constructing and initializng
     * the set controller
     */
    public void refresh() {
        if(temp == 'a') {
            // String Query to have a list of cases for a respective End-user.
            soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
            System.debug('temp a is: ' + temp);
            // Passing the String array to a list with Selected field sorting.
            caseList1 = Database.query(soql + ' where status = \'Closed\' order by ' + sortField + ' ' + sortDir ); 

            // setting values of List in StandardSetController.
            con = new ApexPages.StandardSetController(caseList1);

        }else if(temp == 'b') {
            // String Query to have a list of cases for a respective End-user.
            soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
            System.debug('temp b is: ' + temp);
            // Passing the String array to a list with Selected field sorting.
            caseList1 = Database.query(soql + ' where status != \'Closed\' order by ' + sortField + ' ' + sortDir ); 

            // setting values of List in StandardSetController.
            con = new ApexPages.StandardSetController(caseList1);

        }else if(temp == 'c') {
            // String Query to have a list of cases for a respective End-user.
            soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
            System.debug('temp c is: ' + temp);
            // Passing the String array to a list with Selected field sorting.
            caseList1 = Database.query(soql + ' where CreatedById = :uid and status = \'Closed\' order by ' + sortField + ' ' + sortDir ); 

            // setting values of List in StandardSetController.
            con = new ApexPages.StandardSetController(caseList1);

        }else if(temp == 'd') {
            // String Query to have a list of cases for a respective End-user.
            soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
            System.debug('temp d is: ' + temp);
            // Passing the String array to a list with Selected field sorting.
            caseList1 = Database.query(soql + ' where CreatedById =  :uid and status != \'Closed\' order by ' + sortField + ' ' + sortDir ); 

            // setting values of List in StandardSetController.
            con = new ApexPages.StandardSetController(caseList1);

        }else if(temp == 'e') {
            // String Query to have a list of cases for a respective End-user.
            soql = ''+ 'SELECT'+ ' CreatedById,' + ' AccountId,' + ' OwnerId,' + ' Id,' + ' CaseNumber,' + ' CreatedDate,' + ' Requested_By__c,' + ' Subject,' + ' Release_Type__c,' + ' Status,' + ' Priority,' + ' Major_Release_Version__c,' + ' type' + ' From Case';
            System.debug('temp e is: ' + temp);
            // Passing the String array to a list with Selected field sorting.
            caseList1 = Database.query(soql + ' where CreatedById =  :uid AND LastViewedDate != NULL order by LastViewedDate DESC, ' + sortField + ' ' + sortDir ); 

            // setting values of List in StandardSetController.
            con = new ApexPages.StandardSetController(caseList1);
        }

        // sets the number of records in each page set
        con.setPageSize(10);
    }
}

CaseSortingAndPagination page

<apex:page controller="casesortingandpagination" showHeader="false" sidebar="false" >
    <apex:form styleClass="w25" >
        <apex:selectList id="selected_list" value="{!temp}" required="false" size="1">
            <apex:selectOptions value="{!items}" />
            <apex:actionSupport event="onchange" reRender="form" action="{!refresh}" />    
        </apex:selectList>
    </apex:form>
    <apex:form id="form">

        <apex:pageBlock id="pgblock" >
            <apex:pageBlockSection title="Case Details -  Page #{!pageNumber}" columns="1" collapsible="false" rendered="{!TotalPages > 0}">   

                <apex:pageBlockTable value="{!CaseList}" var="CsList" title="Click Column Header for Sorting" headerClass="FCenter" >

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Case Number" action="{!toggleSort}" rerender="pgblock">
                                <!-- Value attribute should have field (API Name) to sort in asc or desc order -->
                                <apex:param name="sortField" value="CaseNumber" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CsList.CaseNumber}"/>
                    </apex:column> 

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Created Date" action="{!toggleSort}" rerender="pgblock">
                                <!-- Value attribute should have field (API Name) to sort in asc or desc order -->
                                <apex:param name="sortField" value="CreatedDate" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CsList.CreatedDate}"/>
                    </apex:column> 

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Requested_By__c" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Requested_By__c" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CsList.Requested_By__c}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Subject" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Subject" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CsList.Subject}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Status" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Status" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CsList.Status}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Priority" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Priority" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CsList.Priority}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Owner Id" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="OwnerId" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CsList.OwnerId}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Type" action="{!toggleSort}" rerender="pgblock">
                                <apex:param name="sortField" value="Type" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CsList.Type}"/>
                    </apex:column>

                </apex:pageBlockTable>
            </apex:pageBlockSection>

            <apex:outputtext styleclass="detailText" value="Page {!pageNumber} of {!TotalPages}" rendered="{!TotalPages > 0}"></apex:outputtext>
            <apex:panelGrid columns="4"> 
                <apex:commandLink action="{!first}" value="First"></apex:commandlink>
                <apex:commandLink action="{!previous}">Previous</apex:commandlink>
                <apex:commandLink action="{!next}">Next</apex:commandlink>
                <apex:commandLink action="{!last}" value="Last"></apex:commandlink>                
            </apex:panelGrid>

        </apex:pageBlock>
    </apex:form>
</apex:page>

Additional information

I'll try to summarize the modifications I made to your code to achieve hopefully your desired output, and I would highly recommend that you re-think the overall design of the controller to remove redundant code.

  • Moved the logic for initializing the set controller to a method called refresh()
  • Now calls refresh() in the controller's constructor
  • Now calls refresh() when toggling the sort direction in toggleSort()
  • Changing the list view now executes the {!refresh} action
1
  • thanks for the suggestion and edits. Learnt something different today. – sw6 - KTBFFH Jul 9 '14 at 15:58
2

sw6, I think there may be an easier way to achieve what you want. Have you tried using the standard list controller for Cases? The Visualforce page below will enable you to navigate from page to page without having to write a single line of Apex.

<apex:page standardController="Case" recordSetVar="cases" showHeader="false" sidebar="false" >
    <apex:form styleClass="w25" >
        <apex:selectList id="filterId" value="{!filterId}" required="false" size="1">
            <apex:selectOptions value="{!listViewOptions}" />
            <apex:actionSupport event="onchange" reRender="pgblock"/>
        </apex:selectList>
        <apex:selectList id="pageSize" value="{!pageSize}" required="false" size="1">
            <apex:selectOption itemvalue="10" itemlabel="10"></apex:selectOption>
            <apex:selectOption itemvalue="25" itemlabel="25"></apex:selectOption>
            <apex:selectOption itemvalue="50" itemlabel="50"></apex:selectOption>
            <apex:selectOption itemvalue="100" itemlabel="100"></apex:selectOption>
            <apex:selectOption itemvalue="200" itemlabel="200"></apex:selectOption>
            <apex:actionSupport event="onchange" reRender="pgblock"/>
        </apex:selectList>
    </apex:form>

    <apex:form id="form">
        <apex:pageBlock id="pgblock" >
            <apex:pageBlockSection title="Case Details" columns="1" collapsible="false" >   
                <apex:pageBlockTable value="{!cases}" var="case" title="Click Column Header for Sorting" headerClass="FCenter" >

                    <apex:column >
                        <apex:facet name="header">
                           <apex:commandLink value="Case Number"  rerender="pgblock">
                                <!-- Value attribute should have field (API Name) to sort in asc or desc order -->
                                <apex:param name="sortField" value="CaseNumber" assignTo="{!sortField}"/>
                           </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CaSe.CaseNumber}"/>
                    </apex:column> 

                    <apex:column >
                        <apex:facet name="header">
                           <apex:commandLink value="Created Date"  rerender="pgblock">
                                <!-- Value attribute should have field (API Name) to sort in asc or desc order -->
                                <apex:param name="sortField" value="CreatedDate" assignTo="{!sortField}"/>
                           </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CaSe.CreatedDate}"/>
                    </apex:column> 

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Requested_By__c"  rerender="pgblock">
                                <apex:param name="sortField" value="Requested_By__c" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CaSe.Requested_By__c}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Subject"  rerender="pgblock">
                                <apex:param name="sortField" value="Subject" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CaSe.Subject}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Status"  rerender="pgblock">
                                <apex:param name="sortField" value="Status" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CaSe.Status}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Priority"  rerender="pgblock">
                                <apex:param name="sortField" value="Priority" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CaSe.Priority}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Owner Id"  rerender="pgblock">
                                <apex:param name="sortField" value="OwnerId" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CaSe.OwnerId}"/>
                    </apex:column>

                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandLink value="Type"  rerender="pgblock">
                                <apex:param name="sortField" value="Type" assignTo="{!sortField}"/>
                            </apex:commandLink>
                        </apex:facet>
                        <apex:outputField value="{!CaSe.Type}"/>
                    </apex:column>

                </apex:pageBlockTable>
            </apex:pageBlockSection>

            <apex:outputtext styleclass="detailText" value="Page {!pageNumber}"></apex:outputtext>
            <apex:panelGrid columns="4"> 
                <apex:commandLink action="{!first}" value="First"></apex:commandlink>
                <apex:commandLink action="{!previous}">Previous</apex:commandlink>
                <apex:commandLink action="{!next}">Next</apex:commandlink>
                <apex:commandLink action="{!last}" value="Last"></apex:commandlink>                
           </apex:panelGrid>

        </apex:pageBlock>
    </apex:form>
</apex:page>

P.S. I think the main issue that's affecting the code you posted is the fact that the con property is being overwritten every time it's accessed, because the getter always assigns a "fresh" list of data to the returned ApexPages.StandardSetController.

2
  • The postscript is the answer; there's plenty of times when one wants to use a StandardSetController directly, such as showing a list of records from a query not defined by a list view filter. Also, the standard list controller mechanism is borderline incompetent, as it pulls in all records by default (up to the maximum limit) until you apply a filter to it. – sfdcfox Jul 9 '14 at 3:31
  • @Marty - thanks for the suggestions. I have just tested it out but there seems to be a couple of road blocks with it - 1. the sorting doesn't work and 2. it pulls in all the available listviews and it doesn't seem to be a way to scope it down to only certain views like what I have in the "items" selectoption. – sw6 - KTBFFH Jul 9 '14 at 13:09

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.