0

I have used jquery plugin tablesorter to sort objects by column.Sorting is working fine.I have a set of about 12 records in a list which works inside a table with pagination, 10 records per page and sorting enabled on all the columns.When the page loads , the sorting works only for those 10 records .It doesn't consider the other 2 records on the next page.When I click on the first column , the record on next page should come up first according to sorting as its name starts with alphabet 'A'.The whole point of my implementation was if there are many records , sorting should bring them up on the first page. what am i missing here.

Below is my VF page and apex class

VF

<apex:page controller="PSS_PendingMilestonesControllerNew"  >  

    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"/>
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"/>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-lightness/jquery-ui.css" type="text/css" media="all" />
<apex:includeScript value="{!URLFOR($Resource.tablesorter, 'jquery.tablesorter.min.js')}"/>
<apex:stylesheet value="{!URLFOR($Resource.tablesorter, 'themes/blue/style.css')}"/>

<script type="text/javascript">
    $j = jQuery.noConflict();    
    $j(document).ready(function () {
    $j("[id$=pendingMilestones]").tablesorter();

    });

  //some other unrelated js
    function reInitializeTableSorter() {
        $j("[id$=details]").tablesorter();
    }
</script>

    <apex:form id="theform" >  
     <div style="overflow:auto;height:500px">
     <!-- ******* Section Of Pending milestones whose actual recieved date is not null  ********   -->  
        <apex:pageBlock id="pb1" title="Pending Milestones"> 
            <apex:pageBlockButtons location="top" >
            <apex:outputPanel id="myButtons" >
                    <apex:commandButton value="First" status="aStatus" action="{!firstPage}" title="First" disabled="{!IF(currentpage > 0,false,true)}" rendered="{!IF(DisplayList.size > 0,true,false)}" />
                    <apex:commandButton value="Previous" status="aStatus" action="{!previous}" title="Previous" disabled="{!IF(currentpage > 0,false,true)}" rendered="{!IF(DisplayList.size > 0,true,false)}"  />
                    <apex:commandButton value="Next" status="aStatus" action="{!next}" title="Next" disabled="{!IF(currentpage < (totalpages-1),false,true)}" rendered="{!IF(DisplayList.size > 0,true,false)}" />
                    <apex:commandButton value="Last" status="aStatus" action="{!lastPage}" title="Last" disabled="{!IF(currentpage < (totalpages-1),false,true)}" rendered="{!IF(DisplayList.size > 0,true,false)}"  onComplete="reInitializeTableSorter()"/>
            </apex:outputPanel>
          </apex:pageBlockButtons>

          <apex:outputPanel id="myTable" >
            <apex:pageBlockSection columns="1">      
                <apex:pageBlockTable id="pendingMilestones" value="{!displayList}" var="wrap" rendered="{!DisplayList.size>0}"  styleClass="tablesorter" headerClass="header">
                    <apex:column headerValue="Category">
                        <apex:outputtext styleClass="header" value="{!wrap.objName}"/>                                 
                    </apex:column>
                    <apex:column headerValue="Name">
                        <apex:outputLink value="/{!wrap.sobj['Id']}" target="_blank" >{!wrap.sobj['Name']}</apex:outputLink>                                 
                    </apex:column>
                    <apex:column headerValue="Actual Recieved Date">
                        <apex:outputtext styleClass="header" value="{!wrap.field2}"/>                   
                    </apex:column>
                    <apex:column headerValue="Actual Start Date">
                        <apex:outputtext styleClass="header" value="{!wrap.field3}"/>                   
                    </apex:column>
                    <apex:column headerValue="Target Complete Date">
                        <apex:outputtext styleClass="header" value="{!wrap.field4}"/>                   
                    </apex:column>
                    <apex:column headerValue="Priority">
                        <apex:outputtext styleClass="header" value="{!wrap.field5}"/>                   
                    </apex:column> 
                </apex:pageBlockTable>
                <apex:outputText style="font-style:italic" value="No records to display." rendered="{!DisplayList.size=0}"/>
            </apex:pageBlockSection> 
           </apex:outputPanel> 

        </apex:pageBlock>

    <!-- ******* Section Of Incoming milestones whose actual recieved date is null  ********   -->    

        <apex:pageBlock id="pb2" title="Incoming Tasks"> 
        <apex:pageBlockButtons location="top" >
            <apex:outputPanel id="myButtons" >
                    <apex:commandButton value="First" status="aStatus" action="{!firstPage1}" title="First" disabled="{!IF(currentpage1 > 0,false,true)}" rendered="{!IF(DisplayList1.size > 0,true,false)}" />
                    <apex:commandButton value="Previous" status="aStatus" action="{!previous1}" title="Previous" disabled="{!IF(currentpage1 > 0,false,true)}" rendered="{!IF(DisplayList1.size > 0,true,false)}"  />
                    <apex:commandButton value="Next" status="aStatus" action="{!next1}" title="Next" disabled="{!IF(currentpage1 < (totalpages1-1),false,true)}" rendered="{!IF(DisplayList1.size > 0,true,false)}" />
                    <apex:commandButton value="Last" status="aStatus" action="{!lastPage1}" title="Last" disabled="{!IF(currentpage1 < (totalpages1-1),false,true)}" rendered="{!IF(DisplayList1.size > 0,true,false)}"  />
            </apex:outputPanel>
          </apex:pageBlockButtons>

          <apex:outputPanel id="myTable1" >
            <apex:pageBlockSection columns="1">      
                <apex:pageBlockTable id="IncomingMilestones" value="{!DisplayList1}" var="wrap" rendered="{!DisplayList1.size>0}">
                    <apex:column headerValue="Category">
                        <apex:outputtext value="{!wrap.objName}"/>                                 
                    </apex:column>
                    <apex:column headerValue="Name">
                        <apex:outputLink value="/{!wrap.sobj['Id']}" target="_blank" >{!wrap.sobj['Name']}</apex:outputLink>                                 
                    </apex:column>
                    <apex:column headerValue="Priority">
                        <apex:outputtext value="{!wrap.field5}"/>                   
                    </apex:column>  
                </apex:pageBlockTable>
                <apex:outputText style="font-style:italic" value="No records to display." rendered="{!DisplayList1.size=0}"/>
            </apex:pageBlockSection>  
          </apex:outputPanel>  
        </apex:pageBlock>
        </div>
    </apex:form>
