function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lisa HorneLisa Horne 

Default Sort Visualforce table

I created a VF page for a custom object and would like to have the column header "Certification Year" sorted by default in decending order.

Is there an easy way to do this without a controller? I'm new to coding and am not familiar with this. Or can someone help me with getting it done?

This is the code I have for the visualforce page:


<apex:page standardController="Account">
   
      You are viewing the {!account.name} account.

      <apex:pageBlock title="ONS's">

      <apex:pageBlockTable value="{!account.ONC_s__r}" var="onc">

         <apex:column value="{!onc.Name}"/>
         <apex:column value="{!onc.Owner.name}"/>
         <apex:column value="{!onc.Certification_Year__c}"  />
         <apex:column value="{!onc.Practice_Setting__c}"/>
         

      </apex:pageBlockTable>

   </apex:pageBlock>

</apex:page>
Best Answer chosen by Lisa Horne
ClintLeeClintLee
Hi Lisa,

The only way I know to do this is with a controller extension.  

Create a new Apex Class like this:
 
public with sharing class AccountONSExtension
{
    private final Id acctId;
    public List<ONC_s__c> oncs { get; set; }

    public AccountONSExtension(ApexPages.StandardController stdController)
    {
        acctId = stdController.getId();
        oncs = [select Id
                              ,Name
                              ,Owner.Name
                              ,Certification_Year__c
                              ,Practice_Setting__c 
                     from ONC_s__c
                   where Account__c =:acctId
               order by Certification_Year__c DESC];
    }
}

In the above query I'm assuming the name of your lookup field to the Account is "Account__c".  If that's not correct just replace it with the API name of the Account lookup field.

Your Visualforce page will look like this:
 
<apex:page standardController="Account" extensions="AccountONSExtension">
    
    You are viewing the {!account.name} account.

    <apex:pageBlock title="ONS's">
        <apex:pageBlockTable value="{!oncs}" var="onc">
            <apex:column value="{!onc.Name}"/>
            <apex:column value="{!onc.Owner.name}"/>
            <apex:column value="{!onc.Certification_Year__c}"  />
            <apex:column value="{!onc.Practice_Setting__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

You'll also need a test class for the extension:
@istest
public with sharing class Test_AccountONSExtension
{
    @TestSetup
    static void setupData()
    {
        Account a = new Account(Name = 'Test Account');
        insert a;
    }

    static testmethod void testOne()
    {
        Test.startTest();
        Account a = [select Id from Account limit 1];
     
        ApexPages.StandardController controller = new ApexPages.StandardController(a);
        AccountONSExtension extension = new AccountONSExtension(controller);

        System.assertEquals(0, extension.oncs.size());

        Test.stopTest();
    }
}

Hope that helps,

Clint
 

All Answers

ClintLeeClintLee
Hi Lisa,

The only way I know to do this is with a controller extension.  

Create a new Apex Class like this:
 
public with sharing class AccountONSExtension
{
    private final Id acctId;
    public List<ONC_s__c> oncs { get; set; }

    public AccountONSExtension(ApexPages.StandardController stdController)
    {
        acctId = stdController.getId();
        oncs = [select Id
                              ,Name
                              ,Owner.Name
                              ,Certification_Year__c
                              ,Practice_Setting__c 
                     from ONC_s__c
                   where Account__c =:acctId
               order by Certification_Year__c DESC];
    }
}

In the above query I'm assuming the name of your lookup field to the Account is "Account__c".  If that's not correct just replace it with the API name of the Account lookup field.

Your Visualforce page will look like this:
 
<apex:page standardController="Account" extensions="AccountONSExtension">
    
    You are viewing the {!account.name} account.

    <apex:pageBlock title="ONS's">
        <apex:pageBlockTable value="{!oncs}" var="onc">
            <apex:column value="{!onc.Name}"/>
            <apex:column value="{!onc.Owner.name}"/>
            <apex:column value="{!onc.Certification_Year__c}"  />
            <apex:column value="{!onc.Practice_Setting__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

