Jquery Autocomplete Special Char Trigger
Solution 1:
As already mentioned, you would require multiple words approach here.
The link to original source has already been provided above. So, I would like show what I understood from your doubt.
But first let me know if you want to have autocompletion like after '@' all label that start with 'a' should give results that only start with 'a' and not those which contain 'a'.
Since I suppose this would be of much better use, I have code for that part.
Working Demo: http://jsfiddle.net/AJmJt/2/
$(function() {
//Since you told that labels start with '@'var utilizatoriJson = [
{'label': "@ActionScript",'id':'1'},
{'label': "@Java",'id':'2'},
{'label': "@C++",'id':'3'},
{'label': "@Javascript",'id':'4'},
{'label': "@Python",'id':'5'},
{'label': "@BASIC",'id':'6'},
{'label': "@ColdFusion",'id':'7'},
{'label': "@Haskell",'id':'8'},
{'label': "@Lisp",'id':'9'},
{'label': "@Scala",'id':'10'}
];
functionsplit( val ) {
return val.split( / \s*/ );
}
functionextractLast( term ) {
returnsplit( term ).pop();
}
$( "#tags" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 1,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last termvar lastword = extractLast(request.term);
// Regexp for filtering those labels that start with '@'var matcher = newRegExp( "^" + $.ui.autocomplete.escapeRegex( lastword ), "i" );
// Get all labelsvar labels = utilizatoriJson.map( function( item ) { return item.label; });
var results = $.grep( labels, function( item ) {
return matcher.test( item );
});
response( $.ui.autocomplete.filter( results, lastword ) );
},
focus: function() {
// prevent value inserted on focusreturnfalse;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( " " );
returnfalse;
}
});
});
<linkhref="http://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css"><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><scriptsrc="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script><divclass="ui-widget"><labelfor="tags">Tags: </label><inputid="tags"size="50"></div>
If you don't want something like I said, then there is no need for RegExp matching, but there you need to see if word starts with '@' or not.
Code for this behavior working: http://jsfiddle.net/rPfY8/1/
$(function() {
var utilizatoriJson = [
{'label': "@ActionScript",'id':'1'},
{'label': "@Java",'id':'2'},
{'label': "@C++",'id':'3'},
{'label': "@Javascript",'id':'4'},
{'label': "@Python",'id':'5'},
{'label': "@BASIC",'id':'6'},
{'label': "@ColdFusion",'id':'7'},
{'label': "@Haskell",'id':'8'},
{'label': "@Lisp",'id':'9'},
{'label': "@Scala",'id':'10'}
];
functionsplit( val ) {
return val.split( / \s*/ );
}
functionextractLast( term ) {
returnsplit( term ).pop();
}
$( "#tags" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 1,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last termvar lastword = extractLast(request.term);
if(lastword[0] != '@')
returnfalse;
// Get all labelsvar labels = utilizatoriJson.map(function(item){return item.label;});
response( $.ui.autocomplete.filter(
labels, lastword ) );
},
focus: function() {
// prevent value inserted on focusreturnfalse;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( " " );
returnfalse;
}
});
});
<linkhref="http://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css"><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><scriptsrc="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script><divclass="ui-widget"><labelfor="tags">Tags: </label><inputid="tags"size="50"></div>
Solution 2:
You don't want to disable/re-enable the autocomplete as that prevents the control from doing anything. You can do this the way you propose, but there's another approach. Take a look at the example here: http://jqueryui.com/autocomplete/#multiple
This should do what you're looking for, you can update the filter to only do a "starts with" search
Here's a JSFIDDLE example of what I mean: http://jsfiddle.net/UhL5d/
And the corresponding Javascript:
$(function () {
var availableTags = [
{value: 1, label: "@ActionScript" },
{value: 2, label: "@AppleScript" },
{value: 3, label: "@Asp" },
{value: 4, label: "@BASIC" },
{value: 5, label: "@C" } ];
functionsplit(val) {
return val.split(/\s/);
}
functionextractLast(term) {
returnsplit(term).pop();
}
$("#textarea_mesaj_colaborare").autocomplete({
source: function (request, response) {
// delegate back to autocomplete, but extract the last termresponse($.ui.autocomplete.filter(
availableTags, extractLast(request.term)));
},
focus: function () {
// prevent value inserted on focusreturnfalse;
},
select: function (event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.label);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(" ");
returnfalse;
}
});
});
Post a Comment for "Jquery Autocomplete Special Char Trigger"