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
JSchneiderJSchneider 

Sort apex:dataTable in Visualforce Page

<apex:dataTable value="{! Quote.QuoteLineItems }" var="line" rowClasses="odd,even" columnClasses="columnBorders" headerClass="dataTableHeader" cellpadding="5px" cellspacing="0" >
<apex:column styleClass="firstColumn" headerValue="Product" value="{! line.Product2.Name }" />
<apex:column headerValue="Description" value="{! line.Description__c }" />
<apex:column styleClass="rightAlignColumn" headerValue="Quantity" value="{! line.Quantity }" />
<apex:column styleClass="rightAlignColumn" headerValue="Unit Price" value="{! line.UnitPrice }" />
<apex:column styleClass="totalsColumn" headerValue="Total Price" value="{! line.TotalPrice }" />
</apex:dataTable>
There is a standard field on QuoteLineItem called SortOrder that I'd like to sort this by.

Any help would be great!
JSchneiderJSchneider
<apex:page standardController="Quote" renderAs="PDF" showHeader="false" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false">
    <html>
        <head>            
            <style type="text/css">
                @page {
                    size: US-Letter;
                    margin: 1.27cm;

                    @bottom-center {
                        content: "Years";
                        font-family: Arial Unicode MS;
                        font-size:10px;
                        color:#555555
                    }
                }
                table {
                    page-break-inside: always;
                    width:100%
                }
                body {
                    font-family: Arial Unicode MS;
                    font-size:10px;
                    color:#333333
                }
                .columnBorders {
                    border-right: 0.5px solid #626262;
                    border-left: 0.5px solid #626262;
                }
                .odd {
                    font-size:11px;
                }
                .even {
                    font-size:11px;
                    background-color:#EEEEEE;
                }
                .dataTableHeader {
                    color:#FFFFFF;
                    background-color:#767676;
                    font-size:11px;
                    padding-right:5px;
                }
                .centered {
                    text-align:center
                }
                td.oddCol {
                    width:15%;
                }
                td.evenCol {
                    width:35%;
                    color:#555555
                }
                td.totalsBar {
                    background-color:#DEE5EC;
                    font-size:11px;
                }
                td.totalSpacing {
                    width:60%
                }
                td.grandTotalLabel {
                    width:30%;
                    font-size:11px;
                }
                td.grandTotal {
                    width:10%;
                    text-align:right;
                    font-size:11px;
                }
                div.spacing {
                    margin-top:10px;
                }
                .rightAlignColumn {
                    text-align:right
                }
                .leftAlignColumn {
                    text-align:left
                }
                .totalsColumn {
                    text-align:right;
                    border-right:none
                }
                .firstColumn {
                    border-left:none;
                }
                
            </style>
        </head>
        
        <body>
            
            <apex:image width="710px" url="{! $Resource.Logo }"/>
            

            <br/>
            <br/>

            <table>
                <tr >
                    <td class="oddCol"><apex:outputLabel value="Contact Name" /></td>
                    <td class="evenCol"><apex:outputText value="{! Quote.Contact.Name }" /></td>
                    <td class="oddCol"><apex:outputLabel value="Quote Number" /></td>
                    <td class="evenCol"><apex:outputText value="{! Quote.QuoteNumber }" /></td>
                </tr>
                <tr>
                    <td class="oddCol"><apex:outputLabel value="Account Name" /></td>
                    <td class="evenCol"><apex:outputText value="{! Quote.Account.Name }" /></td>
                    <td class="oddCol"><apex:outputLabel value="Expiration Date" /></td>
                    <td class="evenCol">
                        <apex:outputText value="{0, date, M/d/YYYY}" >
                            <apex:param value="{! Quote.ExpirationDate }"/>
                        </apex:outputText>
                    </td>
                </tr>
                <tr>
                    <td class="oddCol"><apex:outputLabel value="Physical Address" /></td>
                    <td class="evenCol" rowspan="3">
                        <apex:outputText value="{! Quote.Account.BillingStreet }" /><br/>
                        <apex:outputText value="{! Quote.Account.BillingCity }," />
                        <apex:outputText value="{! Quote.Account.BillingState } " />
                        <apex:outputText value="{! Quote.Account.BillingPostalCode }" /><br/>
                        <apex:outputText value="{! Quote.Account.BillingCountry }" />
                    </td>
                    <td class="oddCol"><apex:outputLabel value="Terms" /></td>
                    <td class="evenCol"><apex:outputText value="{! Quote.Terms__c }" /></td>
                </tr>
                <tr>
                    <td/>
                    <!--<td/> hidden by address row span -->
                    <td class="oddCol"><apex:outputLabel value="Sales Rep" /></td>
                    <td class="evenCol"><apex:outputText value="{! Quote.Opportunity.Owner.Name }" /></td>
                </tr>
                <tr>
                    <td/>
                    <!--<td/> hidden by address row span -->
                    <td/>
                    <td/>
                </tr>
            </table>

            <br/>
            <br/>
            <apex:outputText value="*** THIS QUOTE EXPIRES ON {!Quote.Expiration_Month__c} {!Quote.Expiration_Day__c}, {!Quote.Expiration_Year__c}***"/>
            <br/>
            <br/>
            <apex:outputText value="{!Quote.Data_Conversion_Disclosure__c}"/>
            <br/>
            <br/>
            <apex:outputText value="{!Quote.Special_Notes__c}" />
            <br/>
            <br/>
            <apex:outputField value="{!Quote.Special_Terms__c}"/>
            <br/>
            <br/>
            
            <div>
                <apex:dataTable value="{! Quote.QuoteLineItems }" var="line" rowClasses="odd,even" columnClasses="columnBorders" headerClass="dataTableHeader" cellpadding="5px" cellspacing="0" >
                    <apex:column styleClass="firstColumn" headerValue="Product" value="{! line.Product2.Name }" />
                    <apex:column headerValue="Description" value="{! line.Description__c }" />
                    <apex:column styleClass="rightAlignColumn" headerValue="Quantity" value="{! line.Quantity }" />
                    <apex:column styleClass="rightAlignColumn" headerValue="Unit Price" value="{! line.UnitPrice }" />
                    <apex:column styleClass="totalsColumn" headerValue="Total Price" value="{! line.TotalPrice }" />
                </apex:dataTable>
                
                <div class="spacing" />
                
                <table>
                    <tr>
                        <td class="totalsBar" colspan="3">
                            <apex:outputLabel value="Totals" />
                        </td>
                    </tr>
                    <tr>
                        <td class="totalSpacing" />
                        <td class="grandTotalLabel" >
                            <apex:outputLabel value="Grand Total  " />
                        </td>
                        <td class="grandTotal" >
                            <apex:outputField style="text-align:right" value="{! Quote.GrandTotal }" />
                        </td>
                    </tr>
                </table>
                
            </div>
            
            <br/>
            <br/>
            <br/>
            <apex:outputPanel rendered="{! Quote.Custom_Development_Details__c != '' }" >
                <apex:outputField value="{! Quote.Custom_Development_Details__c }" />
            </apex:outputPanel>
            <br/>
            <br/>
            <br/>
            <div class="centered">
                <apex:outputText value="The items on this quote do not include any applicable sales tax. Any required sales taxes will be included on your invoice." />
                <br/><br/>
                <apex:outputField value="{!Quote.Outside_Sales_Footer_1__c}"/>
                <br/><br/>
                <apex:outputText value="Accepted By:________________________________ Date:_______________________" />
                <br/><br/>
                <apex:outputField value="{!Quote.Outside_Sales_Footer_2__c}"/>
                <br/><br/>
                <apex:outputField value="{!Quote.Outside_Sales_Footer_3__c}"/>
                <br/><br/>
                <apex:outputField value="{!Quote.Outside_Sales_Footer_4__c}"/>
                <br/><br/><br/>
                <apex:outputText value="****To order please sign and fax to 800 or email a scanned copy to "/>
                <apex:outputField value="{!Quote.SalesRepEmail__c}"/>
				<apex:outputText value=" ****"/>
            </div> 
        </body>
    </html>    
</apex:page>
I figured I'd include the whole thing.
Boris BachovskiBoris Bachovski
You'll need to write some javascript in oder to achieve that. Here is a good example and some resources to help you rebuild your existing table and enable sorting on any column:

http://www.systempartners.com/blog/visualforce-sortable-table
JSchneiderJSchneider
This is getting rendered as a PDF though, is what your suggesting still possible?
Boris BachovskiBoris Bachovski
I'm affraid you don't have access to any sorting functionality using the standard controller. You need to write a controller extension and sort the list by writing some apex. The closest example I could find is here - http://onlysalesforce.blogspot.com.au/2013/08/custom-sort-feature-in-salesforce-based.html