+ Start a Discussion
RossGRossG 

Sorting on apex:pageblocktable columns

Hi,

Is there a way to make the columns in this visualforce page sortable?  It's simply a page that displays the open tasks owned by the current user.

controller:
 
public class tasksController{
    
    List<Task> tk; 
      // toggles the sorting of query from asc<-->desc


    public tasksController(ApexPages.StandardController Controller){
     }  
     
    public List<Task> getResults(){     
        return tk;
    }         
    public PageReference gettk(){
        String userId=UserInfo.getUserId();
        UserId=userId.Substring(0,15);
        tk=[Select Status, Subject, Priority, OwnerId, Owner.Name, WhatId, Response_Needed__c,What.Name, WhoId, Who.Name, ActivityDate from Task WHERE 
             Status != 'Completed' AND (ActivityDate = THIS_WEEK ) AND OwnerId =: UserId ORDER BY ActivityDate DESC LIMIT 25  ];
        return Null; 
    }
}



visualforce page:
 
<apex:page standardController="Task" extensions="tasksController" action="{!gettk}">
<apex:form >
    <html>
    &nbsp;&nbsp;<img src="/img/icon/home32.png"/>
    <font size="5">&nbsp;&nbsp;My Open Tasks Due This Week</font><br></br>

<apex:pageblock >


                    <apex:pageBlockTable value="{!results}" var="tsk">
                                <apex:column >
                                    <apex:outputlink value="/{!tsk.Id}">{!tsk.Subject}</apex:outputLink>
                                    <apex:facet name="header"> Subject </apex:facet>                                                                                                       
                                </apex:column>
                                <apex:column > 
                                    <apex:outputField value="{!tsk.Response_Needed__c}" />
                                    <apex:facet name="header"> Urgency                                      
                                    <span class="helpButton" id="example-title-_help">
                                    <img src="/s.gif" class="helpOrb"/>
                                      <script type="text/javascript">
                                        sfdcPage.setHelp('example-title', 'RED: Task is overdue by 1 day or more or has no due date.  YELLOW: Task is due today.  GREEN: Task is due on some day in the future. ');
                                      </script>
                                    </span>                                                                                                       
                                    </apex:facet>                                                                      
                                </apex:column>                                
                                <apex:column >
                                    <apex:outputLink value="/{!tsk.WhoId}">{!tsk.Who.Name}</apex:outputLink>
                                    <apex:facet name="header"> Name</apex:facet>
                                </apex:column> 
                                <apex:column >
                                    <apex:outputLink value="/{!tsk.OwnerId}">{!tsk.Owner.Name}</apex:outputLink>
                                    <apex:facet name="header"> Assigned To </apex:facet> 
                                </apex:column>
                                <apex:column value="{!tsk.ActivityDate}"/>                                                                                                                                                                                                   
                                <apex:column value="{!tsk.Status}"/>  
                    </apex:pageBlockTable> 
                    
                    
                    <br></br>                 
                    <apex:outputLink target="_blank" value="https://cs15.salesforce.com/00Oe0000000Ta99" id="theLink">View All My Open Tasks</apex:outputLink>

 </apex:pageblock>
                   


    </html>
    </apex:form>
</apex:page>

 
SarfarajSarfaraj
Hi

Please check this link to know how to make a pageblock table sortable,

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AsSzIAK

