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
CoderCoder 

Visualforce Pagination not working properly.

Hi,

 

I'm creating a visualforce page for searching the Contact Names from contact records. I can able to search the records and display it in visualforce page. But when i added the pagination functionality its not working in these page. Am i missing anything or my approach is wrong. Please help me to solve this problem.

 

 

Visualforce Page code...

 

 

<apex:page standardController="Contact" extensions="ContactSearch" showHeader="false" sidebar="false"> <apex:form > <apex:sectionHeader title="Search Contacts"/> <apex:pageBlock > <apex:pageBlockSection title="Contacts"> </apex:pageBlockSection> <!--Panel grid to get the input from users --> <apex:panelGrid columns="2"> <apex:outputLabel style="font-weight:bold;" value="Contact Name"></apex:outputLabel> <apex:inputtext value="{!inp}"/> <apex:outputLabel style="font-weight:bold;" value="Contact Email"></apex:outputLabel> <apex:inputText value="{!emailinp}"/> <apex:OutputLabel value="Select Lead Source" style="font-weight:bold;"/> <apex:selectList value="{!lsource}" size="1"> <apex:selectOptions value="{!Source}"></apex:selectOptions> </apex:selectList> </apex:panelGrid> <apex:commandButton value="Search" action="{!search}"/> <apex:pageBlockSection title="Search results" columns="1"> <apex:outputPanel id="contacts"></apex:outputPanel> <apex:pageBlockTable value="{!results}" var="contactsList"> <!--<apex:column> <apex:facet name="header"></apex:facet> </apex:Column>--> <apex:column headerValue="Contact First Name"> <apex:outputText value="{!contactsList.cont.Name}"/> </apex:column> <apex:column headerValue="Contact Email"> <apex:outputText value="{!contactsList.cont.Email}"/> </apex:column> <apex:column headerValue="Level"> <apex:outputText value="{!contactsList.cont.Level__c}"/> </apex:column> <apex:column headerValue="Lead Source"> <apex:outputText value="{!contactsList.cont.LeadSource}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlockSection> </apex:pageBlock> <apex:panelGrid columns="4"> <apex:commandLink action="{!first}">First</apex:commandlink> <apex:commandLink action="{!previous}" rendered="{!hasPrevious}">Previous</apex:commandlink> <apex:commandLink action="{!next}" rendered="{!hasNext}">Next</apex:commandlink> <apex:commandLink action="{!last}">Last</apex:commandlink> </apex:panelGrid> </apex:form> </apex:page>

 

 Apex Class code.....

 

 In these apex class i'm not able to modify the Controllers Database.getQuerylocator. Now, when i click the search button in visuaforce page it will displays the record, but i want to give the pagination for that records.