</apex:page>

Apex

public class PSS_PendingMilestonesControllerNew{

    public List<PSS_Milestone__c> pendingMilestoneList; //List retrieved on quering PSS Milestones
    public List<PSS_Milestone__c> sListPending = new List<PSS_Milestone__c>(); //Final List of PSS Milestone after filtering
    public integer currentpage {get;set;}
    public integer totalRecords {get;set;}
    public integer pageSize {get;set;}

    public integer currentpage1 {get;set;}
    public integer totalRecords1 {get;set;}
    public integer pageSize1 {get;set;}

    public List<wrapperPendingMilestone> wrapperListShow {get;set;}
    public List<wrapperPendingMilestone> wrapperListShowIncoming {get;set;}

    public List<wrapperPendingMilestone> displayList {get{
          system.debug('wrapperPendingMilestone>>>>'+wrapperListShow);
          List<wrapperPendingMilestone> displayList = new List<wrapperPendingMilestone>();
          totalRecords = wrapperListShow.size();

                if(wrapperListShow.size() > 0 && totalRecords > 0){
                    for(Integer i = currentpage*pageSize ; i < totalRecords && i < (currentpage*pageSize) + pageSize; i++){
                        displayList.add(wrapperListShow[i]);
                    }
                }
            return displayList;

        }set;}  //  For pending task section
    public List<wrapperPendingMilestone> displayList1 {get{
                system.debug('wrapperPendingMilestoneIncomming>>>>'+wrapperListShowIncoming);
                List<wrapperPendingMilestone> displayList1 = new List<wrapperPendingMilestone>();
                totalRecords1 = wrapperListShowIncoming.size();

                if(wrapperListShowIncoming.size() > 0 && totalRecords1 > 0){
                    for(Integer i = currentpage1*pageSize1 ; i < totalRecords1 && i < (currentpage1*pageSize1) + pageSize1; i++){
                        displayList1.add(wrapperListShowIncoming[i]);
                    }
                }
            return displayList1;

        }set;} //For Incoming task section

    /*-----------Parent Constructor---------*/

