+ Start a Discussion
studzeystudzey 

Help with Sorting Pageblocktables..........

Hi Guys,  

 

I'm kind of new to Salesforce.

 

I have managed to produce the following, which works well, apart from the fact that the sorting is not happening:

 

<Div id="Childs1"><apex:pageBlock >

<apex:pageBlockTable rendered="true"

   value="{!Trading_Contract__c.TC_SH__r}"

   var="child1" width="100%">  

 <apex:column value="{!child1.Shipment_Ref__c}"/>    

<apex:column headerValue="Date">   

<apex:outputText value="{0,date,yyyy-MM}">

<apex:param value="{!child1.Date__c}" />

 </apex:outputText>

</apex:column>

<apex:column value="{!child1.Bulk_Quantity__c}"/>  

 <apex:column value="{!child1.Destination_Port_Place__c}"/> 

</apex:pageBlockTable> </apex:pageBlock>

</Div> 

 

Trading_Contract__c is a costom object, linked to TC_SH__r which is another custom "Shipments" object.  

 

What is the best way to sort the above pageblock table??? I would really appreciate the help! 

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

For example:

 

public class myTCExtension{

private Trading_Contact__c TC;



public myTCExtension  (ApexPages.Standardcontroller c){
              controller = c;
                TC=(Trading_Contact__c)Controller.getRecord();              
              
        }

public TC_SH__c[] getlSH(){

   return ([Select YOURFIELDS From TC_SH__c Where PARENTFIELDONTCSH = :TC.id ORDER BY YOURSORTFIELD]);

}

}

 

Then use {!getlSH} as the value in your pageBlockTable. You will need to include the controller extension in you apex:page header as well

All Answers

Starz26Starz26

Create a controller extension to handle the SOQL to return a list of data. In the SOQL statement use ORDER BY

 

This sets a hard sort that cannot be controlled by the user. To implement sorting of the table by clicking on heading is more complicated and there are many example on the forums.

Starz26Starz26

For example:

 

public class myTCExtension{

private Trading_Contact__c TC;



public myTCExtension  (ApexPages.Standardcontroller c){
              controller = c;
                TC=(Trading_Contact__c)Controller.getRecord();              
              
        }

public TC_SH__c[] getlSH(){

   return ([Select YOURFIELDS From TC_SH__c Where PARENTFIELDONTCSH = :TC.id ORDER BY YOURSORTFIELD]);

}

}

 

Then use {!getlSH} as the value in your pageBlockTable. You will need to include the controller extension in you apex:page header as well

This was selected as the best answer
studzeystudzey

Thanks . I managed to get it working! 

 

I just added "public ApexPages.StandardController controller {get; set;}" at the top before the constructor and also changed controller = c to this.controller = c. 

I just have to write the SOQL query then I should be set. 

 

 

One quick question. How do you go about testing in a case like this? I keep getting an "unknown constructor" error when I extend this class....

 

Thakns 

 

Stadler

studzeystudzey

 

Hi Guys,   


Almost done solving this issue.


I'm sitting with the following code:

 

 < apex:page showHeader="false" renderAs="pdf"   

 extensions="myTCExtension" standardController="Trading_Contract__c" >

 


 < apex:pageBlock >

 < apex:pageBlockSection> <apex:pageBlockTable value="{!myTCExtension}" var="shipments">

              < apex:column value="{!shipments.Date__c}" headerValue="Date"/>

              < apex:column value="{!shipments.Quantity__c}" headerValue="Quantity" />

             </apex:pageBlockTable></apex:pageBlockSection></apex:pageBlock >

 

But, I'm getting the following error:  "UNKNOWN PROPERTY "TRADING_CONTRACT__CStandardController.myTCExtension" 

 

Does anyone have any ideas what the problem is?

 

 Just so you have it, my extension class looks like following: 

 

public class myTCExtension{

 

private Trading_Contract__c TC;

public ApexPages.StandardController controller {get; set;}

public List<Contract_Shipments__c> shipments; public myTCExtension (ApexPages.Standardcontroller c)

{   

this.controller = c;    TC = (Trading_Contract__c)Controller.getRecord();                  }

        public Contract_Shipments__c [ ] getShipmentList()   

       shipments = ([Select Bulk_Quantity__c,Cost_of_Shipment__c,Cost_of_Shipment_excl_vat__c,Date__c, Trading_Contract__c, Quantity__c     from Contract_Shipments__c                Where Contract_Shipments__c.Trading_Contract__c = :TC.id ORDER BY Date__c]); 

       return shipments;}  

 

Thanks again for the awesome help....

studzeystudzey

Hi guys,

 

Thanks for the help. I managed to get it sorted out

 

cheers,