public class ContactSearch { //List<categoryWrapper> categories {get; set;} public ContactSearch(ApexPages.StandardController controller) { cid = System.currentPageReference().getParameters().get('id'); } Public ApexPages.StandardSetController con { get { if(con == null) { con = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, Name FROM Contact ORDER BY Name LIMIT 50])); con.setPageSize(5); } return con; } set; } /*Public List<categoryWrapper> getCategories() { categories = new List<categoryWrapper>(); return categories; } */ Public String cid; String inp; String emailinp; String leadsource; String lsource; String Source; public List<Contact> results = new List<Contact>(); public List<sContact> contacts { get; set; } Public Boolean hasNext { get { return con.gethasNext(); } set; } Public Boolean hasPrevious { get { return con.gethasPrevious(); } set; } Public integer pageNumber { get { return con.getpageNumber(); } } public void first() { con.first(); } public void last() { con.last(); } public void previous() { con.previous(); } public void next() { con.next(); } /* Getting the input from the user in the contact name text box and the values are assigned here */ public String getinp() { return inp; } public void setinp(String i) { this.inp = i; } /* -------------------------------------------------- */ /* Getting and Setting the input from the user in the contact email and the values are assigned here */ public String getemailinp() { return emailinp; } public void setemailinp(String email) { this.emailinp = email; } /* ------------------------------------------------------------------------ */ /* Getting and setting the values from the user in the lead source */ public string getleadsource() { return leadsource; } public void setleadsource(String ls) { this.leadsource = ls; } /* ------------------------------------------------------------------------ */ /* Getter and Setter value for Select List */ public string getlsource() { return lsource; } public void setlsource(String lsource) { this.lsource = lsource; } /* ------------------------------------------------------------------------------ */ /* */ public List<selectOption> getSource() { List<selectOption> val = new List<selectOption>(); val.add(new selectOption('','--None--')); val.add(new selectOption('Partner','Partner')); val.add(new selectOption('Web','Web')); val.add(new selectOption('Phone Inquiry','Phone Inquiry')); val.add(new selectOption('Partner Referral','Partner Referral')); val.add(new selectOption('Purchased List','Purchased List')); return val; } public void setSource(String source) { this.Source = source; } public List<contact> search() { contacts = new List<sContact>(); if(lsource!=null) { for(Contact c : [SELECT Name, FirstName, LastName, Email, Level__c, LeadSource, Id FROM Contact WHERE Name LIKE :inp+'%' AND Email LIKE :emailinp+'%' AND LeadSource LIKE:lsource+'%' ORDER BY Level__c DESC ]) { contacts.add(new sContact(c)); } } else { for(Contact c:[SELECT Name, FirstName, LastName, Email, Level__c, LeadSource, Id FROM Contact WHERE Name LIKE :inp+'%' AND Email LIKE :emailinp+'%' ORDER BY Level__c DESC ]) { contacts.add(new sContact(c)); } } return null; } public List<sContact> getresults() { return contacts; } public class sContact { public contact cont { get; set; } public Boolean selected { get; set; } public sContact (Contact c) { cont = c; selected = false; } } }

 

Please help me to solve this.

 

 

Thank You.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
CoderCoder

Thank you very much for ur reply..

 

I got the solution.

 

I have tried the following code.

 

 

public class ContactSearch { Private final ApexPages.StandardController controller; public ContactSearch(ApexPages.StandardController controller){ this.controller = controller; } List<categoryWrapper> categories {get; set;} /* public ContactSearch(ApexPages.StandardController controller) { cid = System.currentPageReference().getParameters().get('id'); }*/ Public List<categoryWrapper> getCategories() { categories = new List<categoryWrapper>(); return categories; } List<Contact> getlbtc; List<Contact> g = new List<Contact>(); Integer i = 10; Integer j = 0, k = 0, flag = 0; Public String cid; String inp; String emailinp; String leadsource; String lsource; String Source; Integer next = 5, count = 5; Boolean showNext, showPrev; public List<Contact> results = new List<Contact>(); public List<Contact> results_next = new List<Contact>(); public List<sContact> contacts { get; set; } /* public ApexPages.StandardSetController con { get { if(con == null) { //ApexPages.StandardSetController ss = new ApexPages.StandardSetController(contacts); con = new ApexPages.StandardSetController(results); //con = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, Name FROM Contact ORDER BY Name LIMIT 50])); con.setPageSize(5); } return con; } set; } public Boolean hasNext { get { return con.gethasNext(); } set; } public Boolean hasPrevious { get { return con.gethasPrevious(); } set; } public integer pageNumber { get { return con.getpageNumber(); } } public void first() { con.first(); } public void last() { con.last(); } /* Getting the input from the user in the contact name text box and the values are assigned here */ public String getinp() { return inp; } public void setinp(String i) { this.inp = i; } /* -------------------------------------------------- */ /* Getting and Setting the input from the user in the contact email and the values are assigned here */ public String getemailinp() { return emailinp; } public void setemailinp(String email) { this.emailinp = email; } /* ------------------------------------------------------------------------ */ /* Getting and setting the values from the user in the lead source */ public string getleadsource() { return leadsource; } public void setleadsource(String ls) { this.leadsource = ls; } /* ------------------------------------------------------------------------ */ /* Getter and Setter value for Select List */ public string getlsource() { return lsource; } public void setlsource(String lsource) { this.lsource = lsource; } /* ------------------------------------------------------------------------------ */ /* */ public PageReference getbtc() { if(lsource!=null) { g.clear(); /*push the query output to instance1 of object*/ getlbtc=[SELECT Name, FirstName, LastName, Email, Level__c, LeadSource, Id FROM Contact WHERE FirstName LIKE :inp+'%' AND Email LIKE :emailinp+'%' AND LeadSource LIKE:lsource+'%' ORDER BY Level__c DESC]; /* check if instance has more den 10 records*/ if(getlbtc.size()>10) /* if instance has more den 10 records*/ for(i=0;i<10;i++) /*then fetch 1st 10 records and push it to 2nd instance*/ g.add(getlbtc[i]); else /* if < than 10 records den just push all d records ion 1st instance to 2nd instance*/ g = getlbtc; return null; } else { g.clear(); /*push the query output to instance1 of object*/ getlbtc=[SELECT Name, FirstName, LastName, Email, Level__c, LeadSource, Id FROM Contact WHERE FirstName LIKE :inp+'%' AND Email LIKE :emailinp+'%' ORDER BY Level__c DESC]; /* check if instance has more den 10 records*/ if(getlbtc.size()>10) /* if instance has more den 10 records*/ for(i=0;i<10;i++) /*then fetch 1st 10 records and push it to 2nd instance*/ g.add(getlbtc[i]); else /* if < than 10 records den just push all d records ion 1st instance to 2nd instance*/ g = getlbtc; return null; } } public PageReference next() { Integer temp = 0; /* if the instance having more dan 10 records*/ if(getlbtc.size()>10) { /* as we know i's current value is 10*/ if(i < getlbtc.size()) /*if 10<total no of records ie if instance having more than 10 records*/ { g.clear(); i = i+10;/*trying to fetch next 10 records by incrementing i=10 to 20 */ for(j=j+10;j<i;j++)/*so now i=20 so trying to fetchnext 10 records by iteratingb thru loop of j*/ /* j=10 now initially*/ if(j < getlbtc.size())/*10 is < suppose if totla dere r 20 records*/ { /* so now iam adding records from 11 th record to 20 */ g.add(getlbtc[j]); temp++;/* so now temp =10 as we iterate d loop 10 times*/ } else/* if i=10>size ie now of records */ break; j = j-10;/*now again set tthe j value to 0 and iterate d loop again if dere are more than 20 records */ } getGetlbtc();/*(getter for the instance*/ } else g = getlbtc; if((temp!=10)&&(flag==0)) { j = j-temp+10; flag = 1; system.debug('temp j:'+j); } return null; } public PageReference prev() { if(getlbtc.size()>10) { if(j>0) { g.clear(); for(k=j-1;K>j-11;K--) if(k>=0) g.add(getlbtc[k]); else break; i = j; j = j-10; } List<Contact> g1 = new List<Contact>(); Integer x=0,y=9; g1 = g.deepclone(); for(x=0;x<10;x++){ g.set(y,g1.get(x)); y--; } getGetlbtc(); } else g = getlbtc; return null; } public List<selectOption> getSource() { List<selectOption> val = new List<selectOption>(); val.add(new selectOption('','--None--')); val.add(new selectOption('Partner','Partner')); val.add(new selectOption('Web','Web')); val.add(new selectOption('Phone Inquiry','Phone Inquiry')); val.add(new selectOption('Partner Referral','Partner Referral')); val.add(new selectOption('Purchased List','Purchased List')); return val; } public void setSource(String source) { this.Source = source; } /* public List<contact> search() { try { contacts = new List<sContact>(); if(lsource!=null) { for(Contact c : [SELECT Name, FirstName, LastName, Email, Level__c, LeadSource, Id FROM Contact WHERE Name LIKE :inp+'%' AND Email LIKE :emailinp+'%' AND LeadSource LIKE:lsource+'%' ORDER BY Level__c DESC ]) { contacts.add(new sContact(c)); } } else { for(Contact c:[SELECT Name, FirstName, LastName, Email, Level__c, LeadSource, Id FROM Contact WHERE Name LIKE :inp+'%' AND Email LIKE :emailinp+'%' ORDER BY Level__c DESC ]) { contacts.add(new sContact(c)); } } } catch(Exception e) { System.debug('Exception :'+e); } return null; } public List<sContact> getresults() { return contacts; } */ public class sContact { public contact cont { get; set; } public Boolean selected { get; set; } public sContact (Contact c) { cont = c; selected = false; } } public List<Contact> getGetlbtc() { return g; } }

 

 

 

 

 

 

All Answers

NZ_XueNZ_Xue
it looks like u missing use attribute 'recordSetVar', there are some examples in the Visualforce Developer's Guide.
CoderCoder

Hi,

Thanks for your reply...

 

When i add the recordSetvar its showing the error message as Unknown Constructor.

 

Here i'm not able to pass the queried records to the con = new ApexPages.StandardSetController.

So i'm not able to do navigation for the searched records.

Do u hav any idea to pass the records to the controller.

Message Edited by Coder on 03-08-2010 01:47 AM
NZ_XueNZ_Xue