    public PSS_PendingMilestonesControllerNew(){

       currentpage=0;
       totalRecords = 0;
       pageSize = 10;

        currentpage1=0;
       totalRecords1 = 0;
       pageSize1 = 10;

        Id userId = UserInfo.getUserId();
        wrapperListShow = new List<wrapperPendingMilestone>();
        wrapperListShowIncoming = new List<wrapperPendingMilestone>();


        /*------Get list of all the child records of PSS Opportunity that are eligible based on the conditions-------*/

        pendingMilestoneList = new List<PSS_Milestone__c>([Select id,name,Assigned_To__c ,Actual_Completed_Date__c,Actual_Received_Date__c,
                                        Actual_Start_Date__c,Target_Completed_Date__c,PSS_Queue__c,RecordType.Name,Priority__c,
                                        PSS_Milestone__c.PSS_Queue__r.Queue_Owner__c,PSS_Milestone__c.PSS_Queue__r.Backup_User1__c,
                                        PSS_Milestone__c.PSS_Queue__r.Backup_User2__c  
                                        from PSS_Milestone__c 
                                        where (Assigned_To__c =: userId or 
                                               (PSS_Milestone__c.PSS_Queue__r.Queue_Owner__c =: userId or
                                               PSS_Milestone__c.PSS_Queue__r.Backup_User1__c =: userId or 
                                               PSS_Milestone__c.PSS_Queue__r.Backup_User2__c =: userId)) and
                                              Actual_Completed_Date__c = null
                                        order by Target_Completed_Date__c DESC NULLS LAST]);


        /*--------Iterate over the individual child records list and add it to SObject list----------*/ 

            if(pendingMilestoneList.size() > 0){
                for(PSS_Milestone__c milestone : pendingMilestoneList ){

                    if((milestone.Assigned_To__c == milestone.PSS_Queue__r.Queue_Owner__c && 
                        (milestone.PSS_Queue__r.Queue_Owner__c == userId ||
                         milestone.PSS_Queue__r.Backup_User1__c == userId ||
                         milestone.PSS_Queue__r.Backup_User2__c == userId)) || 
                         (milestone.Assigned_To__c == userId)){

                               sListPending.add(milestone);                            
                        }             
                    }
                system.debug('**sListPending**'+sListPending);
            }


        /*----------Pass the List and get required details and put it in Wrapper List--------------*/    
        getDetialList(sListPending);
        system.debug('--displayList--'+displayList);
        system.debug('--displayList1--'+displayList1);

    }


    /*---------Get the details of the records retrieved in PSS Milestone list and add them to Wrapperlist----------*/

    public void getDetialList(List<PSS_Milestone__c> pendingMilestoneList){
        system.debug('**pendingMilestoneListPending inside function**'+pendingMilestoneList);

        /*-----Iterate over the Sobject list and add it to Wrapper list along with the required fields if present on the object------*/        
        for(PSS_Milestone__c s : pendingMilestoneList){
            Date d;
            String objName;
            objName = s.RecordType.Name;
            system.debug('---objName---'+objName);             

            /*-----Check for Actual_Received_Date__c field in SObject-----*/
            String d2;
                if(s.get('Actual_Received_Date__c') != null){
                   d = Date.valueOf(s.get('Actual_Received_Date__c'));
                   d.format();               
                   system.debug('Date value...'+d);           
                   d2 = String.valueOf(d);
                   system.debug('String value...'+d2);               
                }


            /*-----Check for Actual_Start_Date__c field in SObject-----*/           
            String d3;

                if(s.get('Actual_Start_Date__c') != null){
                   d = Date.valueOf(s.get('Actual_Start_Date__c'));
                   system.debug('Date value...'+d);         
                   d3 = String.valueOf(d);
                   system.debug('String value...'+d3);               
                }


            /*-----Check for Target_Completed_Date__c field in SObject-----*/            
            String d4;

                if(s.get('Target_Completed_Date__c') != null){
                    d = Date.valueOf(s.get('Target_Completed_Date__c'));
                    system.debug('Date value...'+d);           
                    d4 = String.valueOf(d);
                    system.debug('String value...'+d4);                
                }

            /*-----Check for Target_Completed_Date__c field in SObject-----*/            
            String d5;

                if(s.get('Priority__c') != null){         
                    d5 = String.valueOf(s.get('Priority__c'));
                    system.debug('String value...'+d5);                
                }


            /*-------Add the records to WrapperList along with the retrieved parameters--------*/
            // if actual recieved date is not null add in list
             if(s.get('Actual_Received_Date__c') != null) 
             {
                wrapperListShow.add(new wrapperPendingMilestone(s,objName,d2,d3,d4,d5));  
             }  

             else
                wrapperListShowIncoming.add(new wrapperPendingMilestone(s,objName,d3,d4,d5));         
        }

        system.debug('****wrapperListShow***'+wrapperListShow);
    }

