Thursday, March 13, 2014

Sorting Table Using Apex

Hey guys, i am back to blog  with sorting table.
In standard UI of salesforce, if we are looking any list view, then salesforce provides the sorting in both way(ASC && DESC). The same functionally may be achieved using apex. Here i am going to write the code of Sorting the table using the Apex Class and VF page.


********************Controller**************************

public class SortTableController{
    public List<Account> acc {get; set;}
    public string uparr {get; set;}
    public boolean NameUpArrow {get; set;}
    public boolean NameDownArrow {get; set;}
    public boolean TypeDownArrow {get; set;}
    public boolean TypeUpArrow {get; set;}
    public boolean CreateDownArrow {get; set;}
    public boolean CreateUpArrow {get; set;}
    Public integer i {get; set;}
    Public integer j {get; set;}
    Public integer k {get; set;}
    public SortTableController(){
        acc = new List<Account>();
        acc = [select id , name , createddate, type from account];
        uparr = '&darr;';
        NameUpArrow = false;
        NameDownArrow = false;
        TypeUpArrow = false;
        TypeDownArrow = false;
        CreateUpArrow = false;
        CreateDownArrow = false;
        i = 0;
        j = 0;
        k = 0;
    }
    //sort by name....
    public pagereference NameSort(){
        TypeUpArrow = false;
        TypeDownArrow = false;
        CreateUpArrow = false;
        CreateDownArrow = false;
        if(i == 0){
            NameDownArrow = true;
            NameUpArrow = false;
        }else if(i == 1){
            NameUpArrow = true;
            NameDownArrow = false;
        }
        if(NameDownArrow == true){
            acc = [select id , name , createddate, type from account Order By Name ASC];
            i = 1;
            NameUPArrow = false;
        }else if(NameUpArrow == true){
            acc = [select id , name , createddate, type from account Order By Name DESC];
            i = 0;
            NameDownArrow = false;
        } 
        return null;
    }
    //sort by type....
    public pagereference TypeSort(){
        NameUpArrow = false;
        NameDownArrow = false;
        CreateUpArrow = false;
        CreateDownArrow = false;
        if(j == 0){
            TypeDownArrow = true;
            TypeUpArrow = false;
        }else if(j == 1){
            TypeUpArrow = true;
            TypeDownArrow = false;
        }
        if(TypeDownArrow == true){
            acc = [select id , name , createddate, type from account Order By type ASC];
            j = 1;
            TypeUPArrow = false;
        }else if(TypeUpArrow == true){
            acc = [select id , name , createddate, type from account Order By Type DESC];
            j = 0;
            TypeDownArrow = false;
        } 
        return null;
    }
    //sort by created date....
    public pagereference CreateSort(){
        NameUpArrow = false;
        NameDownArrow = false;
        TypeUpArrow = false;
        TypeDownArrow = false;
        if(k == 0){
            CreateDownArrow = true;
            CreateUpArrow = false;
        }else if(k == 1){
            CreateUpArrow = true;
            CreateDownArrow = false;
        }
        if(CreateDownArrow == true){
            acc = [select id , name , createddate, type from account Order By CreatedDate ASC];
            k = 1;
            CreateUPArrow = false;
        }else if(CreateUpArrow == true){
            acc = [select id , name , createddate, type from account Order By CreatedDate DESC];
            k = 0;
            CreateDownArrow = false;
        } 
        return null;
    }
    
}
*****************************Page*****************************

<apex:page controller="SortTableController">
  <apex:form >
      <apex:pageBlock >
          <table width="60%" border="0">
              <tr>
                  <th>
                      <apex:commandLink value="Name" action="{!NameSort}"/>
                      <apex:image value="{!$Resource.UpArrow}" width="10px" height="10px" rendered="{!NameUpArrow}"></apex:image>
                      <apex:image value="{!$Resource.DownArrow}" width="10px" height="10px" rendered="{!NameDownArrow}"></apex:image>
                  </th>
                  <th>
                      <apex:commandLink value="Type" action="{!TypeSort}"/>
                      <apex:image value="{!$Resource.UpArrow}" width="10px" height="10px" rendered="{!TypeUpArrow}"></apex:image>
                      <apex:image value="{!$Resource.DownArrow}" width="10px" height="10px" rendered="{!TypeDownArrow}"></apex:image>
                  </th>
                  <th>
                      <apex:commandLink value="Created Date" action="{!CreateSort}"/>
                      <apex:image value="{!$Resource.UpArrow}" width="10px" height="10px" rendered="{!CreateUpArrow}"></apex:image>
                      <apex:image value="{!$Resource.DownArrow}" width="10px" height="10px" rendered="{!CreateDownArrow}"></apex:image>
                  </th>
              </tr>
              <apex:repeat value="{!acc}" var="ac">
                  <tr>
                      <td>
                          <apex:outputfield value="{!ac.name}"/>
                      </td>
                      <td>
                          <apex:outputfield value="{!ac.type}"/>
                      </td>
                      <td>
                          <apex:outputfield value="{!ac.createddate}"/>
                      </td>
                  </tr>
             </apex:repeat>
         </table>
     </apex:pageBlock>
  </apex:form>
</apex:page>


For live demo please visit http://vickygaur-developer-edition.ap1.force.com/SortTable

No comments:

Post a Comment