 i got your point now. your query based on the conditions that inputed by user, but the standardcontroller fired when page

loaded. so the  standardcontroller has the different records set from you need. i make a simple example: i created 6 new

contacts with the name start with "temp" .

 

when the page loaded  the controller list all the contact. pagesize=10

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

do search by the name  key word "temp"

 

 

 

 

 

i changed the pagesize =2 

 

 

 

 

 

 

it works. but i do not like it. it is a little bit tricky,not a good solution. i hope some one else can post a good one. i will try to 

make my code better today and get it back to your.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CoderCoder
Thank u very much for ur reply.....
NZ_XueNZ_Xue

you welcome. i feel this question is a common need on VF, so it should have a good solution somewhere.

 

My approach is rebulid the controller in doSearch() method after i get the search conditons from page.

 

In my example :variable searchText  contains the key word for my query

 

i rebuild con in doSearch method


con = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, Name, Email, MobilePhone

FROM Contact where Name like: searchText+'%' ORDER BY Name LIMIT 50]));
con.setPageSize(2);


 now the standard controller has the same set of records as your query. is it that your said pass query to controller?

 

 it is simple for get records set method:

 

public List<Contact> getResults() {

return (List<Contact>) con.getRecords();

}

 

I hope these help you。 thanks

 

 

 

 

 

 

 

 

 

 

