You need to sign in to do that
Don't have an account?
phani_m
Dynamic search with visualforce page
Hai.............
I had one problem regarding Dynamic search....
I Had one custom object (Candidate) and input fields First Name ,LastName , City, Education etc.....
On my visual force page, without typing any value in the input text field the values are displaying automatically and when i enter any value in the input field ERROR msg is displaying even the record is present
MY visualforce code :
<apex:page controller="CandidateSearchController" sidebar="false"> <script type="text/javascript"> function doSearch() { searchServer( document.getElementById("First_Name__c").value, document.getElementById("Last_Name__c").value, document.getElementById("City__c").value, document.getElementById("Education__c").options[document.getElementById("Education__c").selectedIndex].value ); } </script> <apex:form > <apex:pageMessages id="errors" /> <apex:pageBlock title="Find Me A Candidate!" mode="edit"> <table width="100%" border="0"> <tr> <td width="200" valign="top"> <apex:pageBlock title="Parameters" mode="edit" id="criteria"> <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors"> <apex:param name="First_Name__c" value="" /> <apex:param name="Last_Name__c" value="" /> <apex:param name="City__c" value="" /> <apex:param name="Education__c" value="" /> </apex:actionFunction> <table cellpadding="2" cellspacing="2"> <tr> <td style="font-weight:bold;">First Name<br/> <input type="text" id="First_Name__c" onkeyup="doSearch();"/> </td> </tr> <tr> <td style="font-weight:bold;">Last Name<br/> <input type="text" id="Last_Name__c" onkeyup="doSearch();"/> </td> </tr> <tr> <td style="font-weight:bold;">City<br/> <input type="text" id="City__c" onkeyup="doSearch();"/> </td> </tr> <tr> <td style="font-weight:bold;">Education<br/> <select id="Education__c" onchange="doSearch();"> <option value=""></option> <apex:repeat value="{!Education }" var="edc"> <option value="{!edc}"> {!edc}</option> </apex:repeat> </select> </td> </tr> </table> </apex:pageBlock> </td> <td valign="top"> <apex:pageBlock mode="edit" id="results"> <apex:pageBlockTable value="{!candidates}" var="candidate"> <apex:column > <apex:facet name="header"> <apex:commandLink value="First Name" action="{!toggleSort}" rerender="results,debug"> <apex:param name="sortField" value="first_name__c" assignTo="{!sortField}"/> </apex:commandLink> </apex:facet> <apex:outputField value="{!Candidate.First_Name__c}"/> </apex:column> <apex:column > <apex:facet name="header"> <apex:commandLink value="Last Name" action="{!toggleSort}" rerender="results,debug"> <apex:param name="sortField" value="last_Name__c" assignTo="{!sortField}"/> </apex:commandLink> </apex:facet> <apex:outputField value="{!candidate.Last_Name__c}"/> </apex:column> <apex:column > <apex:facet name="header"> <apex:commandLink value="City" action="{!toggleSort}" rerender="results,debug"> <apex:param name="sortField" value="City__c" assignTo="{!sortField}"/> </apex:commandLink> </apex:facet> <apex:outputField value="{!candidate.City__c}"/> </apex:column> <apex:column > <apex:facet name="header"> <apex:commandLink value="Education" action="{!toggleSort}" rerender="results,debug"> <apex:param name="sortField" value="Education__c" assignTo="{!sortField}"/> </apex:commandLink> </apex:facet> <apex:outputField value="{!candidate.Education__c}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </td> </tr> </table> <apex:pageBlock title="Debug - SOQL" id="debug"> <apex:outputText value="{!debugSoql}" /> </apex:pageBlock> </apex:pageBlock> </apex:form> </apex:page>
here is my Controller code;
public with sharing class CandidateSearchController {
private String soql {get;set;}
// the collection of contacts to display
Public List<Candidate__c> candidates {get;set;}
// the current sort direction. deaults to asc
public String sortDir{
get { if (sortDir == null) { sortDir = 'asc'; }
return sortDir;
}
set;
}
// the current field to sort by. defaults to last name
public String sortField {
get {
if ( sortField == null) {
sortField = 'Last_Name__c'; } return sortField; }
set;
}
// format the soql for display on the visualforce page
public String debugSoql {
get { return soql + ' order by ' + sortField + ' ' + sortDir + 'limit 20 '; }
set;
}
// init the contrller and display some sample data when the page loads
public CandidatesearchController() {
soql='select First_name__c,Last_name__c,City__c,Education__c from Candidate__c where Candidate__c.First_name__c != null';
runQuery();
}
// toggles the sorting of query from asc<-->desc
public void toggleSort() {
//simply toggle the direction
sortDir = sortDir.equals('asc') ? 'desc' : 'asc' ;
// run the query again
runQuery();
}
//runs the actualquery
public void runQuery() {
try {
candidates = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20 ');
}
catch (Exception e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ' OOOps! ') );
}
}
// runs the search with parameters passed via JavaScript
public PageReference runSearch(){
String FirstName = Apexpages.currentPage().getParameters().get('First_Name__c');
String LastName = Apexpages.currentPage().getParameters().get('Last_Name__c');
String City = Apexpages.currentPage().getParameters().get('City__c');
String Education = Apexpages.currentPage().getParameters().get('Education__c');
soql='select First_name__c,Last_name__c,City__c,Education__c from Candidate__c where Candiadate__c.First_Name__c != null';
if (!FirstName.equals(''))
soql += ' and First_Name__c LIKE \' '+String.escapeSingleQuotes(FirstName)+'%\'';
if (!LastName.equals(''))
soql += ' and Last_Name__c LIKE \''+String.escapeSingleQuotes(LastName)+'%\'';
if (!City.equals(''))
soql += ' and City__c LIKE \''+String.escapeSingleQuotes(city)+'%\'';
if (!Education.equals(''))
soql += ' and Education__c LIKE (\''+Education+'\')';
// run the query again
runQuery();
return null;
}
// use apex describe to build the picklist values
public List<String> Education {
get {
if (Education == null) {
Education = new List<String>();
Schema.DescribeFieldResult field = Candidate__c.Education__c.getDescribe();
for (Schema.PicklistEntry f : field.getPicklistValues())
Education.add(f.getLabel());
}
return Education;
}
set;
}
}
The Only mistake was in the spelling candidate,
All Answers
The Only mistake was in the spelling candidate,
Hi,
Thank you for this post.
I have a little problem with this implementation. This dynamic search produces a weird result if you implement it only with one input field, and if you press several keys quickly which will send several requests to the server. It is unpredictable which response will return as the last one. The last response will define how the table looks.
For example, if you you quickly type "Ba" ( shift+b+a ) then requests will be send. If request for "B" returns response as the last one, then result will not be as expected.
Do you have a solution for this ?
Thanks.