--Akram
Nirmal ChristopherNirmal Christopher
Use dynamic get set variables for the filters in your query something like this 
tk=[Select Status, Subject, Priority, OwnerId, Owner.Name, WhatId, Response_Needed__c,What.Name, WhoId, Who.Name, ActivityDate from Task WHERE
17
             Status != 'Completed' AND (ActivityDate = THIS_WEEK ) AND OwnerId =: UserId ORDER BY //variable name(May be a field value of some sort) DESC LIMIT 25  ];
pass the value for the variable name using the Apex param tag enclose the whole component something like this 
<apex:commandLink value="Job Name " style="font-size: 130%;" action="{!init}" reRender="theBlock"> 
                    <apex:outputPanel layout="none" rendered="{!sortExpression=='job__c' && sortDirection='ASC'}">&#8593;</apex:outputPanel>
                    <apex:outputPanel layout="none" rendered="{!sortExpression=='job__c' && sortDirection='DESC'}">&#8595;</apex:outputPanel>
                        <apex:param value="job__c" name="column" assignTo="{!sortExpression}"  ></apex:param>
                </apex:commandLink>

inside the Init method i have created a string query variable some thing like this 
public void init(){
        system.debug('******sortExpression****'+sortExpression);
        system.debug('******sortDirection****'+sortDirection);
        String stringQuery = 'select id,name,Account__c,Job__c,Account1__c,Job_Locations__r.Account__r.name,Location_State__c,City__r.name,Job_Locations__r.Employer_Wisdom__c,City__c,Job_Locations__c from Jobs__c where Status__c=\'Active\' and Advertise__c=true and Applications_Close_Date__c>=today ORDER BY '+sortExpression +' '+sortDirection;
        system.debug('******stringQuery ****'+stringQuery);
        openjobs=database.query(stringQuery);
      
  }
The characters "#8593"  & &"#8595"is the upward arrow and downward arrow respectively which is a clickable link. On clicking this link the param tag captures the input from the visualforce page snd supplies values to the variables 
sortExpression sortDirection respectively.

If this helps please hit like and best answer it helps others.

Thanks and Regards,

Nirmal Christopher
Certfied Tech consultant, Global Tech and Resources, www.gtr.net





 
Sébastien RichardSébastien Richard
Hi,

You will also find an elegant solution without javascript proposed by Trailhead Salesforce.
Here is the link : https://developer.salesforce.com/trailhead/force_com_dev_beginner/visualforce_fundamentals/visualforce_custom_controllers
See Section "Add a New Action Method".
Sagar Nagvekar 14Sagar Nagvekar 14
This code will allow you to sort alternately in ascending and descending order when you click on the "First Name" and "Last Name" column headers in the table.
public class ContactsListController
{
    private String sortOrder = 'LastName';
    public String ascendingOrDescending = ' ASC ';
   
    public List<Contact> getContacts()
    {
        List<Contact> results = Database.query(
        'SELECT Id, FirstName, LastName, Title, Email ' +
        'FROM Contact ' +
        'ORDER BY ' + sortOrder + ascendingOrDescending +
        'LIMIT 900'
        );       
        return results;
    }
   
    public void sortByLastName()
    {
        this.sortOrder = 'LastName';
       
        if(ascendingOrDescending == ' ASC ')
        {
            ascendingOrDescending = ' DESC ';
        }
        else
        {
            ascendingOrDescending = ' ASC ';
        }   
    }
   
    public void sortByFirstName()
    {
        this.sortOrder = 'FirstName';
       
        if(ascendingOrDescending == ' ASC ')
        {
            ascendingOrDescending = ' DESC ';
        }
        else
        {
            ascendingOrDescending = ' ASC ';
        }       
    }
}
<apex:page controller="ContactsListController">
  <apex:form >
    <apex:pageBlock title="Contacts List" id="contacts_list">
      <apex:pageBlockTable value="{! contacts }" var="ct">
       
        <apex:column value="{! ct.FirstName }">
          <apex:facet name="header">
            <apex:commandLink action="{! sortByFirstName }"
                reRender="contacts_list">First Name
            </apex:commandLink>
          </apex:facet>
        </apex:column>
        <apex:column value="{! ct.LastName }">
         <apex:facet name="header">
           <apex:commandLink action="{! sortByLastName }"
                reRender="contacts_list">Last Name
           </apex:commandLink>
         </apex:facet>
        </apex:column>
       
        <apex:column value="{! ct.Title }"/>
        <apex:column value="{! ct.Email }"/>
       
      </apex:pageBlockTable>
    </apex:pageBlock>
  </apex:form>
</apex:page>