JQuery AutoComplete In Visualforce
In this article, we will perform Autocomplete Jquery search in visualforce page on Lead object.most of the developers requirement are create custom search and searching perform when keyup and update data when select search data and show the success message.
same process I did here , follow the process and create own Autocomplete search.
here we will use JavaScript Remoting to populate data in search.
Click For Demo
Now understand by example--
Step-1 :
Create Apex class which has the remote method to populate data and update method to update Lead when you select any Lead when search.
Here I used Lead object but you can any object for the search.
Apex Class:
- global class AutoCompleteCtr
- {
- public static List<String> leadNameList{get;set;}
- public String lName{get;set;}
- public String successMessage{get;set;}
- @RemoteAction
- global static String[] populateLeadByName(String value)
- {
- try
- {
- leadNameList=new List<String>();
- if(String.isNotBlank(value))
- {
- value= '%'+value+'%';
- List<Lead> lstLead=[Select Id,name from Lead where name LIKE:value];
- for(Lead l:lstLead)
- {
- leadNameList.add(l.name);
- }
- }
- return leadNameList;
- }
- catch(Exception e)
- {
- System.debug('Message:='+e.getMessage()+'**Line Number='+e.getLineNumber());
- }
- return null;
- }
- public void updateLead()
- {
- try
- {
- lName=ApexPages.currentPage().getParameters().get('leadName');
- if(String.isNotBlank(lName))
- {
- Lead lead=[select id,name,Update_By__c from Lead where Name=:lName];
- lead.Update_By__c='Autocomplete';
- update lead;
- successMessage='Lead Update Successfully';
- }
- }
- catch(Exception e)
- {
- System.debug('Message:='+e.getMessage()+'**Line Number='+e.getLineNumber());
- }
- }
- }
Visualforce Page:
- <apex:page controller="AutoCompleteCtr" sidebar="false">
- <html lang="en">
- <head>
- <meta charset="utf-8"/>
- <meta name="viewport" content="width=device-width, initial-scale=1"/>
- <title>jQuery UI Autocomplete - Default functionality</title>
- <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"/>
- <link rel="stylesheet" href="/resources/demos/style.css"/>
- <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
- <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
- <script>
- $( function() {
- $( "#tags" ).autocomplete({
- source: function(request, response) {
- Visualforce.remoting.Manager.invokeAction( '{!$RemoteAction.AutoCompleteCtr.populateLeadByName}',
- request.term,
- function(result, event){
- if (event.status)
- {
- console.log(result);
- response(result);
- }
- else
- {
- alert(event.message);
- }
- });
- },
- select:function( event, ui ){
- // Call class method by passing this value using action function
- callControllermethod(ui.item.label);
- }
- });
- } );
- </script>
- </head>
- <body>
- <apex:form >
- <apex:actionRegion >
- <apex:actionFunction name="callControllermethod" action="{!updateLead}" reRender="mssge">
- <apex:param name="leadName" value=""/>
- </apex:actionFunction>
- </apex:actionRegion>
- <div class="ui-widget">
- <label for="tags">Search Lead: </label>
- <input id="tags"/>
- </div>
- <apex:outputPanel id="mssge">
- <div style="margin: 5px;color: green;">{!successMessage}</div>
- </apex:outputPanel>
- </apex:form>
- </body>
- </html>
- </apex:page>
After use this code then your autocomplete is ready. Search any text and select any populated record then it will call controller method via action function and update record and show success message on page.
After select, it show success message like--
I used this code. the autocomplete is not firing for me
ReplyDeleteWhat is the problem you are getting?
DeleteThe autocomplete is not firing, nothing happens when i enter the input. The apex controller is also not called
ReplyDelete//Script
$( function() {
$("#tags").autocomplete({
minLength: 2,
source: function(request, response) {
Visualforce.remoting.Manager.invokeAction( '{!$RemoteAction.Classname.MethodName}',
request.term,
function(result, event){
if (event.status)
{
console.log(result);
response(result);
}
else
{
alert(event.message);
}
});
},
select:function( event, ui ){
// Call class method by passing this value using action function
//callControllermethod(ui.item.label);
}
});
} );
did you get result in console? First check the status , check controller ,check method you are calling . if not send me complete code via mail so i will rectify .
DeleteThis comment has been removed by the author.
ReplyDelete