jQuery tag-it allow tags only from autocomplete source
Asked Answered
F

4

6

I'm trying to implement a tag-it input except that I want to restrict the users to only select values from the autocomplete box. I tried to overried the beforeTagAdded event using the source json and check if the tag exists in the source property but no luck.

here's the code, see beforeTagAdded function.

     $(document).ready(function () {
        var itemId;
        var theTags = $('#msg_to');
        theTags.tagit({
            autocomplete: {
                source: [{ id: "1", value: 'David'}, { id: "2", value: 'John' }],
                minLength: 0,
                select: function (event, ui) {
                    itemId = ui.item.id;
                    theTags.tagit("createTag", ui.item.value);
                }
            },
            showAutocompleteOnFocus: true,
            afterTagAdded: function (event, ui) {
                if (itemId) {
                    $(ui.tag).find('input').attr('name', "tag[\'" + itemId + "']['" + ui.tagLabel + "']");
                    itemId = null;
                }
            },
            beforeTagAdded: function (event, ui) {
                var id = ui.autocomplete.source; // not working

           // what to do next?
            }
        })
    });
</script>

Thanks in advance

Frijol answered 13/2, 2014 at 9:24 Comment(0)
I
5

My solution:

$(function() {     
   var currentlyValidTags = ['ac', 'dc'];
   $('input[name="_tags"]').tagit({
    availableTags: currentlyValidTags,
    autocomplete: { source: function( request, response ) {        
        var filter = removeDiacritics(request.term.toLowerCase());
        response( $.grep(currentlyValidTags, function(element) {
                        return (element.toLowerCase().indexOf(filter) === 0);
                    }));
    }},  
    beforeTagAdded: function(event, ui) {
      if ($.inArray(ui.tagLabel, currentlyValidTags) == -1) {
        return false;
      }
    }          
  });    
});
Infraction answered 13/5, 2014 at 5:44 Comment(0)
S
2

The following works for me:

var currentlyValidTags = [];
$('#tags').tagit({
  autocomplete: {
    source: function(req, resp) {
      search(req, function(data) {
        currentlyValidTags = data;
        resp(data);
      });
    }
  },
  beforeTagAdded: function(event, ui) {
    if (!_.contains(currentlyValidTags, ui.tagLabel)) {
      return false;
    }
  }
});

The above solution works with version 2.0 at 65d6fb4dad833bf490f2a7b27063ecd08f792ede (different from version 2.0 on tag v2.0).

Snowblink answered 29/4, 2014 at 17:6 Comment(0)
F
1

There is a fork of tag-it that will do what you want:

https://github.com/chrisleishman/tag-it

It has a requireAutocomplete setting.

(I had to kind of merge both versions, as I needed things from each ... but hopefully that will help you.)

Fantasm answered 15/4, 2014 at 20:25 Comment(1)
Thank you all, I found that using select2 is more easy to use.Frijol
V
0

If you are looking for autocomplete via ajax, here is a solution.

//this will be set true only if fetched via ajax and also selected from autocomplete
$.is_ajax_fetched = false; 

Inside the autocomplete you would need these events:

select: function( event, ui ) {
$.is_ajax_fetched = true;
return true;
},

//select event will not work alone as tag-it captures event to check for comma and other stuff    
close: function( event, ui ) {
if($.is_ajax_fetched == true){
$('.ui-autocomplete-input').blur();
$('.ui-autocomplete-input').focus();
}
}

Now in the tag-it call, you will need to add call events in the options:

beforeTagAdded: function(event, ui) {
    if($.is_ajax_fetched != true){return false;}
},

afterTagAdded: function(event, ui) {
    $.is_ajax_fetched = false;
},
Violate answered 14/7, 2017 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.