+ Start a Discussion
bsil bsilbsil bsil 

Sorting PageBlockTable with column wise

hi,

I am facing problem in sorting records in pageblock table see the blow code was working fine for sorting recordes based on column.
when i am going to filter the records based on listview it was not working
this is the code for filtering records based on listview
if(AccFilterId != null)
                {
                    AccSetController.setFilterId(AccFilterId);
                }
this i used in Standardsetcontroller in the apex class
if i will not use the above code for filetering it will work for sorting but it will not filter based on listview.
see the total visualforce and apex code below

Kindly suggest me how to fix this issue.
<apex:page controller="newProjectCreationController" sidebar="false" docType="html-5.0">
    
    <style type="text/css">
        .loadingIcon {
        background-image: url(/img/loading.gif);
        width: 16px;
        height: 16px;
        }
       
    </style>
    <script type="text/javascript">
     function doCheckboxChange(cb,itemId){
        //alert(itemId);    
        if(cb.checked==true){
            aSelectItem(itemId);
        }
        else{
            aDeselectItem(itemId);
        }
    }
    
    function checkAll(cb,cbid)
    {
        var inputElem = document.getElementsByTagName("input");
        for(var i=0; i<inputElem.length; i++)
        {
            
            if(inputElem[i].id.indexOf(cbid)!=-1){
                inputElem[i].checked = cb.checked;
            }
        }
    }
    </script>
    
    <apex:form id="pageForm">
        
        <apex:pageBlock id="block">
        
            <div align="left">
               <b> Views:  </b>
                <apex:selectList value="{!AccFilterId}" size="1" id="filterMenu">
                    <apex:selectOptions value="{!AccountExistingViews}"></apex:selectOptions>
                    <apex:actionSupport event="onchange"  action="{!resetFilter}" rerender="mpb" status="ajaxStatus"/>
                </apex:selectList>
                
                <apex:actionStatus id="ajaxStatus" startText="Loading..."  stopText=""/>
            </div> 
           
        </apex:pageBlock>
        <apex:pageBlock title="List Of Accounts" id="mpb">
            <apex:actionStatus startText=" (sorting...)" stopText=" (done)" id="TableUpdateStatus"/>
            <apex:actionFunction name="aSelectItem" action="{!doSelectItem}" rerender="table,count">
                <apex:param name="contextItem" value="" assignTo="{!contextItem}"/>
            </apex:actionFunction>
            <apex:actionFunction name="aDeselectItem" action="{!doDeselectItem}" rerender="table,count">
                <apex:param name="contextItem" value="" assignTo="{!contextItem}"/>
            </apex:actionFunction>
            <apex:pageBlockTable value="{!CurrentList}" var="c" id="table" >
                <apex:column >
                    <apex:facet name="header">
                        <apex:inputcheckbox value="{!fals}">
                        <apex:actionSupport event="onclick" onsubmit="checkAll(this,'check')" rerender="count,allcount" />
                        </apex:inputCheckbox>                          
                    </apex:facet>
                    <apex:inputcheckbox value="{!c.checked}" id="check" onchange="doCheckboxChange(this,'{!c.oaccount.Id}')" >                           
                    </apex:inputcheckbox> 
                    <apex:actionStatus id="selected" startText="Loading..."  stopText=""/>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink action="{!SortToggle}" rerender="mpb" status="TableUpdateStatus">
                            <apex:param name="SortField" value="Name" assignTo="{!SortField}" />
                            <apex:outputText value="{!$ObjectType.Account.Fields.Name.Label}{!IF(SortField=='Name',IF(SortDirection='asc','▲','▼'),'')}" />
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLink value="/{!c.oaccount.id}" target="_blank">{!c.oaccount.name}</apex:outputlink>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink action="{!SortToggle}" rerender="mpb" status="TableUpdateStatus">
                            <apex:param name="SortField" value="Billingcity" assignTo="{!SortField}" />
                            <apex:outputText value="{!$ObjectType.Account.Fields.Billingcity.Label}{!IF(SortField=='Billingcity',IF(SortDirection='asc','▲','▼'),'')}" />
                        </apex:commandLink>
                    </apex:facet>
                     <apex:outputLabel >{!c.oaccount.billingcity}</apex:outputLabel>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink action="{!SortToggle}" rerender="mpb" status="TableUpdateStatus">
                            <apex:param name="SortField" value="type" assignTo="{!SortField}" />
                            <apex:outputText value="{!$ObjectType.Account.Fields.type.Label}{!IF(SortField=='type',IF(SortDirection='asc','▲','▼'),'')}" />
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLabel >{!c.oaccount.type}</apex:outputLabel>
                </apex:column>
                <apex:column >
                    <apex:facet name="header">
                        <apex:commandLink action="{!SortToggle}" rerender="mpb" status="TableUpdateStatus">
                            <apex:param name="SortField" value="phone" assignTo="{!SortField}" />
                            <apex:outputText value="{!$ObjectType.Account.Fields.phone.Label}{!IF(SortField=='phone',IF(SortDirection='asc','▲','▼'),'')}" />
                        </apex:commandLink>
                    </apex:facet>
                    <apex:outputLabel >{!c.oaccount.phone}</apex:outputLabel>
                </apex:column>
               
            </apex:pageBlockTable>                   
            <apex:outputLabel value="[{!SelectedCount} records selected]" id="count"/>
            <apex:commandButton action="{!doPrevious}" rendered="{!hasPrevious}" value="Previous" style="color:red" rerender="out,table,table1"/>
            <apex:outputLabel rendered="{!NOT(hasPrevious)}" value="Previous" style="color:red" /> 
            <apex:outputLabel value=" (Page {!pageNumber} of {!totalPages}) " style="color:Blue" /> 
            <apex:commandButton action="{!doNext}" rendered="{!hasNext}" value="Next" style="color:green" rerender="out,table,table1"/>
            <apex:outputLabel rendered="{!NOT(hasNext)}" value="Next" style="color:green"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>