    //---------------methods for PAGINATION for WrapperListShow --------------

    public Integer getTotalPages() {
      if (math.mod(totalRecords, Pagesize ) > 0) {
         return totalRecords/Pagesize + 1;
      } else {
         return (totalRecords/Pagesize);
      }
   }

    public Integer getTotalPages1() {
      if (math.mod(totalRecords1, Pagesize1 ) > 0) {
         return totalRecords1/Pagesize1 + 1;
      } else {
         return (totalRecords1/Pagesize1);
      }
    }

    public void firstPage() { //user clicked First button
      currentpage=0;
    }

    public void previous() { //user clicked previous button
      currentpage--;
    }

    public void next() { //user clicked next button
      currentpage++;
    }

    public void lastPage() { //user clicked last button
      if(math.mod(totalRecords,pageSize) == 0){
          currentpage = (totalRecords / pageSize) - 1;
      }
      else{
          currentpage = totalRecords / pageSize;
      }
    }

    //---------------methods for PAGINATION for WrapperListShow --------------
    public void firstPage1() { //user clicked First button
      currentpage1=0;
    }

    public void previous1() { //user clicked previous button
      currentpage1--;
    }

    public void next1() { //user clicked next button
      currentpage1++;
    }

    public void lastPage1() { //user clicked last button
      if(math.mod(totalRecords1,pageSize1) == 0){
          currentpage1 = (totalRecords1 / pageSize1) - 1;
      }
      else{
          currentpage1 = totalRecords1 / pageSize1;
      }


    }
    //-----------------------------------------------

    /*--------Wrapper Class--------*/
    public class wrapperPendingMilestone{
        public SObject sobj {get;set;}
        public String objName {get;set;}
        public String field2 {get;set;}
        public String field3 {get;set;}
        public String field4 {get;set;}
        public String field5 {get;set;}    


        /*---------Wraper Constructor--for section where-Actual Recieved date not null------*/
        public wrapperPendingMilestone(SObject sobj, String objName, String field2, String field3,String field4,String field5){
            this.sobj = sobj;
            this.objName = objName;           
            this.field2 = field2;
            this.field3 = field3;
            this.field4 = field4;
            this.field5 = field5;

        }
        /*---------Wraper Constructor--for Incoming section with Actual Recieved date is null------*/

         public wrapperPendingMilestone(SObject sobj, String objName, String field3,String field4,String field5){
            this.sobj = sobj;
            this.objName = objName;           
            this.field3 = field3;
            this.field4 = field4;
            this.field5 = field5;
        }             
    }
}

2 Answers 2

3

The issue is the way you implemented it.

In your displayList, you are always carrying 10 items depending upon which page you are. So at any point of time, your pageBlockTable has 10 records in it. Now the tablesorter works on client side i.e your browser. So from tablesorter's point of view, it only has 10 records in the table whose ID you have provided to tablesorter. Hence its working only on the current 10 records.

As a solution, you can either handle sorting & pagination both using apex class or both using javascript(i.e 3rd party JS library, footable is a good way to start)

3
  • Isn't there any way to let the table sorter know that there are 12 records?
    – Samir
    Dec 30, 2016 at 10:48
  • That's something you need to check on tablesorter's documentation. But as far as I know normally you don't have that option unless you have the whole data in your table source. Dec 30, 2016 at 10:50
  • Thanks for the info.i will look for the solution and wait for some more feedback before i close this question
    – Samir
    Dec 30, 2016 at 10:53
0

Either you can sort in apex code to by using following code:

public class PSS_PendingMilestonesControllerNew implements Comparable {
   List<wrapperPendingMilestone> wrapperListShow = new 

   List<wrapperPendingMilestone>();
   wrapperListShow.add(new wrapperPendingMilestone(parameters));
  .
  .
  .
  wrapperListShow.sort();
  public Integer compareTo(Object obj) {
       SelectOpt obj1 = (SelectOpt) obj;
       if (this.objName == obj1.objName) return 0;
       if (this.objName > obj1.objName) return 1;
       return -1;        
   }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .