2

I already have the Controller:

public class ContactListController {
    public List<Contact> ContactList {
        get {
            String sortExp = 'name';
            String sortDirection = 'ASC';

              if(ContactList == Null){
              ContactList = [SELECT Account.Name, Name, Phone, Email, MailingCity, MailingPostalCode, MailingCountry, MailingState FROM Contact WHERE Account.Name != Null];

              }

        return ContactList;
        }

        set;
    }
}

and my Page is:

<apex:page controller="ContactListController">
    <apex:form >
        <apex:pageBlock title="List Of Contacts with Accounts">
                <apex:pageBlockTable value="{!ContactList}" var="contact" cellpadding="0" cellspacing="0" width="50%">
                    <apex:column value="{!contact.Account.Name}"/>                  
                    <apex:column value="{!contact.Name}"/>-->
                    <apex:column value="{!contact.Phone}"/>
                    <apex:column value="{!contact.Email}"/>
                    <apex:column value="{!contact.MailingCity}"/>
                    <apex:column value="{!contact.MailingPostalCode}"/>
                    <apex:column value="{!contact.MailingState}"/>                   
                </apex:pageBlockTable>               
        </apex:pageBlock>
    </apex:form> 
</apex:page>

Can any one known that?

  • 1
    One approach is to do all the work at the client-side using e.g. DataTables, though sorting can take extra work and JavaScript skills are needed. – Keith C Sep 17 '16 at 12:19
0

You can use jQuery Tablesorter library to sort the columns.

Code will look like this and it is working perfectly at my developer org.

<apex:page controller="ContactListController">
<apex:includeScript value="//code.jquery.com/jquery-2.1.1.min.js"/>
<apex:includeScript value="//cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.18.3/js/jquery.tablesorter.js"/>
<apex:includeScript value="//cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.18.3/js/jquery.tablesorter.widgets.js"/> 
<apex:stylesheet value="//cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.18.3/css/theme.jui.css"/>
<script>
$(document).ready(function(){
    $("table").tablesorter({
    theme : 'jui', 
    headerTemplate : '{content} {icon}',
    widgets : ['uitheme', 'zebra'],
    widgetOptions : {
      zebra   : ["even", "odd"],
    }
 });
});
</script>
<apex:form >
    <apex:pageBlock title="List Of Contacts with Accounts">
            <apex:pageBlockTable value="{!ContactList}" var="contact" cellpadding="0" cellspacing="0" width="50%">
                <apex:column value="{!contact.Account.Name}"/>                  
                <apex:column value="{!contact.Name}"/>
                <apex:column value="{!contact.Phone}"/>
                <apex:column value="{!contact.Email}"/>
                <apex:column value="{!contact.MailingCity}"/>
                <apex:column value="{!contact.MailingPostalCode}"/>
                <apex:column value="{!contact.MailingState}"/>                   
            </apex:pageBlockTable>               
    </apex:pageBlock>
</apex:form> 
</apex:page>
1

friends I got the answer now, Controller class: public class SortOfContactListController { public List ContactListTable {get; set;} public String ExpressionOfSort = 'name'; public String DirectionOfSort = 'ASC';

public SortOfContactListController() {
    ContactListTable = new List<Contact>();
}
public String ExpressionSort {
    get {
        return ExpressionOfSort;
    }
    set {
        If(value == ExpressionOfSort) {
            DirectionOfSort = (DirectionOfSort == 'ASC')? 'DESC' : 'ASC';
        }
        else {
            DirectionOfSort = 'ASC';
            ExpressionOfSort = value;
        }
    }

}

public String getDirectionOfSort() {
    If(ExpressionOfSort == Null || ExpressionOfSort == '') {
        return 'DESC';
    }
    else {
        return DirectionOfSort;
    }
}

public void setDirectionOfSort(String value) {
    DirectionOfSort = value;
}

public List<Contact>getContacts() {
    return ContactListTable;
}

public PageReference ViewData() {
    String FullSortExpression = ExpressionOfSort + ' ' + DirectionOfSort;
    system.debug('ExpressionOfSort:::::'+ExpressionOfSort);
    system.debug(DirectionOfSort);

    //String Queryitem = new String();
   String Queryitem = 'SELECT Id, Name, Account.Name, Phone, Email, MailingCity, MailingCountry, MailingPostalCode FROM Contact WHERE Account.Name != Null ORDER BY ' + FullSortExpression + ' Limit 1000';

    ContactListTable = DataBase.query(Queryitem);
    system.debug(ContactListTable);
    return Null;
}

}

Visual Page :

             <apex:column >
                 <apex:facet name="header">   
                   <apex:commandLink action="{!ViewData}" value="Contact Name{!IF(ExpressionSort=='name',IF(DirectionOfSort == 'ASC', '▼', '▲'),'')}">
                     <apex:param value="name" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                   </apex:commandLink>
                 </apex:facet>
                 <apex:outputLink value="/{!contact.Id}" target="_blank">{!contact.Name}</apex:outputLink>
              </apex:column>

              <apex:column value="{!contact.Account.Name}">
                 <apex:facet name="header">
                   <apex:commandLink action="{!ViewData}" value="Account Name{!IF(ExpressionSort=='Account Name',IF(DirectionOfSort == 'ASC','▼','▲'),'')}">
                     <apex:param value="Account Name" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                   </apex:commandLink>
                 </apex:facet>
              </apex:column>

              <apex:column value="{!contact.Phone}">
                 <apex:facet name="header">
                   <apex:commandLink action="{!ViewData}" value="Phone{!IF(ExpressionSort=='Phone',IF(DirectionOfSort == 'ASC','▼','▲'),'')}">
                     <apex:param value="Phone" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                   </apex:commandLink>
                 </apex:facet>
              </apex:column>

              <apex:column value="{!contact.Email}">
                 <apex:facet name="header">
                   <apex:commandLink action="{!ViewData}" value="Email{!IF(ExpressionSort=='Email',IF(DirectionOfSort == 'ASC','▼','▲'),'')}">
                     <apex:param value="Email" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                   </apex:commandLink>
                 </apex:facet>
              </apex:column>

              <apex:column value="{!contact.MailingCity}">
                 <apex:facet name="header">
                   <apex:commandLink action="{!ViewData}" value="Mailing City{!IF(ExpressionSort=='Mailing City',IF(DirectionOfSort == 'ASC','▼','▲'),'')}">
                     <apex:param value="Mailing City" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                   </apex:commandLink>
                 </apex:facet>
              </apex:column>

              <apex:column value="{!contact.MailingCountry}">
                 <apex:facet name="header">
                   <apex:commandLink action="{!ViewData}" value="Mailing Country{!IF(ExpressionSort=='Mailing Country',IF(DirectionOfSort == 'ASC','▼','▲'),'')}">
                     <apex:param value="Mailing Country" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                   </apex:commandLink>
                 </apex:facet>
              </apex:column>

              <apex:column value="{!contact.MailingPostalCode}">
                 <apex:facet name="header">
                   <apex:commandLink action="{!ViewData}" value="Mailing Postal Code{!IF(ExpressionSort=='Mailing Postal Code',IF(DirectionOfSort == 'ASC','▼','▲'),'')}">
                     <apex:param value="Mailing Postal Code" name="column" assignTo="{!ExpressionSort}" ></apex:param>
                   </apex:commandLink>
                 </apex:facet>
              </apex:column>

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

Thanks Mohan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.