ads

Wednesday 17 August 2016

Shorting Records Based on Alphabet in Visualforce Page



Controller Code : -


public with sharing class OpportunitySortingController {
  
    private list<opportunitySubClass> opportunityList {get; set;}
    private set<Id> opportunitySelectedSet;
    public Integer opportunitySelectedCount {get; set;}
    public String SelectedOneopportunity {get; set;}
    
   
    public list<String> AlphaList {get; set;}
    public String AlphaFilter {get; set;}
    public String SearchName {get; set;}
    public String SearchBillingAddress {get; set;} 
    private String SaveSearchName;
    private String SaveSearchBillingAddress;
    private String Queryopportunity;
    
 
    public String RecPerPage {get; set;}
    public list<SelectOption> RecPerPageOption {get; set;}  
    public String SortFieldSave;
    
   
    public OpportunitySortingController(){
        opportunityList = new list<opportunitySubClass>();
        opportunitySelectedSet = new set<Id>();
        
    
        RecPerPageOption = new list<SelectOption>();
        RecPerPageOption.add(new SelectOption('10','10'));
        RecPerPageOption.add(new SelectOption('25','25'));
        RecPerPageOption.add(new SelectOption('50','50'));
        RecPerPageOption.add(new SelectOption('100','100'));
        RecPerPageOption.add(new SelectOption('200','200'));
        RecPerPage = '10'; 
        
        AlphaList = new list<String> {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'Other', 'All'};
            SortFieldSave = SortField;
        
     
        if (apexpages.currentpage().getparameters().get('alpha') == null) {
            AlphaFilter = 'All';
        } else {
            AlphaFilter = apexpages.currentpage().getparameters().get('alpha');
        }
      
        BuildQuery();  
    }
    
    public ApexPages.StandardSetController StdSetControlleropportunity {
        get {
            if(StdSetControlleropportunity == null) {
                StdSetControlleropportunity = new ApexPages.StandardSetController(Database.getQueryLocator(Queryopportunity));
               
                StdSetControlleropportunity.setPageSize(Integer.valueOf(RecPerPage));
            }
            return StdSetControlleropportunity;
        }
        set;
    }
  
    public list<opportunitySubClass> getCurrentopportunityList() {
        UpdateopportunitySelectedSet();
        
        opportunityList = new list<opportunitySubClass>();
        for (opportunity a : (list<opportunity>)StdSetControlleropportunity.getRecords()) {
            opportunityList.add(new opportunitySubClass(a, opportunitySelectedSet.contains(a.Id)));
        }
        return opportunityList;
    }
    
    public void UpdateopportunitySelectedSet(){
        for(opportunitySubClass a : opportunityList ){
            if(a.aCheckBox == true) {
                opportunitySelectedSet.add(a.aopportunity.Id);
            } else {
                if(opportunitySelectedSet.contains(a.aopportunity.Id)) {
                    opportunitySelectedSet.remove(a.aopportunity.Id);
                }
            }
        }  
        opportunitySelectedCount = opportunitySelectedSet.size();
    }
    
   
    public PageReference Searchopportunity() {
        SaveSearchName = SearchName;
        SaveSearchBillingAddress = SearchBillingAddress;
        
        BuildQuery();
        
        return null;
    }
    
    
    public void BuildQuery() {
        StdSetControlleropportunity = null;
        String QueryWhere = '';
        
        if (AlphaFilter == null || AlphaFilter.trim().length() == 0) {
            AlphaFilter = 'All';
        }
        
        Queryopportunity = 'SELECT Id, Name, StageName, Probability ' +
            ' FROM opportunity'; 
        
        if (AlphaFilter == 'Other') {
            QueryWhere = BuildWhere(QueryWhere, '(' + String.escapeSingleQuotes(SortField) + ' < \'A\' OR ' + 
                                    String.escapeSingleQuotes(SortField) + ' > \'Z\') AND (NOT ' + 
                                    String.escapeSingleQuotes(SortField) + ' LIKE \'Z%\') ');
        } else if (AlphaFilter != 'All') {
            QueryWhere = BuildWhere(QueryWhere, '(' + String.escapeSingleQuotes(SortField) + ' LIKE \'' + String.escapeSingleQuotes(AlphaFilter) + '%\')' );
        }
        
        if (SaveSearchName != null) {
            QueryWhere = BuildWhere(QueryWhere, ' (Name LIKE \'%' + String.escapeSingleQuotes(SaveSearchName) + '%\')');
        }
        if (SaveSearchBillingAddress != null) {
            QueryWhere = BuildWhere(QueryWhere, '((BillingStreet LIKE \'%' + String.escapeSingleQuotes(SaveSearchBillingAddress) + '%\') or' +
                                    ' (BillingCity LIKE \'%' + String.escapeSingleQuotes(SaveSearchBillingAddress) + '%\') or' +
                                    ' (BillingState LIKE \'%' + String.escapeSingleQuotes(SaveSearchBillingAddress) + '%\') or' +
                                    ' (BillingPostalCode LIKE \'%' + String.escapeSingleQuotes(SaveSearchBillingAddress) + '%\') or' +
                                    ' (BillingCountry LIKE \'%' + String.escapeSingleQuotes(SaveSearchBillingAddress) + '%\'))');
        }
        
        Queryopportunity += QueryWhere;
        Queryopportunity += ' ORDER BY ' + String.escapeSingleQuotes(SortField) + ' ' + String.escapeSingleQuotes(SortDirection) + ' LIMIT 10000';
        
        system.debug('Queryopportunity:' + Queryopportunity);
    }
    
   
    public String BuildWhere(String QW, String Cond) {
        if (QW == '') {
            return ' WHERE ' + Cond;
        } else {
            return QW + ' AND ' + Cond;
        }
    }
    
    public String SortDirection {
        get { if (SortDirection == null) {  SortDirection = 'asc'; } return SortDirection;  }
        set;
    }
    
   
    public String SortField {
        get { if (SortField == null) {SortField = 'Name'; } return SortField;  }
        set; 
    }
    
  
    public void SortToggle() {
        SortDirection = SortDirection.equals('asc') ? 'desc NULLS LAST' : 'asc';
       
        if (SortFieldSave != SortField) {
            SortDirection = 'asc';
            AlphaFilter = 'All';
            SortFieldSave = SortField;
        }
    
        BuildQuery();
    }
    
 
    public PageReference DoSomethingOne() {
        system.debug('SelectedOneopportunity: ' + SelectedOneopportunity);
        return null;
    }
   
    public PageReference DoSomethingMany() {
        for (Id opportunityId : opportunitySelectedSet) {
            system.debug('Checked: ' + opportunityId);
        }
        return null;
    }
    
  
    public class opportunitySubClass {
        public Boolean aCheckBox {get;set;}
        public opportunity aopportunity {get;set;}
        
      
        public opportunitySubClass(opportunity a, Boolean chk){
            aopportunity = a;
            aCheckBox = chk;
        }
    }
}