CoderCoder

Thank you very much for ur reply..

 

I got the solution.

 

I have tried the following code.

 

 

public class ContactSearch { Private final ApexPages.StandardController controller; public ContactSearch(ApexPages.StandardController controller){ this.controller = controller; } List<categoryWrapper> categories {get; set;} /* public ContactSearch(ApexPages.StandardController controller) { cid = System.currentPageReference().getParameters().get('id'); }*/ Public List<categoryWrapper> getCategories() { categories = new List<categoryWrapper>(); return categories; } List<Contact> getlbtc; List<Contact> g = new List<Contact>(); Integer i = 10; Integer j = 0, k = 0, flag = 0; Public String cid; String inp; String emailinp; String leadsource; String lsource; String Source; Integer next = 5, count = 5; Boolean showNext, showPrev; public List<Contact> results = new List<Contact>(); public List<Contact> results_next = new List<Contact>(); public List<sContact> contacts { get; set; } /* public ApexPages.StandardSetController con { get { if(con == null) { //ApexPages.StandardSetController ss = new ApexPages.StandardSetController(contacts); con = new ApexPages.StandardSetController(results); //con = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, Name FROM Contact ORDER BY Name LIMIT 50])); con.setPageSize(5); } return con; } set; } public Boolean hasNext { get { return con.gethasNext(); } set; } public Boolean hasPrevious { get { return con.gethasPrevious(); } set; } public integer pageNumber { get { return con.getpageNumber(); } } public void first() { con.first(); } public void last() { con.last(); } /* Getting the input from the user in the contact name text box and the values are assigned here */ public String getinp() { return inp; } public void setinp(String i) { this.inp = i; } /* -------------------------------------------------- */ /* Getting and Setting the input from the user in the contact email and the values are assigned here */ public String getemailinp() { return emailinp; } public void setemailinp(String email) { this.emailinp = email; } /* ------------------------------------------------------------------------ */ /* Getting and setting the values from the user in the lead source */ public string getleadsource() { return leadsource; } public void setleadsource(String ls) { this.leadsource = ls; } /* ------------------------------------------------------------------------ */ /* Getter and Setter value for Select List */ public string getlsource() { return lsource; } public void setlsource(String lsource) { this.lsource = lsource; } /* ------------------------------------------------------------------------------ */ /* */ public PageReference getbtc() { if(lsource!=null) { g.clear(); /*push the query output to instance1 of object*/ getlbtc=[SELECT Name, FirstName, LastName, Email, Level__c, LeadSource, Id FROM Contact WHERE FirstName LIKE :inp+'%' AND Email LIKE :emailinp+'%' AND LeadSource LIKE:lsource+'%' ORDER BY Level__c DESC]; /* check if instance has more den 10 records*/ if(getlbtc.size()>10) /* if instance has more den 10 records*/ for(i=0;i<10;i++) /*then fetch 1st 10 records and push it to 2nd instance*/ g.add(getlbtc[i]); else /* if < than 10 records den just push all d records ion 1st instance to 2nd instance*/ g = getlbtc; return null; } else { g.clear(); /*push the query output to instance1 of object*/ getlbtc=[SELECT Name, FirstName, LastName, Email, Level__c, LeadSource, Id FROM Contact WHERE FirstName LIKE :inp+'%' AND Email LIKE :emailinp+'%' ORDER BY Level__c DESC]; /* check if instance has more den 10 records*/ if(getlbtc.size()>10) /* if instance has more den 10 records*/ for(i=0;i<10;i++) /*then fetch 1st 10 records and push it to 2nd instance*/ g.add(getlbtc[i]); else /* if < than 10 records den just push all d records ion 1st instance to 2nd instance*/ g = getlbtc; return null; } } public PageReference next() { Integer temp = 0; /* if the instance having more dan 10 records*/ if(getlbtc.size()>10) { /* as we know i's current value is 10*/ if(i < getlbtc.size()) /*if 10<total no of records ie if instance having more than 10 records*/ { g.clear(); i = i+10;/*trying to fetch next 10 records by incrementing i=10 to 20 */ for(j=j+10;j<i;j++)/*so now i=20 so trying to fetchnext 10 records by iteratingb thru loop of j*/ /* j=10 now initially*/ if(j < getlbtc.size())/*10 is < suppose if totla dere r 20 records*/ { /* so now iam adding records from 11 th record to 20 */ g.add(getlbtc[j]); temp++;/* so now temp =10 as we iterate d loop 10 times*/ } else/* if i=10>size ie now of records */ break; j = j-10;/*now again set tthe j value to 0 and iterate d loop again if dere are more than 20 records */ } getGetlbtc();/*(getter for the instance*/ } else g = getlbtc; if((temp!=10)&&(flag==0)) { j = j-temp+10; flag = 1; system.debug('temp j:'+j); } return null; } public PageReference prev() { if(getlbtc.size()>10) { if(j>0) { g.clear(); for(k=j-1;K>j-11;K--) if(k>=0) g.add(getlbtc[k]); else break; i = j; j = j-10; } List<Contact> g1 = new List<Contact>(); Integer x=0,y=9; g1 = g.deepclone(); for(x=0;x<10;x++){ g.set(y,g1.get(x)); y--; } getGetlbtc(); } else g = getlbtc; return null; } public List<selectOption> getSource() { List<selectOption> val = new List<selectOption>(); val.add(new selectOption('','--None--')); val.add(new selectOption('Partner','Partner')); val.add(new selectOption('Web','Web')); val.add(new selectOption('Phone Inquiry','Phone Inquiry')); val.add(new selectOption('Partner Referral','Partner Referral')); val.add(new selectOption('Purchased List','Purchased List')); return val; } public void setSource(String source) { this.Source = source; } /* public List<contact> search() { try { contacts = new List<sContact>(); if(lsource!=null) { for(Contact c : [SELECT Name, FirstName, LastName, Email, Level__c, LeadSource, Id FROM Contact WHERE Name LIKE :inp+'%' AND Email LIKE :emailinp+'%' AND LeadSource LIKE:lsource+'%' ORDER BY Level__c DESC ]) { contacts.add(new sContact(c)); } } else { for(Contact c:[SELECT Name, FirstName, LastName, Email, Level__c, LeadSource, Id FROM Contact WHERE Name LIKE :inp+'%' AND Email LIKE :emailinp+'%' ORDER BY Level__c DESC ]) { contacts.add(new sContact(c)); } } } catch(Exception e) { System.debug('Exception :'+e); } return null; } public List<sContact> getresults() { return contacts; } */ public class sContact { public contact cont { get; set; } public Boolean selected { get; set; } public sContact (Contact c) { cont = c; selected = false; } } public List<Contact> getGetlbtc() { return g; } }

 

 

 

 

 

 

This was selected as the best answer