You need to sign in to do that
Don't have an account?

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:
visualforce page:
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> <img src="/img/icon/home32.png"/> <font size="5"> 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>
Please check this link to know how to make a pageblock table sortable,
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AsSzIAK
--Akram
inside the Init method i have created a string query variable some thing like this 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
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".
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>