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
Chitra ThambirajanChitra Thambirajan 

How to sort page block table

Hi Everyone,

Am struggling with this for long time.Trying to add sorting for my pageblock table.

If all the columns contains direct values I can easily use standard controller even though it is server side.But it has some agrregate result,boolean values  also.I do not know how to add sorting for thses aggregate values.

If I use StandardController then the aggregate result sorting is not working.

If am using Enhanced page block table(V-2.25) the boolean sort is not working.

If I use Enhanced Pageblock table ADV(V-2.01) then the link field(first field is id field and made it as link to view the record) is not getting sorted.
I have all these fields in my table.

So how can I add sorting for these columns....???

Any idea..??

Thanks
Chitra
Ramu_SFDCRamu_SFDC
See if the below article is of any help

http://salesforcesource.blogspot.in/2008/11/adding-sorting-capability-to.html
RishavRishav
HIi chitra,
                    why don't you use the custom controller for this. your task will be much easier and you will achieve all functionality.
                    i will suggest you to use custom controller because our standard controller is having some limitation.

Thanks
Rishav
Chitra ThambirajanChitra Thambirajan
Hi Rishav,

Thank you for your reply..Am a newbie and not much familier with this...
Could you please elaborate your answer..I do not even know what is Custom Controller..
Whether this Custom Controller will work for all the columns which I have mentioned..?(link,boolean & aggregate result)

Thanks
Chitra
RishavRishav
hi chitra,
               First i will suggest you to go through this document "http://www.salesforce.com/us/developer/docs/pages/salesforce_pages_developers_guide.pdf
                  then you will get idea about the custom controller . 
  And if after that you still don't understand then i will give you a example to make you understand.

Thanks
Rishav
Chitra ThambirajanChitra Thambirajan
Sure Rishav..Thank you...Will read it and update you..
Chitra ThambirajanChitra Thambirajan
Hi Rishav,

Am reading the pdf document which you have shared...It seems the sorting will be happen in server side..So does it mean when ever I click table header it will again query the data from object for sorting..?Please correct me if am wrong.
If we do this sorting in client side it may not create any performance issue..
Please share your thoughts..


Thanks in advance
Chitra
RishavRishav
HI chitra,
                  hey i am little busy in my work .
                well, it seem you have a good mind.
                 ya definitely sorting will happen on server side and whenever you will click table header it will again query the data from object.
               There are different approach of sorting chitra,it totally depend upon you which approach you choose. if i need standard sorted data then i will sort it inside the controlller because it is more convenient.

And processing on the client side is a good idea,it will not create the performace issue.But tell me how you will sort it on the client side.

Thanks      

Chitra ThambirajanChitra Thambirajan
Thank you for spending some time in your busy schedule..Am really thankfull to you.
With the help of Jquery it is possible upto my understanding..I have read about Enhanced Page block table & Enhanced page block table ADV and applied it..both of them are client side sorting.It is light weight and working fine with the simple columns..
As I have mentioned in my question,one is having issue with boolean sorting and another one is having issue with link sorting(hyper linked id column).
The links which I have referred,
1. http://blogforce9.blogspot.in/2012/08/sort-able-pageblock-table-component-for_23.html?showComment=1404986955775#c1309963738379587440 (http://blogforce9.blogspot.in/2012/08/sort-able-pageblock-table-component-for_23.html?showComment=1404986955775#c1309963738379587440" target="_blank)
2. http://blogforce9dev-developer-edition.ap1.force.com/ProjectDetail?id=a0290000007rdwd (http://blogforce9dev-developer-edition.ap1.force.com/ProjectDetail?id=a0290000007rdwd" target="_blank)
So I just wanted to know if some other option is there like this for client side sorting..?Or if you already faced this kind of situation..
The code which am using for collecting values for my pageblock table,
public ApexPages.StandardSetController con {
    get {
      if(con == null) {
        
         soql = 'Select Id,Name,Description__c,Validated__c FROM Cause__c';
        

        // Passing the String array to a list with Selected field sorting.
        CauseList1 = Database.query(soql + ' order by ' + sortField + ' ' + 'limit 100'); 

        // setting values of List in StandardSetController.
        con = new ApexPages.StandardSetController(CauseList1);
        
        // sets the number of records in each page set
        con.setPageSize(15);
        
        CauseOccurances = new Map<string,string>();
        RootCauseOccurances = new Map<string,string>();
        
        
            for(AggregateResult ar : [select Cause__c, count(Id) from CaseCauseMapping__c group by Cause__c])
            {
              CauseOccurances.put(string.valueof(ar.get('Cause__c')),string.valueof(ar.get('expr0')));
            }
            for(AggregateResult ar : [select Cause__c, count(Id) from CaseCauseMapping__c WHERE RootCause__c=true group by Cause__c])
            {
              RootCauseOccurances.put(string.valueof(ar.get('Cause__c')),string.valueof(ar.get('expr0')));
            }
      }
      
      return con;
    }
    set;
  }
Here Validated__c contains boolean value.CauseOccurances,RootCauseOccurances will contains the aggregated values and it will be assigned to the page block table fields.
Below is my sorting code(even I referred this from web,but forgot the site url),
public String sortDir {
        // To set a Direction either in ascending order or descending order.
        get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;}
        set;
    }

    // the current field to sort by. defaults to last name
    public String sortField {
        // To set a Field for sorting.
            get  { if (sortField == null) {sortField = 'Name'; } return sortField;  }
        set;
    } 
    
    
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';

         soqlsort = 'Select Id,Name,Description__c,Validated__c FROM Cause__c';

          // Passing the String array to a list with Selected field sorting.
          CauseList2 = Database.query(soqlsort + ' order by ' + sortField + ' ' + sortDir+' ' +'limit 100'); 

            // setting values of List in StandardSetController.
            con = new ApexPages.StandardSetController(CauseList2);

            // Set Page Size to 5
          con.setPageSize(15);
  }

with this code am able to sort Id,Name & validated fields.But not able to write code for sorting CauseOccurances,RootCauseOccurances..
I hope now I have clearly explained about my problem..
Waiting for your advice..

Thanks in advance
Chitra

RishavRishav
Ah, it is too long.

   well, again i will choose simple way no any jQuery table and anything. 
  Why don't you try the ORDER BY clause with multiple fields .I think sometimes it work well according to my SQL server experience.