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
studzeystudzey 

Sorting a pageblocktable - Please help! :)

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 must 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

 

Stadler - Admin

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

You are correct, you would have to write a custom controller or controller extension depending on your needs.

All Answers

sdetweilsdetweil

YOU have to provide all the code to do the work.. no builtin feature for this..(bummer)..

 

here is the header for one of my columns

 

 

<apex:column > 
<apex:facet name="header">
<apex:commandLink action="{!ViewData}" value="Asset
 Name{!IF(sortExpression=='Asset',IF(sortDirection='ASC','▼','▲'),'')}" id="assetSort">
<apex:param value="Asset" name="column" assignTo="{!sortExpression}" ></apex:param>
</apex:commandLink>
</apex:facet>
<apex:outputtext value="{!contract.asset.name}"/>
</apex:column>


 this invokes a method in your controller, and passes in the column name.
the controller code then reorders the data, and the view panel redisplays the data..

I found this on some other search in the VisualForce section
this only does ONE column..

multiple columns (with that column only sort) is more work (more data in the sortExpression variable)

   you have to add this code to each sortable column header and change the column name in the commandlink and the param.

   then your controller method has to set sortExpression and sortDirection for the currently sorted column

   one sortExpression/sortDirection field for the entire table

multiple concurrent column sort is even more work than that. 

   you will have ot make multiple 'sortExpression/sortDirection' fields and keep them all up to date

   and then implement some multi-column sorting algorithm

 

studzeystudzey

Thanks so much for that. 

 

I've been trying some random things up till now and will look into this.

 

Thanks again

 

 

studzeystudzey

Sorry one more thing, 

 

Why do I get an error when I use "facet" - what is it exactly for? I've seen plenty of people using it

 

Thanks

 

Stadler

 

sdetweilsdetweil

from the VF developers guide http://www.salesforce.com/us/developer/docs/pages/index.htm

 

An<apex:facet>component can only exist in the body of a parent component if the parent supports facets. The name of the facet component must match one of the pre-defined facet names on the parent component. This name determines where the content of the facet component is rendered. Consequently, the order in which a facet component is defined within the body of a parent component does not affect the appearence of the parent component.

See<apex:dataTable>for an example of facets.



basically the facet gives u more control options on the standard element attributes..

 

in the sample I provided, it is the control of the column header, which by default takes the data name..

or headervalue, which is just text..

if u look at the VF guide for apex column, in the description on the right, you will see the 'supported' facet names for that element type..

studzeystudzey

Just wanted to clarify something: 

1. It says that commandLink can only occur between <apex:form> </apex:form>? Where do I put the form?

2. I just don't understand the following lines:

a) If my custom objects name is "Trading_Contract__c", what should i replace and where. And lets say I want to sort by date (the field is called Date__c) - then where do I placethat?

 

I'm still a bit of a nub...

 

 


 

<apex:column > 
<apex:facet name="header">
<apex:commandLink action="{!ViewData}" value="Asset
 Name{!IF(sortExpression=='Asset',IF(sortDirection='ASC','▼','▲'),'')}" id="assetSort">
<apex:param value="Asset" name="column" assignTo="{!sortExpression}" ></apex:param>
</apex:commandLink>
</apex:facet>
<apex:outputtext value="{!contract.asset.name}"/>
</apex:column>


 

sdetweilsdetweil

ok, the way this code works..

 

the sorter needs to know what column you are talking about.

so this code uses the column 'header' label to identify what column order is being changed

 

the <apex:param value="Asset" name="column" assignTo="{!sortExpression}"

says to set the shared controller variable with the value 'Asset', when the commandlink is clicked.

 

the value="Asset Name{!IF(sortExpression=='Asset',IF(sortDirection='ASC','▼','▲'),'')}"

dynamically builds the column header string with 'Asset Name' and  the up/down arrows for sort direction,

ONLY if this 'Asset' column is the one being sort displayed.

 

