?
Solved

How to get jQuery autocomplete results to populate input and append data to div upon select?

Posted on 2014-08-11
16
Medium Priority
?
1,337 Views
Last Modified: 2014-08-14
Experts,

I need to update the following code to enable the following:

1) On select of an autocomplete option, fill the input with the selected "employee_name"
2) On select of an autocomplete option, append the "employee_name" to another div

The autocomplete function works properly in that it returns results made available via my external data source.

I am just unable to do steps 1 and 2 above.

Assistance would be appreciated.

Cheers!

$(function() {
	var projects = {};
	$( "#employee_name" ).autocomplete({
		minLength: 2,
		source: function( request, response ) {
			var term = request.term;
			if (term in projects) {
				response(projects[ term ] );
				return;
			}
			$.getJSON("./scripts/search.php?employee",request,function(data,status,xhr) {
				projects[term] = data;
				response(data);
			});
		},
		select: function(ui,item) {
			var employee_name = item.employee_name;
			$("#employee_results" ).empty();
			$("#employee_results").append("<b>Testing: </b>" + employee_name);
			return false;
		}
	})
	.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li>" )
		.append( "<a>" + item.employee_name + "<br>" + item.manager + "</a>" )
		.appendTo( ul );
	};
});

Open in new window


<table cellpadding="2" cellspacing="2" border="0" width="100%">
    <tr>
    <td width="15%" valign="top">
    <h3><label for="employee_name">Employee Name: </label></h3>
    </td>
    <td align="left" width="35%" valign="top">
    <div class="ui-widget">
    <input style="width:90%; height:25px; padding:2px;" name="employee_name" id="employee_name" >
    </div>
    </td>
    <td width="50%" valign="top" align="left">
    <div class="ui-widget">
        <div id="employee_results" style="overflow: auto;" class="ui-widget-content">
            <!-- THIS IS WHERE THE EMPLOYEE INFORMATION WILL APPEAR AS A RESULT OF SEARCH -->
            
        </div>
    </div>
    </td>
    </tr>
</table>

Open in new window

0
Comment
Question by:evibesmusic
  • 9
  • 7
16 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 40255411
what is the response for?
http://yourwebserver.adresse/scripts/search.php?employee?term=a
0
 

Author Comment

by:evibesmusic
ID: 40255943
@leakim971:

It is a jSON array.

When ?term=Har

[{"employee_name":"Bobo Harrington","employee_role":"MD","facility":"FacilityA","department":"Shipping"},{"employee_name":"Smith Harris","employee_role":"SGT","facility":"FacilityB","department":"Receiving"}]
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 40256846
check the changes here :
			$(function() {
	var projects = {};
	$( "#employee_name" ).autocomplete({
		minLength: 2,
		source: function( request, response ) {
			var term = request.term;
			if (term in projects) {
				response(projects[ term ] );
				return;
			}
			$.getJSON("./scripts/search.php?employee",request,function(data,status,xhr) {
				projects[term] = data;
				response(data);
			});
		},
		select: function(ui,user_choice) {
			var employee_name = user_choice.employee_name;
			$("#employee_results").html("<b>Testing: </b>" + user_choice.item.employee_name);
			return false;
		}
	})
	.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li>" )
		.append( "<a>" + item.employee_name + "<br>" + item.employee_role + "</a>" )
		.appendTo( ul );
	};
});

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 83

Expert Comment

by:leakim971
ID: 40256856
basically we try : item.manager
but there's no " manager " field in your data, in bold what is available
employee_name":"Bobo Harrington","employee_role":"MD","facility":"FacilityA","department":"Shipping"
0
 

Author Comment

by:evibesmusic
ID: 40257250
@leakim971:

Using your suggested code, I get the following error message:

TypeError: user_choice.item is undefined
$("#employee_results").html("<b>Testing: </b>" + user_choice.item.employee_name)
0
 
LVL 83

Expert Comment

by:leakim971
ID: 40257263
do e have the same line 16?
0
 

Author Comment

by:evibesmusic
ID: 40257272
@leakim971:

Yes, I do have exactly what you posted in line 16: select: function(ui,user_choice) {

I've read that there are some coding differences between jQuery versions.

I am using jQuery 1.11.1 & jQueryUI 1.11.0
0
 
LVL 83

Expert Comment

by:leakim971
ID: 40257462
any link to see that ?
0
 

Author Comment

by:evibesmusic
ID: 40257572
@leakin971:

Sorry but, the site is behind a firewall but, I have pulled the resulting source code so you can see what is being output.
0
 
LVL 83

Expert Comment

by:leakim971
ID: 40257593
yes and my code was working fine on my side with your data
0
 

Author Comment

by:evibesmusic
ID: 40257604
So the code I just provided works for you using the array example?

You have it posted where I can see it?
0
 
LVL 83

Expert Comment

by:leakim971
ID: 40258139
first stepClipboard04.png
0
 

Author Comment

by:evibesmusic
ID: 40258559
@Leakim971:

I see that it is working but, your example doesn't seem to be correct. You've typed in "ev" and you get both employees in the array but, neither of them have a consecutive "ev" in their names?

On my end the script is executing properly by only giving me the names of those employees who match what I have typed (in your example "ev").

?
0
 
LVL 83

Expert Comment

by:leakim971
ID: 40258572
are you trying to debug mine instead your?
as I don't have you php script, I'm just returning using php the data...
0
 

Author Comment

by:evibesmusic
ID: 40258626
@leakim971:

No, I am not trying to debug your code but, the image you posted seems to be giving you erroneous results based on the sample data I posted.

My problem is that when I select an option from the suggested fields, the value of the option I select is not placed into the input field. The selected option's value is also not placed into the div.

I am simply trying to figure out why? Would you mind posting your code so I can see what you have done? Maybe there is a slight difference which is causing my script not to work properly?
0
 
LVL 83

Expert Comment

by:leakim971
ID: 40258887
<?php
echo '[{"employee_name":"Bobo Harrington","employee_role":"MD","facility":"FacilityA","department":"Shipping"},{"employee_name":"Smith Harris","employee_role":"SGT","facility":"FacilityB","department":"Receiving"}]';
?>

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month16 days, 14 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question