• Amanda Carolina Pistolato
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hello Guys,
I'm new at visual force and I'm trying to make a list with column sorting(asc and desc) and pagination with option to select records per page and I can't make both work together. Could anyone help me please?

VF page
<apex:page controller="AccountsPaginationController" tabStyle="Account"  action="{!ViewData}" extensions="AccountListViewController4">
    <apex:form >
        <apex:actionFunction name="refreshPageSize" action="{!refreshPageSize}" status="fetchStatus" reRender="pbId"/>
        <apex:pageBlock id="pbId">
            <apex:pageBlockSection id="blocktable" title="Accounts" collapsible="false" columns="1">
                <apex:pageBlockTable value="{!AccountsortList}" var="acc">
                     
                     <apex:column headerValue="Account Name" value="{!acc.Name}">
                       <apex:facet name="header">   
                                <apex:commandLink action="{!ViewData}" value="Account Name{!IF(ExpressionSort=='name',IF(DirectionOfSort == 'ASC', '▼', '▲'),'')}">
                                    <apex:param value="name" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                                </apex:commandLink>
                            </apex:facet>
                        </apex:column>
                     
                    <apex:column headerValue="Billing State" value="{!acc.BillingState}">
                       <apex:facet name="header">   
                                <apex:commandLink action="{!ViewData}" value="Account BillingState{!IF(ExpressionSort=='billingstate',IF(DirectionOfSort == 'ASC', '▼', '▲'),'')}">
                                    <apex:param value="billingstate" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                                </apex:commandLink>
                            </apex:facet>
                        </apex:column>
                     
                    <apex:column headerValue="Phone">
                        <apex:outputField value="{!acc.Phone}"/>
                    </apex:column>
                     
                    <apex:column headerValue="Type">
                        <apex:outputField value="{!acc.type}"/>
                    </apex:column>
                     
                    <apex:column headerValue="Owner Name">
                        <apex:outputField value="{!acc.Owner.Name}"/>
                    </apex:column>
                     
                    <apex:column headerValue="Website">
                        <apex:outputField value="{!acc.Website}"/>
                    </apex:column>
                </apex:pageBlockTable>
               
                 
                <apex:panelGrid columns="8"> 
                 Records per page:
                <apex:selectList value="{!size}" multiselect="false" size="1" onchange="refreshPageSize();">
                    <apex:selectOptions value="{!paginationSizeOptions}"/>
                </apex:selectList>
                 
                <apex:commandButton status="fetchStatus" reRender="pbId" value="First" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/> 
  
                <apex:commandButton status="fetchStatus" reRender="pbId" value="Previous" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/> 
  
                <apex:commandButton status="fetchStatus" reRender="pbId" value="Next" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/> 
  
                <apex:commandButton status="fetchStatus" reRender="pbId" value="Last" action="{!setCon.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:outputPanel >                      
                    <apex:actionStatus id="fetchStatus" >
                        <apex:facet name="start" >
                          <img src="/img/loading.gif" />                    
                        </apex:facet>
                    </apex:actionStatus>
                </apex:outputPanel> 
  
            </apex:panelGrid>  
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Controller:
public class AccountsPaginationController{

    public PageReference ViewData() {
        return null;
    }

    Public Integer size{get;set;} 
    Public Integer noOfRecords{get; set;} 
    public List<SelectOption> paginationSizeOptions{get;set;}
         
    public AccountsPaginationController(){
        size=10;
        paginationSizeOptions = new List<SelectOption>();
        paginationSizeOptions.add(new SelectOption('5','5'));
        paginationSizeOptions.add(new SelectOption('10','10'));
        paginationSizeOptions.add(new SelectOption('20','20'));
        paginationSizeOptions.add(new SelectOption('50','50'));
        paginationSizeOptions.add(new SelectOption('100','100'));
    }
     
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {                
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [select id,Name,Phone,BillingState,Type,Owner.Name, Website from Account]));
                setCon.setPageSize(size);  
                noOfRecords = setCon.getResultSize();
            }            
            return setCon;
        }
        set;
    }
     
    //Changes the size of pagination
    public PageReference refreshPageSize() {
         setCon.setPageSize(size);
         return null;
    }
 
    // Initialize setCon and return a list of record    
     
    public List<Account> getAccount() {
         return (List<Account>) setCon.getRecords();
    }
}

