Reputation: 9017
I need to have a multiline textbox with a free text allowed, but If I start type-in characters: "@@"
the autocomplete box with available tags should show up and allow me to select tags from existing, and "@@" characters should not be present in the tag name.
Currently I'm playing with tag-it plugin for jquery UI but cannot figure out how to allow free text and only shw autocomplete box on "@@" enter. and how to user textarea instead of regular input.
Also, I'd like to force them to select from the list if they enter @@ and do not allow to type free text (first available selection would be good)
Javascript:
$(document).ready(function() {
//The demo tag array
var availableTags = [
{value: 1, label: 'tag1'},
{value: 2, label: 'tag2'},
{value: 3, label: 'tag3'}];
//The text input
var input = $("input#text");
//The tagit list
var instance = $("<ul class=\"tags\"></ul>");
//Store the current tags
//Note: the tags here can be split by any of the trigger keys
// as tagit will split on the trigger keys anything passed
var currentTags = input.val();
//Hide the input and append tagit to the dom
input.hide().after(instance);
//Initialize tagit
instance.tagit({
tagSource:availableTags,
tagsChanged:function () {
//Get the tags
var tags = instance.tagit('tags');
var tagString = [];
//Pull out only value
for (var i in tags){
tagString.push(tags[i].value);
}
//Put the tags into the input, joint by a ','
input.val(tagString.join(','));
}
});
//Add pre-loaded tags to tagit
instance.tagit('add', currentTags);
});
html:
<p>This example shows how to use Tagit on an input!</p>
<input type="text" id="text" name="tags" value="one,two,three"/>
testing here: http://jsfiddle.net/hailwood/u8zj5/
Upvotes: 8
Views: 2288
Reputation: 36900
I've created a Meteor package for this, which allows both free text and multiple autocomplete sources. Meteor's data model allows for fast multi-rule searching with custom rendered lists. If you're not using Meteor for your web app, (I believe) you unfortunately won't find anything this awesome for autocompletion.
Autocompleting users with @
, where online users are shown in green:
In the same line, autocompleting something else using !
with metadata and bootstrap icons:
Fork, pull, and improve:
Upvotes: 0
Reputation: 79830
Since you had used tag-it plugin.. I have added some handler to the input to handle
@@
to show auto complete as you type@@
I still need time to look into the Do not allow free text if @@ is typed
DEMO: http://jsfiddle.net/xBgfJ/2/ and below is the full code,
Note: Below code is tweak to the existing plugin code.
$(document).ready(function() {
//The demo tag array
var availableTags = [{value: 1, label: 'tag1'},{ value: 2,label: 'tag2'}, { value: 3, label: 'tag3'}];
//The text input
var input = $("input#text");
//The tagit list
var instance = $("<ul class=\"tags\"></ul>");
//Store the current tags
//Note: the tags here can be split by any of the trigger keys
// as tagit will split on the trigger keys anything passed
var currentTags = input.val();
//Hide the input and append tagit to the dom
input.hide().after(instance);
//Initialize tagit
instance.tagit({
tagSource: availableTags,
tagsChanged: function() {
//Get the tags
var tags = instance.tagit('tags');
var tagString = [];
//Pull out only value
for (var i in tags) {
tagString.push(tags[i].value);
}
//Put the tags into the input, joint by a ','
input.val(tagString.join(','));
},
onTagAdded: function() {
inpNext.parent().find('.pre-filter').remove();
}
});
//Add pre-loaded tags to tagit
instance.tagit('add', currentTags);
var inpNext = input.next();
var autoCompelteMenu = $('.ui-autocomplete', inpNext);
inpNext.on('keydown', '.tagit-input', function(e) {
var $parent = $(this).parent();
var $preFilter = $parent.find('.pre-filter');
if (e.which == 8 && this.value == '') { //backspace
$preFilter.remove();
} else if (e.which == 9 || e.which == 32
|| e.which == 188 || e.which == 44 ||
e.which == 13 ) { //tab or space, comma and enter
$preFilter.remove();
autoCompelteMenu.css('opacity', 0);
}
}).on('keypress', '.tagit-input', function(e) {
var $parent = $(this).parent();
var $preFilter = $parent.find('.pre-filter');
if (e.which == 64 && !$preFilter.length) {
$parent.prepend('<span class="pre-filter hidden">@</span>');
autoCompelteMenu.css('opacity', 0);
} else if ( e.which == 64 && $preFilter.length) {
e.preventDefault();
this.value = '';
$preFilter.append('@').removeClass('hidden');
autoCompelteMenu.css('opacity', 1);
}
return;
}).on('blur', '.tagit-input', function() {
$(this).parent().find('span').remove();
autoCompelteMenu.css('opacity', 0);
});
});
Upvotes: 4