You need to sign in to do that
Don't have an account?
Zach Ackerman
VF Sorting
I have the following code and visualforce. I wanted to give users the ability to click on the column headers to allow them to sort the table. Is this possible? I've seen a few things online, but was unable to get it to work with pagination.
Apex:
Visual Force
Apex:
public with sharing class Pagination_min { Public Integer noOfRecords{get; set;} Public Integer size{get;set;} public ApexPages.StandardSetController setCon { get{ if(setCon == null){ size = 500; string OwnersId = userinfo.getUserId() ; string Rectypeid = '01240000000Uexl'; string Rectypeid2 = '01240000000cnJS'; string queryString = 'Select Name, StageName,FED__c,Broker__c,Broker_Name__c,Core_Product__c,LeadSource,Number_Quoted__c,CreatedDate FROM Opportunity Where OwnerId = :OwnersId AND IsClosed = False AND (RecordTypeId = :Rectypeid OR RecordTypeId= :RecTypeId2) order by Probability,FED__c asc'; setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString)); setCon.setPageSize(size); noOfRecords = setCon.getResultSize(); } return setCon; }set; } Public List<Opportunity> getOpps(){ List<Opportunity> oppList = new List<Opportunity>(); for(Opportunity o : (List<Opportunity>)setCon.getRecords()) oppList.add(o); return oppList; } public pageReference refresh() { setCon = null; getOpps(); setCon.setPageNumber(1); return null; } }
Visual Force
<apex:page controller="Pagination_min"> <apex:sectionHeader title="My Opportunity List"/> <apex:form > <apex:commandButton value="New Opportunity" onClick="window.open('https://cs15.salesforce.com/006/e?retURL=%2F006%2Fo&RecordType=01240000000Uexl&ent=Opportunity');"/> <apex:commandButton value="Detailed Report" onClick="window.open('https://cs15.salesforce.com/00Oe0000000fRSG');"/> <apex:pageBlock id="pb"> <apex:pageBlockTable value="{!opps}" var="o"> <apex:column headerValue="Opportunity Name"> <apex:outputlink value="/{!o.id}" target="__blank">{!o.Name}</apex:outputlink> </apex:column> <apex:column value="{!o.StageName}"/> <apex:column headerValue="Effective Date" value="{!o.FED__c}"/> <apex:column headerValue="Broker Name"> <apex:outputlink value="/{!o.Broker__c}" target="__blank">{!o.Broker_Name__c}</apex:outputlink> </apex:column> <apex:column value="{!o.Core_Product__c}"/> <apex:column value="{!o.LeadSource}"/> <apex:column value="{!o.Number_Quoted__c}"/> <apex:column value="{!o.CreatedDate}"/> </apex:pageBlockTable> <apex:panelGrid columns="7"> <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/> <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/> <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/> <apex:commandButton status="fetchStatus" reRender="pb" value=">|" 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: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:pageBlock> </apex:form> </apex:page>
With pagination, you have to re-run the SOQL query with the appropriate ordering (the data of the other pages are unknown by the client by definition).
- bobbuzzard.blogspot.com: Sorting Visualforce Tables with JavaScript This will only sort the rows displayed on the page. If this page displayed a subset of rows in a collection, the sorted values wouldn’t reflect the full dataset. In this scenario I’d re-run the SOQL query with the appropriate ordering or sort the collection of data server side.
http://bobbuzzard.blogspot.com/2014/09/sorting-visualforce-tables-with.html- Here is a very simple example of how you could implement sorting using some visualforce and apex. The trick here is an apex:facet tag
http://salesforce.stackexchange.com/questions/44562/how-to-do-arrow-sorting-on-columnsApex with Sorting: Visual Force with Sorting: