+ Start a Discussion
souvik9086souvik9086 

Pagination from totallist

I want to do pagination of records.Suppose I want to show 5 records in one page,next 5 in next page.But the requirement is not to query database each time next button is clicked.The requirement is at first total list will be retrieved from controller then from that list, limited values will be displayed in a page,after clicking next , the next values from the list will be displayed. How this can be done. If you have any idea kindly help me as soon as possible.

 

Thanks

Souvik.

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

-------------------- vf page---------------

 

<apex:page controller="showdatawithnext" >

 <apex:form >

     <apex:outputpanel id="pp">

         <table border="2">

         <tr>

             <th>Name</th>

             <th>Email</th>

             <th>Mobile Phone</th>

             <th>Date of Birth</th>

         </tr>

         <apex:repeat value="{!ct1}" var="k">

         <tr>

             <td>{!k.name}</td>

             <td>{!k.email}</td>

             <td>{!k.MobilePhone}</td>

             <td>{!k.Birthdate}</td>

             </tr>

        

        

         </apex:repeat>

     </table>

    

   

            </apex:outputpanel>

     <table>

     <tr>

     <apex:repeat value="{!noofpage}" var="nn">

    <td>        <apex:commandLink action="{!display}"  reRender="pp" ><apex:param value="{!nn}" name="sss"  assignTo="{!pageno}"/> {!nn}</apex:commandLink></td>

     </apex:repeat>

     </tr>

     </table>

 <apex:commandButton value="next" action="{!display1}" reRender="pp"></apex:commandButton>

 

 

 

 </apex:form>

 

 

</apex:page>

 

-------------- Apex controller ----------------

public class showdatawithnext

{

public integer count{get;set;}

public integer count1{get;set;}

public integer size1{get;set;}

public string picklistvalue{get;set;}

public integer indexfrom{get;set;}

public integer indexto{get;set;}

public integer remaining{get;set;}

public integer temp{get;set;}

public integer temp1{get;set;}

public list<contact> ct{set;get;}

public list<integer> noofpage{get;set;}

 

public integer pageno{get;set;}

public list<contact> ct1{set;get;}

 

 

public showdatawithnext()

{

  

   

   // picklistvalue = 'a';

    ct=new list<contact>();

    calldisplay();

 

}

public void calldisplay()

{

     noofpage=new list<integer>();

    ct=[SELECT name,email,MobilePhone,Birthdate from contact];

    //string query1='SELECT name,email,MobilePhone,Birthdate from contact where name like \''+picklistvalue+'%\'  limit 1000 ';

      string query1='SELECT name,email,MobilePhone,Birthdate from contact limit 1000 ';

    system.debug('!!!!!!!!!!'+query1);

    ct= database.query(query1);

    system.debug('@@@@@@@@@@'+ct);

    count=ct.size();

    count1=ct.size();

    indexfrom=0;

    indexto=0;

    temp=0;

    size1=5;

    temp1=count/size1;

    temp=(math.mod(count,size1));

    count=0;

    if(temp>0)

    {

        for(integer i=1;i<=temp1+1;i++)

        {

            noofpage.add(i);

        }

    }

    else

    {

    for(integer i=1;i<=temp1;i++)

        {

            noofpage.add(i);

        }

    } 

    pageno=1;

   

    display();

}

public void display()

{

       if(pageno<=temp1+1)

       {

               ct1=new list<contact>();

               indexfrom=size1*(pageno-1);

               count=count+1;

               indexto=size1*pageno;

               if(indexto<=count1)

               {

                    for(integer i=indexfrom;i<indexto;i++)

                    {

                       if(ct[i]!=null)

                       {

                           ct1.add(ct[i]);

                       }

                     }

                }

                else

                {

                      indexto=((size1*(pageno-1))+temp);

                      for(integer i=indexfrom;i<indexto;i++)

                      {

                           if(ct[i]!=null)

                           {

                               ct1.add(ct[i]);

                           }

                      }

     

                }

 

   

    }

   

}

public void display1()

{

    if(pageno<=temp1)

    {

    pageno=pageno+1;

    display();

    }

    else

    {

        pageno=1;

    }

}

 

 

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

souvik9086souvik9086

Thanks very much for your reply.