Extension for sorting column:
public class AccountListViewController4{

    public AccountListViewController4(AccountsPaginationController controller) {

    }

public List<Account> AccountsortList {get; set;}
public String SortingExpression = 'name';
public String DirectionOfSort = 'ASC';

    public AccountListViewController4(ApexPages.StandardSetController controller) {
        AccountsortList = new List<Account>();
        ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(AccountsortList);
    }

    public String ExpressionSort {
        get {
            return SortingExpression;
        }
        set {
            If(value == SortingExpression) {
                DirectionOfSort = (DirectionOfSort == 'ASC')? 'DESC' : 'ASC';
            }
            else {
                DirectionOfSort = 'ASC';
                SortingExpression = value;
            }
        }

    }

    public String getDirectionOfSort() {
        If(SortingExpression == Null || SortingExpression == '') {
            return 'DESC';
        }
        else {
            return DirectionOfSort;
        }
    }

    public void setDirectionOfSort(String value) {
        DirectionOfSort = value;
    }

    public List<Account>getAccounts() {
        return AccountsortList;
    }

     public PageReference ViewData() {
        String FullSortExpression = SortingExpression + ' ' + DirectionOfSort;

       String Queryitem = ' SELECT Id, Name, Phone, BillingState, Type, Owner.Name, Website FROM Account WHERE Account.Name != Null ORDER BY ' + FullSortExpression;

        AccountsortList = DataBase.query(Queryitem);
        return Null;
    }
    
    
    
    
}


 
Hello Everyone!

I'm really new on VisualForce pages and I'm kind of lost using standard controller and extensions. I have this code that has pagination; previous, next, first and last link button; shows the number of pages and user can select to see 20 or 5 rows per page(Records per page). That worked when I was testing with standard controller. But then I have to sort the columns of the table, when you click on column header it will change betwen ascending or descending sorting and for that I have created an extension. I would like to know if it is possible to make both work together or if I will have to make a controller for pagination and buttons.

