Those standard reference Visualforce inputfields not working out for you? Why not switch over to jQuery autocomplete?
First, load up what you need:
<script type="text/javascript" src="{!URLFOR($Resource.JQueryUICup,'js/jquery-1.4.2.min.js')}"></script>
<script type="text/javascript" src="{!URLFOR($Resource.JQueryUICup,'js/jquery-ui-1.8.1.custom.min.js')}"></script>
<script type="text/javascript" src="{!URLFOR($Resource.JQAutocomplete11,'jquery-autocomplete/jquery.autocomplete.pack.js')}"></script>
<apex:stylesheet value="{!URLFOR($Resource.JQAutocomplete11,'jquery-autocomplete/jquery.autocomplete.css')}"/>
<apex:stylesheet value="{!URLFOR($Resource.JQAutocomplete11,'jquery-autocomplete/demo/main.css')}"/>
Then add a method that will transform all of the data for the object you are looking into json:
public String getJson(String displayColumn)
{
if (jsonData == null)
{
jsonData = '';
try {
System.debug('getJsonifiedObjectRecords for:' + fieldname + ' ' + objectToLookup );
List<String> columns = new List<String>();
columns.add('id');
if(displayColumn != null)
columns.add(displayColumn);
else
columns.add('name');
String queryString = 'Select id, ' + displayColumn + ' from ' + objectToLookup + ' limit 1000';
System.debug('queryString:' + queryString);
List<sObject> sos = Database.query(queryString);
//Map<String, Schema.SObjectField> columns = sos.getSObjectType().getDescribe().fields.getMap();
jsonData += '[';
for (sObject so : sos) {
jsonData += ' { ';
for(String column : columns)
{
System.debug('jsonData:' + jsonData);
try
{
string cellVal = String.valueOf( so.get(column));
jsonData += ''+column+': "'+cellVal+'",';
}
catch(Exception ex)
{
System.debug(ex);
}
}
jsonData = jsonData.substring(0, jsonData.length() - 1);
jsonData = jsonData + '},';
}
jsonData = jsonData.subString(0,jsonData.length() - 1);
jsonData = jsonData + ']';
}
catch(Exception e)
{
System.debug('Bad query in Jsonify Call ' + e);
}
}
System.debug('jsonData:' + jsonData);
return jsonData;
}
We use the displayName variable to specify which field we want to display when generating the autocomplete. See the community boards and Joe Ferraro’s article for some more cool examples of generating JSON.
Now we are ready to include our Autocomplete field. All we need is one field to show and a second hidden field to store the result ID in.
Something like this:
<apex:outputPanel>
<tr>
<td><apex:outputText value="field.Label__c" /></td>
<td>
<span class="cfs{!field.order}">
<input id="cfi{!field.order}" type="text" style="width:200px"/>
<apex:inputHidden value="{!field.Text_Value__c}" />
</span>
</td>
</tr>
</apex:outputPanel>
Now all we need to do is add in the nifty Javascript that enables it:
var cf{!f.order}data = {!f.json};
$("#cfi{!f.order}").autocomplete(cf{!f.order}data, {
formatItem: function(item) {
return item.name;
}
}).result(function(event, item) {
$(".cfs{!f.order} :input:hidden:first").val(item.id);
});
</apex:repeat>
My code is a little more complicated than yours needs to be since I am automatically populating all of the form elements.
See a live examples of this and other JQuery enhanced components I am working on here.


6 comments
Comments feed for this article
February 28, 2011 at 11:20 pm
Sreekumar Menon
Thanks for the post.
can you post the fill code or send it to my email address
sree
February 28, 2011 at 11:20 pm
Sreekumar Menon
read fill code as “full code”
February 28, 2011 at 11:41 pm
d3developer
Most of it is here:
http://code.google.com/p/sfdcjqueryenhancements/source/browse/trunk/src/classes/EnhancedLookupController.cls
March 1, 2011 at 4:10 am
Sreekumar Menon
got it , thank you..
March 4, 2011 at 5:46 am
Sreekumar Menon
one more question-
how do I pass additional parameters (multiple display columns, filter criteria)
to enhancedlookupcontorller class..
dc- I want to display name and phone in this format name(phone)
filter – exclude all inactive accounts.
I tried this doesn’t work-
$(“#agencyName”).autocomplete({
source: function(request, response) {
$.getJSON(“{!$Page.largeLookup}”, {
“core.apexpages.devmode.url” :’1′,
dc:name,phone,
filterstring:isactive=false,
term: request.term
}, response);
},
March 4, 2011 at 6:46 am
Sreekumar Menon
never mind – I got it working by making few changes to enhancedlookupcontroller class..thanks