Visualforce Page :-



<apex:page controller="OpportunitySortingController">
   <apex:form >
      
      <apex:pageBlock id="TablePanel">
        
 
         <apex:actionStatus id="TableUpdateStatus">
            <apex:facet name="stop">
               <apex:pageBlockTable value="{!CurrentopportunityList}" var="a">                  
                  <apex:column >
                     <apex:facet name="header">
                        <apex:commandLink action="{!SortToggle}" rerender="TablePanel" status="TableUpdateStatus">
                           <apex:param name="SortField" value="Name" assignTo="{!SortField}" />
                           <apex:outputText value="{!$ObjectType.opportunity.Fields.Name.Label}{!IF(SortField=='Name',IF(SortDirection='asc','▲','▼'),'')}" />
                        </apex:commandLink>
                     </apex:facet>
                     <apex:outputLink value="/{!a.aopportunity.Id}" target="_blank">{!a.aopportunity.Name}</apex:outputlink>
                  </apex:column>
                  <apex:column >
                     <apex:facet name="header">
                        <apex:commandLink action="{!SortToggle}" rerender="TablePanel" status="TableUpdateStatus">
                           <apex:param name="SortField" value="StageName" assignTo="{!SortField}" />
                           <apex:outputText value="{!$ObjectType.opportunity.Fields.StageName.Label}{!IF(SortField=='StageName',IF(SortDirection='asc','▲','▼'),'')}" />
                        </apex:commandLink>
                     </apex:facet>
                     <apex:outputField value="{!a.aopportunity.StageName}" />
                  </apex:column>
                  <apex:column >
                     <apex:facet name="header">
                        <apex:commandLink action="{!SortToggle}" rerender="TablePanel" status="TableUpdateStatus">
                           <apex:param name="SortField" value="Probability" assignTo="{!SortField}" />
                           <apex:outputText value="{!$ObjectType.opportunity.Fields.Probability.Label}{!IF(SortField=='Probability',IF(SortDirection='asc','▲','▼'),'')}" />
                        </apex:commandLink>
                     </apex:facet>
                     <apex:outputField value="{!a.aopportunity.Probability}" />
                  </apex:column>                 
               </apex:pageBlockTable>
            </apex:facet>
         </apex:actionStatus>
      </apex:pageBlock>
   </apex:form>
</apex:page>


OutPut Screen 1 :-



OutPut Screen 2 :-


No comments:

Post a Comment