VF page:
<apex:page standardController="Account" recordSetVar="Accountvar"  action="{!ViewData}" extensions="AccountListViewController3">
<apex:sectionHeader title="My Accounts" subtitle="Account List View"/>
    <apex:form >
         <apex:pageBlock id="accounts_list" >
          <apex:pageMessages id="error" />

           <apex:pageBlockSection id="blocktable" >

                <apex:pageBlockTable value="{!AccountsortList}" var="t" rendered="{!NOT(ISNULL(AccountsortList))}" >

                        <apex:column >
                            <apex:facet name="header">   
                                <apex:commandLink action="{!ViewData}" value="Account Name{!IF(ExpressionSort=='name',IF(DirectionOfSort == 'ASC', '▼', '▲'),'')}" id="cmdsort">
                                    <apex:param value="name" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                                </apex:commandLink>
                            </apex:facet>
                            <apex:outputLink value="/{!t.Id}" target="_blank">{!t.Name}</apex:outputLink>
                        </apex:column>
                        
                          <apex:column value="{!t.BillingState}" >
                            <apex:facet name="header">   
                                <apex:commandLink action="{!ViewData}" value="Account BillingState{!IF(ExpressionSort=='billingstate',IF(DirectionOfSort == 'ASC', '▼', '▲'),'')}">
                                    <apex:param value="billingstate" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                                </apex:commandLink>
                            </apex:facet>
                        </apex:column>
                        
                        <apex:column value="{!t.Phone}" >
                            <apex:facet name="header">   
                                <apex:commandLink action="{!ViewData}" value="Account Phone{!IF(ExpressionSort=='phone',IF(DirectionOfSort == 'ASC', '▼', '▲'),'')}">
                                    <apex:param value="phone" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                                </apex:commandLink>
                            </apex:facet>
                        </apex:column>
                        
                             <apex:column value="{!t.Type}" >
                            <apex:facet name="header">   
                                <apex:commandLink action="{!ViewData}" value="Account Type{!IF(ExpressionSort=='type',IF(DirectionOfSort == 'ASC', '▼', '▲'),'')}">
                                    <apex:param value="type" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                                </apex:commandLink>
                            </apex:facet>
                        </apex:column>
                        
                        <apex:column value="{!t.Owner.Name}" >
                            <apex:facet name="header">   
                                <apex:commandLink action="{!ViewData}" value="Account Owner Alias{!IF(ExpressionSort=='owner.name',IF(DirectionOfSort == 'ASC', '▼', '▲'),'')}">
                                    <apex:param value="owner.name" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                                </apex:commandLink>
                                </apex:facet>
                        </apex:column>
                        
                        <apex:column >
                            <apex:facet name="header">   
                                <apex:commandLink action="{!ViewData}" value="Website{!IF(ExpressionSort=='website',IF(DirectionOfSort == 'ASC', '▼', '▲'),'')}">
                                    <apex:param value="website" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                                </apex:commandLink>
                            </apex:facet>
                            <apex:outputLink value="{!t.website}" >{!t.website}</apex:outputLink>
                            
                        </apex:column>
                        
                         


                        <!-- <apex:column headerValue="BillingState/Province" value="{!t.BillingState}"/> -->
                        <!--<apex:column headerValue="Phone" value="{!t.Phone}"/> -->
                        <!--<apex:column headerValue="Type" value="{!t.Type}"/> -->                  
                        <!--<apex:column headerValue="Account Owner Alias" value="{!t.Owner.Name}"/> -->
                        <!--<apex:column headerValue="Website" value="{!t.Website}"/>-->

                    <apex:inlineEditSupport event="onClick"/>


                </apex:pageBlockTable>

           </apex:pageBlockSection>   

 <!-- Pagination -->
<table style="width: 100%"><tr>
    <td>
        <!-- Page X of Y -->
        Page:
        <apex:outputText value=" {!PageNumber} of {! CEILING(ResultSize / PageSize) }"/>
    </td>
    <td align="center">
        
        <!-- First page -->
        <!-- active -->
        <apex:commandLink action="{! First }" value="First" rendered="{! HasPrevious }"
       /> 
    <!-- inactive (already in the first page) -->
    <apex:outputText style="color: #ccc;" value="First"
     rendered="{!NOT(HasPrevious)}"/>
    &nbsp;&nbsp;
        
        <!-- Previous page -->
        <!-- active -->
    <apex:commandLink action="{! Previous }" value="« Previous"
     rendered="{! HasPrevious }"/>
    <!-- inactive (no earlier pages) -->
    <apex:outputText style="color: #ccc;" value="« Previous"
     rendered="{! NOT(HasPrevious) }"/>
    &nbsp;&nbsp;
        
    <!-- Next page -->
    <!-- active -->
    <apex:commandLink action="{! Next }" value="Next »"
     rendered="{! HasNext }"/>
    <!-- inactive (no more pages) -->
    <apex:outputText style="color: #ccc;" value="Next »"
     rendered="{! NOT(HasNext) }"/>
     &nbsp;&nbsp;
        
    <!-- Last page -->
    <!-- active -->
    <apex:commandLink action="{! Last }" value="Last"
     rendered="{! HasNext }"/>
    <!-- inactive (no more pages) -->
    <apex:outputText style="color: #ccc;" value="Last"
     rendered="{! NOT(HasNext) }"/>
        
        
    </td>
    <td align="right">
        <!-- Records per page -->
        Records per page:
        <apex:selectList value="{! PageSize }" size="1">
            <apex:selectOption itemValue="5" itemLabel="5"/>
            <apex:selectOption itemValue="20" itemLabel="20"/>
            <apex:actionSupport event="onchange" reRender="accounts_list"/>
        </apex:selectList>
    </td>