you can add the facet and the two variables without the actual sorting code to see the UI change.

 

I didn't show the whole page form.

 

you would have <page><form><pageblock><pageblockTable><column> ...

 

so YOU need to decide, how YOU want to communicate between your page and column and the controller in the back that does the sorting work. in MY sample I used an arbitrary string that means 'the asset object.'  only the controller code knows the REAL data

you can use numbers, or whatever u like.  again, this is all hand built.

 

studzeystudzey

Ok,

 

My page is rendered as PDF, does that affect anything? I.e. No clickon on any of the items on the list will occur?

 

Thanks again,

 

sdetweilsdetweil

well, yeh.. but your code above doesn't have pdf in it at all.

 

none of the APEX code can work of course..

 

how does the PDF get generated?


Sam

studzeystudzey

It's for a contract in particular...so the visual force page has a "renderAs=PDF" clause at the top, and then I attach the visual force page to a button...I'm not sure if there is a better solution???

 

What i'm trying to do is order the one list of relating objects (to this object) in a correct order. I'm adding the entire page's code to the bottom so you can see - forgive me if it will cause an inconvenience..

 

<apex:page showHeader="false" renderAs="pdf"  standardController="Trading_Contract__c">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<body>

 

<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>

 

</body></html></apex:page>

 

Starz26Starz26

If you just want to display the data in a specific sort order upon render it is very simple:

 

In the controller when you do the SOQL, just add ...... ORDER BY: FIELD]; to the SOQL

 

Bingo, your data will be displayed in the order you needed.

 

This will not allow the user to select the sort order.

 

I see you are using a standard controller..... To do this you will need to create a custom controller or controller extension and manipulate the data....

sdetweilsdetweil

yeh.. HOW to get the data sorted in another issue..

 

getting it from the database sorted is easy as u mentioned

 

whether u want to REFETCH the data using the SOQL order by to do the sorting everytime for the selected columns is a programming,

response time, data usage issu.

 

 

 

studzeystudzey

Starz26 wrote:

If you just want to display the data in a specific sort order upon render it is very simple:

 

In the controller when you do the SOQL, just add ...... ORDER BY: FIELD]; to the SOQL

 

Bingo, your data will be displayed in the order you needed.

 

This will not allow the user to select the sort order.

 

I see you are using a standard controller..... To do this you will need to create a custom controller or controller extension and manipulate the data....


Hi, thanks guys. But what do you mean with :"when you do the SOQL"?  Did I do an SOQL? Is there another way to the way that I am doing this? I thought about doing an extension but I cant get it to work... :(

studzeystudzey

Where do I add the ORDER BY: FIELD exactly? Im confused (and sorry, new to SF...)   

 

:(((

sdetweilsdetweil

well, u had to get the data out of the SF database.. and SOQL is the database access language..

 

its like SQL, select fields from xxxx wherre conditions ordey by field sort_order (ascending, decending)

 

see the SOQL syntax online doc..

 

just a google search away

 

Sam

studzeystudzey
Thanks sdetweil . The thing that confuses me is that I only connected a visual force page to the button. I didnt write any queries? This because, once I clicked on one of my custom object "items" , I just had to click on a button which ran the visual force code that I posted previously... So where would I go about writing the query? Must I write an extension to a class or something? It's not actually struggling with the syntax so much...is more a matter of - where do I write the code? 
Maybe it's just too late at night...

sdetweil wrote:

well, u had to get the data out of the SF database.. and SOQL is the database access language..

 

its like SQL, select fields from xxxx wherre conditions ordey by field sort_order (ascending, decending)

 

see the SOQL syntax online doc..

 

just a google search away

 

Sam


 

Starz26Starz26

You are correct, you would have to write a custom controller or controller extension depending on your needs.

This was selected as the best answer
studzeystudzey

Starz26 wrote:

You are correct, you would have to write a custom controller or controller extension depending on your needs.


Thanks man. Will try it first thing and then will report back if I have issues with it. Thanks for the help