Skip to content
This repository has been archived by the owner on Aug 15, 2019. It is now read-only.

Added an autocomplete_only option to disable manual inputting tags #294

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ described here.
autocomplete:{selectFirst:true,width:'100px',autoFill:true}
});

If you want to disable manual additions to the tagsinput field and only want to use
autocompleted values use the autocomplete_only option

$('#tags').tagsInput({
autocomplete_url:'http://myserver.com/api/autocomplete',
autocomplete_only:true
});

You can add and remove tags by calling the addTag() and removeTag() functions.

$('#tags').addTag('foo');
Expand Down Expand Up @@ -88,6 +96,7 @@ option to false.
$(selector).tagsInput({
'autocomplete_url': url_to_autocomplete_api,
'autocomplete': { option: value, option: value},
'autocomplete_only': false,
'height':'100px',
'width':'300px',
'interactive':true,
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.tagsinput.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 17 additions & 10 deletions src/jquery.tagsinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

var delimiter = new Array();
var tags_callbacks = new Array();
var autocomplete_only = false;
$.fn.doAutosize = function(o){
var minWidth = $(this).data('minwidth'),
maxWidth = $(this).data('maxwidth'),
Expand Down Expand Up @@ -85,14 +86,20 @@

value = jQuery.trim(value);

var skipTag = false;

if (options.unique) {
var skipTag = $(this).tagExist(value);
if(skipTag == true) {
//Marks fake input as not_valid to let styling it
$('#'+id+'_tag').addClass('not_valid');
}
} else {
var skipTag = false;
skipTag = $(this).tagExist(value);
}

// Only allow autocompleting
if (!skipTag && autocomplete_only && !options.autocompleted) {
skipTag = true;
}

//Marks fake input as not_valid to let styling it
if (skipTag == true) {
$('#'+id+'_tag').addClass('not_valid');
}

if (value !='' && skipTag != true) {
Expand Down Expand Up @@ -271,13 +278,13 @@
$(data.fake_input).autocomplete(settings.autocomplete_url, settings.autocomplete);
$(data.fake_input).bind('result',data,function(event,data,formatted) {
if (data) {
$('#'+id).addTag(data[0] + "",{focus:true,unique:(settings.unique)});
$('#'+id).addTag(data[0] + "",{focus:true,unique:(settings.unique),autocompleted:true});
}
});
} else if (jQuery.ui.autocomplete !== undefined) {
$(data.fake_input).autocomplete(autocomplete_options);
$(data.fake_input).bind('autocompleteselect',data,function(event,ui) {
$(event.data.real_input).addTag(ui.item.value,{focus:true,unique:(settings.unique)});
$(event.data.real_input).addTag(ui.item.value,{focus:true,unique:(settings.unique),autocompleted:true});
return false;
});
}
Expand Down Expand Up @@ -352,7 +359,7 @@
var id = $(obj).attr('id');
var tags = val.split(delimiter[id]);
for (i=0; i<tags.length; i++) {
$(obj).addTag(tags[i],{focus:false,callback:false});
$(obj).addTag(tags[i],{focus:false,callback:false,autocompleted:true});
}
if(tags_callbacks[id] && tags_callbacks[id]['onChange'])
{
Expand Down