i use jQuery tag-it in my script:
$("#user_autocomplete").tagit({
tagSource: function (request, response) {
$.ajax({
data: {
term: request.term
},
type: "POST",
url: "/site2/message/users.php",
dataType: "json",
success: function (data) {
response($.map(data, function (item) {
return {
label: item,
value: item
}
}));
}
});
},
tagLimit: 3,
autocomplete: {
delay: 0,
minLength: 1
},
});
In this case, all input fields are confirmed. But I want only the fields that are available to be added. How to do it?
Posted on behalf of OP:
I'm using the beforeTagAdded function I found my answer:
tagSource: function(request, response)
{
$.ajax({
data: { term:request.term },
type: "POST",
url: "/site2/message/users.php",
dataType: "json",
success: function( data ) {
array = data;
response( $.map( data, function( item ) {
return {
label:item,
value: item
}
}));
}
});
},
tagLimit :3,
autocomplete: {delay: 0, minLength: 1},
beforeTagAdded: function(event, ui) {
if(array.indexOf(ui.tagLabel) == -1)
{
return false;
}
if(ui.tagLabel == "not found")
{
return false;
}
},
I was able to achieve this by adding the following check to the top of the createTag
function:
if (this.options.onlyAvailableTags && $.inArray(this._formatStr(value), this.options.availableTags) == -1) {
return false;
}
And then adding this option in the tag-it
call:
onlyAvailableTags: true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With