The answer of abstraktor gave me a good starting point, but there were some missing parts. So I developed an example on github: jquery-autocomplete-inner
Here is the complete source code of the example (no HTML), and some explanation:
$().ready(function() {
// search only, if the regexp matches
var cities = [
"Amsterdam", "Stuttgart", "Singapore", "Madrid", "Barcelona", "Hamburg",
"Esslingen", "Berlin", "Frankfurt", "Essingen", "Straßburg", "London",
"Hannover", "Weil am Rhein", "Tuttlingen", "München", "Marsaille", "Paris",
"Manchester", "Rome", "Neapel", "New York", "Brasil", "Rio de Janeiro"
];
// Defines for the example the match to take which is any word (with Umlauts!!).
function _leftMatch(string, area) {
return string.substring(0, area.selectionStart).match(/[wäöüÄÖÜß]+$/)
}
function _setCursorPosition(area, pos) {
if (area.setSelectionRange) {
area.setSelectionRange(pos, pos);
} else if (area.createTextRange) {
var range = area.createTextRange();
range.collapse(true);
range.moveEnd( character , pos);
range.moveStart( character , pos);
range.select();
}
}
$("#citites").autocomplete({
position: { my : "right top", at: "right bottom" },
source: function(request, response) {
var str = _leftMatch(request.term, $("#citites")[0]);
str = (str != null) ? str[0] : "";
response($.ui.autocomplete.filter(
cities, str));
},
//minLength: 2, // does have no effect, regexpression is used instead
focus: function() {
// prevent value inserted on focus
return false;
},
// Insert the match inside the ui element at the current position by replacing the matching substring
select: function(event, ui) {
//alert("completing "+ui.item.value);},
var m = _leftMatch(this.value, this)[0];
var beg = this.value.substring(0, this.selectionStart - m.length);
this.value = beg + ui.item.value + this.value.substring(this.selectionStart, this.value.length);
var pos = beg.length + ui.item.value.length;
_setCursorPosition(this, pos);
return false;
},
search:function(event, ui) {
var m = _leftMatch(this.value, this);
return (m != null )
}
});
})
- First the data of the examples, some cities (mostly German)
- A helper function to extract the matching substring left from the cursor, named _leftMatch
- A helper function copied mostly from jQuery Set Cursor Position in Text Area
- Then in the usage of autocomplete the following points:
- Search only when the substring (_leftMatch) before the cursor position matches the defined regexpression (in my example
/[wäöüÄÖÜß]+$/
--> all german work characters).
- Filter the source things by using the request - response variation and filtering there by the substring (as in the other examples of autocomplete with multiple values
- Replace the matching substring with the selection from the user and ensure that the cursor is positioned right after that.
If you want to take a look, download the zip-file from the GitHup repository and start the local example under examples/cities-local.html
.