I have an input field for locations with jquery-ui-autocomplete.
<script type="text/javascript">
$(document).ready(function(){
var location_input=$('input[id="location-autocomplete"]');
var cityNames = [
{ value: 'Mallorca' },
{ value: 'Berlin' },
{ value: 'London' },
// many other elements
];
location_input.autocomplete({
source: cityNames,
minLength: 2
});
} );
// keeps same width as box
jQuery.ui.autocomplete.prototype._resizeMenu = function () {
var ul = this.menu.element;
ul.outerWidth(this.element.outerWidth());
}
</script>
However, I am not happy with the case when the same location can have different names.
For example, let's say that the user wants to look for Mallorca. He could write: Mallorca, Majorca, Palma de Mallorca, PMI or Palma.
My first idea was to use the label
property
var cityNames = [
{ value: 'Mallorca', label: 'Palma de Mallorca' },
{ value: 'Mallorca', label: 'Mallorca' },
{ value: 'Mallorca', label: 'Majorca' },
// etc
];
However, this can be very confusing. Writing Ma the autocomplete would show Mallorca, Palma de Mallorca and Majorca, even if they are the same place.
I would like that when typing Ma the user only gets one suggestion. Similarly, no matter if the user types Maj, Mal or Pal, he should just get only one suggestion term for Mallorca.
Ideally, an additional property called input
working as the input keyword to be searched would have been perfect. Then, having an array with the properties value
and input
for each would have done the trick. Unfortunately, I did not find such a thing in the docs.
As you can see, I am using as source type an array
. For what I read, maybe the function
type would allow me to do something like this, but it is not clear to me how, as I am not very familiar with js and did not find any clear example about it.
How could I achieve that in a simple way?