</tr></table>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Account List View Controller with column sorting:
public class AccountListViewController3{
public List<Account> AccountsortList {get; set;}
public String SortingExpression = 'name';
public String DirectionOfSort = 'ASC';

    public AccountListViewController3(ApexPages.StandardSetController controller) {
        AccountsortList = new List<Account>();
        ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(AccountsortList);
    }

    public String ExpressionSort {
        get {
            return SortingExpression;
        }
        set {
            If(value == SortingExpression) {
                DirectionOfSort = (DirectionOfSort == 'ASC')? 'DESC' : 'ASC';
            }
            else {
                DirectionOfSort = 'ASC';
                SortingExpression = value;
            }
        }

    }

    public String getDirectionOfSort() {
        If(SortingExpression == Null || SortingExpression == '') {
            return 'DESC';
        }
        else {
            return DirectionOfSort;
        }
    }

    public void setDirectionOfSort(String value) {
        DirectionOfSort = value;
    }

    public List<Account>getAccounts() {
        return AccountsortList;
    }

     public PageReference ViewData() {
        String FullSortExpression = SortingExpression + ' ' + DirectionOfSort;

       String Queryitem = ' SELECT Id, Name, Phone, BillingState, Type, Owner.Name, Website FROM Account WHERE Account.Name != Null ORDER BY ' + FullSortExpression +' Limit 1000';

        AccountsortList = DataBase.query(Queryitem);
        return Null;
    }
}

Thank you :D
Hi, 

Hopeing someone can help me here. 

I would like to filter results in a pageBlockTable in ascending order.

I've created a standard controller extension: 

public with sharing class ProjectONSExtension
{
    private final Id acctId;
    public List<REPRO__Project__c> oncs { get; set; }

    public ProjectONSExtension(ApexPages.StandardController stdController)
    {
        acctId = stdController.getId();
        oncs = Database.query('SELECT Name,REPRO__Street__c, REPRO__City__c, (SELECT Id,name,Property_Level__c,Price_List_Property_Name__c,REPRO__Type__c,REPRO__Bdr__c,REPRO__Bth__c,REPRO__Study__c,REPRO__Internal_Size__c,REPRO__External_Size__c,REPRO__Car__c,REPRO__List_Price__c,REPRO__Status__c from REPRO__Properties__r where REPRO__Project__c =:acctId order by Price_List_Property_Name__c )' +
                ' FROM REPRO__Project__c ' 
   );             
    }
}

It runs without an error. But it didn't order the pageBlockTable results in ascending order. Boo! Any thoughts?

Here is the visual page for reference:

<apex:page standardController="REPRO__Project__c" extensions="ProjectONSExtension" renderAs="pdf" showHeader="false" sidebar="false" >

    <apex:pageBlock >
     
          <h1 style= "text-align: center;">{!REPRO__Project__c.name}</h1>
        <p style= "text-align: center;">{!REPRO__Project__c.REPRO__Street__c}, {!REPRO__Project__c.REPRO__City__c} <br/> Price List as of {! TODAY()} </p>
   
        <apex:pageBlockTable value="{!REPRO__Project__c.REPRO__Properties__r}" var="custom" align="center" cellpadding="1" border="2"  style="font-weight: bold; text-align: center; ">
              <apex:column value="{!custom.Property_Level__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.Price_List_Property_Name__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__Type__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />   
              <apex:column value="{!custom.REPRO__Bdr__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__Bth__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__Study__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__Internal_Size__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; "  />
              <apex:column value="{!custom.REPRO__External_Size__c}"  style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__Car__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__List_Price__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
              <apex:column value="{!custom.REPRO__Status__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />
            </apex:pageBlockTable>

Any help would be greatly appreciated :)