You'll also need a test class for the extension:
@istest
public with sharing class Test_AccountONSExtension
{
    @TestSetup
    static void setupData()
    {
        Account a = new Account(Name = 'Test Account');
        insert a;
    }

    static testmethod void testOne()
    {
        Test.startTest();
        Account a = [select Id from Account limit 1];
     
        ApexPages.StandardController controller = new ApexPages.StandardController(a);
        AccountONSExtension extension = new AccountONSExtension(controller);

        System.assertEquals(0, extension.oncs.size());

        Test.stopTest();
    }
}

Hope that helps,

Clint
 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Please check below Code. I hope that will help u
<apex:page standardController="Opportunity" tabStyle="Opportunity" extensions="myext" id="thepage">
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"/>
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"/>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-lightness/jquery-ui.css" type="text/css" media="all" />
    <apex:includeScript value="{!URLFOR($Resource.tablesorter, 'jquery.tablesorter.min.js')}"/>

    <script type="text/javascript">
        $j = jQuery.noConflict();    
        $j(document).ready(function () {
        $j("[id$=theaddrs]").tablesorter();
        });    
    </script>

    <apex:pageBlock id="theaddrsblock">
        <apex:pageBlockTable value="{!Addrs}" var="a" id="theaddrs" styleClass="tablesorter" headerClass="header">
            <apex:column>
                <apex:facet name="header">
                    <apex:outputText styleClass="header" value="{!$ObjectType.Address__c.Fields.Street__c.Label}" />
                </apex:facet>
                <apex:outputText value="{!a.Street__c}" />
            </apex:column>
        <!-- the other columns, closing tags, and that's it -->
https://developer.salesforce.com/forums/?id=906F0000000BVX2IAO

On Simple example you see on below blog
http://bobbuzzard.blogspot.in/2014/09/sorting-visualforce-tables-with.html
http://salesforcesource.blogspot.in/2008/11/adding-sorting-capability-to.html

Please let us know if this will help you

Thanks
Amit Chaudhary
Lisa HorneLisa Horne
Thanks Clint!  Works perfectly! 
Ratna NutalapatiRatna Nutalapati
Thank you @Amit!! Your solution gives the ability to sort by each column in the table and it doesn't run query mulitple times just to sort the data table.
Heather_HansonHeather_Hanson
I'm trying to do the same thing using apex:datatable, but when I test it, it doesn't work.  Is Clint still around to help me out?  Or anyone!  It would be greatly appreciated!

Here is my visualforce page:
 
<apex:page standardController="Account" extensions="AssetPrintableView" sidebar="false" showHeader="false" renderAs="PDF">
    
    
    <apex:dataTable value="{!Account.Assets}" var="a" style="width: 98.7%">
        <apex:column headerValue="Asset" headerClass="tbl_hdr" styleClass="tbl" style="width: 30%" value="{!a.Name}"/>
        <apex:column headerValue="Qty" headerClass="tbl_hdr tac" styleClass="tbl tac" style="width: 5%"><apex:outputText value="{0,number,#,##0}"><apex:param value="{!a.Quantity}"/></apex:outputText></apex:column>
        <apex:column headerValue="Status" headerClass="tbl_hdr" styleClass="tbl" style="width: 10%" value="{!a.Status}"/>
        <apex:column headerValue="Install Date" headerClass="tbl_hdr" styleClass="tbl tac" style="width: 10%" value="{!a.InstallDate}"/>
        <apex:column headerValue="Serial Number" headerClass="tbl_hdr" styleClass="tbl" style="width: 7%" value="{!a.SerialNumber}"/>
        <apex:column headerValue="Service Payment Type" headerClass="tbl_hdr" styleClass="tbl" style="width: 7%" value="{!a.Payment_Type__c}"/>
        <apex:column headerValue="Hardware Payment Type" headerClass="tbl_hdr" styleClass="tbl" style="width: 7%" value="{!a.Rental_Purchase__c}"/>
    </apex:dataTable>

Here is my controller:
 
public with sharing class AssetPrintableView{
    
    private final Id acctId;
    public List<Asset> asset { get; set; }

    public AssetPrintableView(ApexPages.StandardController stdController)
    {
        acctId = stdController.getId();
        asset = [select Id
                 		,Name                             
                     from asset
                   where AccountId =:acctId
               order by Name DESC];
    }
}

Here is the test...I need the column "Asset Name" to be sorted.

User-added image
Clint_LeeClint_Lee
Hi Heather,

Feel free to send me your contact details at clint@textey.io.  I can help you out.

Clint