Apex Controller:
 
public with sharing class newProjectCreationController{
  
    
    private String QueryAccount;
     public String SortFieldSave;
    
    //public Integer Selectedaccid{get;set;}
    public Boolean showError{get;set;}
    public String a { get; set; }
    public String url;
    
    public boolean vBoolean = false;
    public integer noofadded{get;set;}
    //  private String baseQuery = 'Select ID, Name,phone,rating,accountnumber FROM Account ORDER BY NAME ASC Limit 5000';
    public String AccFilterId {get; set;}
    public string RecPerPage {get; set;}
    public list<SelectOption> RecPerPageOption {get; set;} 
    public String contextItem{get;set;}
    public String contextItem1{get;set;}

    public set<id> accountids;
   private List<accountSet> accountSetList{get;set;}
    public set<Account> selectedAccount;
    public boolean fals{get;set;}
      
        
    public newProjectCreationController()
    {
       
       
        
        fals=false;
        this.accountSetList = new List<accountSet>();
        this.selectedAccount = new set<account>();
        this.accountids = new set<id>();
        //records for page initialization
        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'; //default records per page
       BuildQuery(); 
    }
    public ApexPages.StandardSetController AccSetController {
        get{            
            if(AccSetController == null){
                AccSetController = new ApexPages.StandardSetController(Database.getQueryLocator(QueryAccount));
                //AccSetController.setPageSize(pageSize);
                this.AccSetController.setpageNumber(1);
                // sets the number of records in each page set
                this.AccSetController.setPageSize(Integer.valueOf(RecPerPage));
                // We have to set FilterId after Pagesize, else it will not work
                if(AccFilterId != null)
                {
                    AccSetController.setFilterId(AccFilterId);
                }
            }
            return AccSetController;
        }set;
    }
    
    //public newProjectCreationController(ApexPages.StandardSetController c) {   }
    
    public List<accountSet> getCurrentList() { 
          
        updateSelectedAccount();
        accountSetList = new List<accountSet>();
        for (account category : (List<account>)AccSetController.getRecords()){
            if(selectedaccount.contains(category))
                accountSetList.add(new accountSet(category,'true'));
            else
                accountSetList.add(new accountSet(category));
        }
        
        fals=false;
        system.debug('Allaccounts   '+accountSetList.size());
        return accountSetList;
    }
     
    
    
    Private void updateSelectedAccount(){
        //contactSetList.clear();
        
        for(accountSet cs:accountSetList ){
            if(cs.checked)
                selectedAccount.add(cs.oaccount);
            else{
                if(selectedAccount.contains(cs.oaccount))
                    selectedAccount.remove(cs.oaccount);
                
            }
            
        }
    }
    
    public class AccountSet {
        public Boolean checked{ get; set; }
        public account oAccount { get; set;}
        public AccountSet(){
            oAccount = new Account();
            checked = false;
        }
        public AccountSet(account a){
            oAccount = a;
            checked = false;
        }
        public accountSet(account a,string s){
            oAccount = a;
            checked = true;
        }
    }
    
    
    //Get all available list view for Account
    public SelectOption[] getAccountExistingViews(){
        return AccSetController.getListViewOptions();
    }
    
    /**
    * Reset List View
    */
    public PageReference resetFilter()
    {  
        selectedaccount.clear();
        accountSetList.clear();
        AccSetController = null;
        AccSetController.setPageNumber(1);
        
        return null;
    }
    
    
    /*
    *   return count of selected items
    */
    
    public void doSelectItem(){
        
        if(contextItem!=null)
            accountids.add(this.contextItem);
        
    }
    
    public void doDeselectItem(){
        
        accountids.remove(this.contextItem);
        
    }
    
    /*
    *   return count of selected items
    */
    public Integer getSelectedCount(){
        
        accountids.clear();
        for(account acc: selectedaccount){
            accountids.add(acc.id);
        }
        //selectedaccount.clear();
        return this.accountids.size();
        
    }
    
    /*
    *   advance to next page
    */
    public void doNext(){
        
        if(this.AccSetController.getHasNext())
            this.AccSetController.next();
        
    }
    
    /*
    *   advance to previous page
    */
    public void doPrevious(){
        
        if(this.AccSetController.getHasPrevious())
            this.AccSetController.previous();
        
    }
    /*
    *   return whether previous page exists
    */
    public Boolean getHasPrevious(){
        
        return this.AccSetController.getHasPrevious();
        
    }
    
    /*
    *   return whether next page exists
    */
    public Boolean getHasNext(){
        
        return this.AccSetController.getHasNext();
        
    }
    
    /*
    *   return page number
    */
    public Integer getPageNumber(){
        
        return this.AccSetController.getPageNumber();
        
    }
    
    /*
    *    return total pages
    */
    Public Integer getTotalPages(){
        
        Decimal totalSize = this.AccSetController.getResultSize();
        Decimal pageSize = this.AccSetController.getPageSize();
        
        Decimal pages = totalSize/pageSize;
        
        return (Integer)pages.round(System.RoundingMode.CEILING);
    }
    
    
   
     /***
    * BuildQuery - build query command for list selection change
    ***/
    public void BuildQuery() {
        AccSetController = null;
        QueryAccount = 'SELECT Name,Id,Type,Phone,BillingCity' +
            ' FROM Account'; 
 
        QueryAccount += ' ORDER BY ' + String.escapeSingleQuotes(SortField) + ' ' + String.escapeSingleQuotes(SortDirection) + ' LIMIT 1000';
        
        system.debug('QueryAccount:' + QueryAccount);
        
    }
     /***
    * SortToggle - toggles the sorting of query from asc<-->desc
    ***/
    public void SortToggle() {
        SortDirection = SortDirection.equals('asc') ? 'desc' : 'asc';
      
        BuildQuery();
    }
  /***
    * SortField - return sort by field. Default to Name
    ***/
    public String SortField {
        get { if (SortField == null) {SortField = 'Name'; } return SortField;  }
        set; 
    }
     /***
    * SortDirection - return sort direction. Default ascending(asc)
    ***/
    public String SortDirection {
        get { if (SortDirection == null) {  SortDirection = 'asc'; }
        
         return SortDirection;  }
        set;
    }
 
}

thanks in advance............
 
Navee RahulNavee Rahul

this may help you mate,check this

http://www.datatables.net/