0

i am trying to do pagination for the following page but i dont know where i went wrong the pagination is not working. And i want to show 5 records per page,.so how to do can anyone help!!!!

<apex:pageBlock >
    <center><table width ="90%" cellspacing="0" cellpading="0" border ="2">

    <div><h2><center>Job Openings</center></h2></div><br/>
    <tr><td width="10%"><b><center>Job Id</center></b></td><td width="10%"><b><center>Job Title</center></b></td><td width="5%"><b><center>experience (yrs)</center></b></td>
    <td width="10%"><b><center>Company</center></b></td><td width="20%"><b><center>skills</center></b></td> <td width="5%"><b><center>vacancies</center></b></td><td width="5%"><b><center>location</center></b></td></tr>
    <apex:repeat value="{!jbList }" var="jb">

    <tr><td><center>{!jb.Name}</center></td><td><center>{!jb.SCSCHAMPS__Job_Title__c}</center></td><td><center>{!jb.SCSCHAMPS__Experience_Required__c}</center></td><td><center>{!jb.SCSCHAMPS__Account__r.Name}</center></td>
    <td><center>{!jb.SCSCHAMPS__Desired_Skills__c}</center></td><td><center>{!jb.SCSCHAMPS__Postions__c}</center></td><td><center>{!jb.Work_Location__c}</center></td> </tr>
    </apex:repeat>
    </table></center><br/><br/>
    <apex:commandButton action="{!Beginning}"  value="<<" disabled="{!disablePrevious}" reRender="myPanel,myButtons"/>
            <apex:commandButton action="{!Previous}" title="Previous" value="<"/>        
            <apex:commandButton action="{!Next}" title="Next" value=">" />
            <apex:commandButton action="{!End}" title="End" value=">>" disabled="{!disableNext}" reRender="myPanel,myButtons"/>


    </apex:pageBlock>

Controller

public with sharing class Jobs1 {

    public String var{set; get;}
    public List < SCSCHAMPS__Job__c> jbList {get;set;}

   private integer counter=0; 
   private integer list_size=5;
   public integer total_size; 
   public Jobs1(){
   total_size=[select count() from SCSCHAMPS__Job__c where SCSCHAMPS__Stage__c='Open' ];
   }
   public SCSCHAMPS__Job__c[] getNumbers() {
      try {
         SCSCHAMPS__Job__c[] numbers = [select id,Name 
                                from SCSCHAMPS__Job__c 
                                order by Name
                                limit :list_size 
                                ];

         return numbers;
      } catch (QueryException e) {
         ApexPages.addMessages(e);   
         return null;
      }
   }
   public PageReference Beginning() { //user clicked beginning
      counter = 0;
      return null;
   }

   public PageReference Previous() { //user clicked previous button
      counter -= list_size;
      return null;
   }

   public PageReference Next() { //user clicked next button
      counter += list_size;
      return null;
   }

   public PageReference End() { //user clicked end
      counter = total_size - math.mod(total_size, list_size);
      return null;
   }

   public Boolean getDisablePrevious() { 
      //this will disable the previous and beginning buttons
      if (counter>0) return false; else return true;
   }

   public Boolean getDisableNext() { //this will disable the next and end buttons
      if (counter + list_size < total_size) return false; else return true;
   }

   public Integer getTotal_size() {
      return total_size;
   }

   public Integer getPageNumber() {
      return counter/list_size + 1;
   }

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



    public Jobs1(ApexPages.StandardController controller) {
    jbList = new List < SCSCHAMPS__Job__c >();
    List < SCSCHAMPS__Job__c > jb= new list < SCSCHAMPS__Job__c >();
    //this.var= apexPages.currentpage().getParameters().get('id');
   jbList= [select id,Name,SCSCHAMPS__Experience_Required__c,SCSCHAMPS__Account__r.Name,SCSCHAMPS__Desired_Skills__c,SCSCHAMPS__Account__c,Work_Location__c  ,SCSCHAMPS__Postions__c ,SCSCHAMPS__Job_Title__c
                                                         from SCSCHAMPS__Job__c where SCSCHAMPS__Stage__c='Open' ];
    jblist.sort();
    //totalSize = jbList.size();
    System.debug('+++Test'+jbList+var); 
    //Controller.setPageSize(2);                                                    
    }





}

1 Answer 1

0

You should leverage StandardSetController. It will take care all pagination related functionalities which you are trying to achieve in a very simplified way.

Based on your dynamic query, the results will be assigned to StandardSetController instance.

public ApexPages.StandardSetController setCon{get;set;}
String querystr = 'select id,Name,SCSCHAMPS__Experience_Required__c,SCSCHAMPS__Account__r.Name,SCSCHAMPS__Desired_Skills__c,SCSCHAMPS__Account__c,Work_Location__c  ,SCSCHAMPS__Postions__c ,SCSCHAMPS__Job_Title__c
                   from SCSCHAMPS__Job__c';

setCon = new ApexPages.StandardSetController(Database.getQueryLocator(querystr));

You can also, limit the records on the page, based on the dropdown, default is shown 10 records.

Refer my answer at this question: Pagination is not working for my VF page

or otherwise refer my blog Custom Controller Pagination - easiest way

2
  • Please quote relevant portions along with links
    – Eric
    Apr 18, 2017 at 5:59
  • @Eric, actually its better rewrite the code from beginning, thats why I have suggested that Apr 18, 2017 at 15:27

You must log in to answer this question.

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