Quick explaination: I have 3 inputs first_name, last_name and contact_number. They all have the class name autocomplete. e.g.
<input type="input" name="first_name" id="first_name" class="autocomplete">
<input type="input" name="last_name" id="last_name" class="autocomplete">
<input type="input" name="contact_number" id="contact_number" class="autocomplete">
I use the autocomplete class as a selector for initiating the jQuery UI autocomplete function (see code below) such that filling in any of these will result in an ajax search using all 3 inputs. Because I use all 3 fields to do the search the result has to be in a specific place (not under each input as is normally the case) so I use a div with a table inside which in turn displays the results. This is possible by overriding the internal _renderItem function (see code below).
This all works perfectly fine, however, only for the very first input in the form e.g. first_name. The other inputs all show up the dropdown li list below their respective inputs. It seems the _renderItem override is ignored for subsequent inputs. I have tried swapping around the inputs and whichever is first works correctly and the others don't. Any suggestions as to how I could fix the behaviour?
$(document).ready(function() {
$(".autocomplete").autocomplete({
search: function(event, ui) {
$("#autocompleteoutput table tbody").empty();
$("#autocompleteoutput").css("display", "inline");
},
source: function(request, response) {
jQuery.ajax({
url: "'.site_url('reservations/search_customer').'",
type: "post",
dataType: "json",
data: {
first_name: $("#first_name").val(),
last_name: $("#last_name").val(),
contact_number: $("#contact_number").val(),
'.$this->security->get_csrf_token_name().' : "'.$this->security->get_csrf_hash().'"
},
success: function(data) {
response(jQuery.map(data, function(item) {
return {
diner_id: item.diner_id,
first_name: item.first_name,
last_name: item.last_name,
dialing_code: item.dialing_code,
country_id: item.country_id,
contact_number: item.contact_number,
email: item.email
}
}))
}
})
}
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<tr class=\"customerselect\" data-dinerid=\"" + item.diner_id + "\" data-fname=\"" + item.first_name + "\" data-lname=\"" + item.last_name + "\" data-countryid=\"" + item.country_id + "\" data-num=\"" + item.contact_number + "\" data-email=\"" + item.email + "\"></tr>" )
.data( "item.autocomplete", item )
.append( "<td><span class=\"icon-user\">" + item.first_name + " " + item.last_name + "</span></td>")
.append( "<td><span class=\"icon-phone\">(+" + item.dialing_code + ") " + item.contact_number + "</span></td>" )
.append( "<td><span class=\"icon-mail\">" + item.email + "</span></td>" )
.appendTo($("#autocompleteoutput table tbody"));
};
});
The autocomplete (options) method declares that an HTML <input> element must be managed as an input field that will be displayed above a list of suggestions. The options parameter is an object that specifies the behavior of the list of suggestions when the user is typing in the input field.
Autocomplete. The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try. The datasource is a simple JavaScript array, provided to the widget using the source-option.
This method does not accept any arguments. In addition to the autocomplete (options) method which we saw in the previous sections, JqueryUI provides event methods which gets triggered for a particular event. These event methods are listed below −
The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try. The datasource is a simple JavaScript array, provided to the widget using the source-option. Want to learn more about the autocomplete widget?
The .data("autocomplete") here returned only the first element's autocomplete data. Try using this method separately for each input after assigning the autocomplete control.
I mean like this
function myRenderFunc(ul,item){
// code for the _renderItem method
}
$(".autocomplete").autocomplete({
//your autocomplete code
})
$('#first_name').data( "autocomplete" )._renderItem = myRenderFunc;
$('#last_name').data( "autocomplete" )._renderItem = myRenderFunc;
$('#contact_number').data( "autocomplete" )._renderItem = myRenderFunc;
I tried this right now and it worked for me. Should work for you too.
This works for me too, especially if the input element generated dynamically :
$('.autocomplete').each(function() {
$(this).data('uiAutocomplete')._renderItem = function (ul, item) {
// render item code
};
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With