How we can get the ID of selected tags using Jquery tagit?
Asked Answered
M

3

4

I have a input tag field ,and I want to get the ID of the selected tages So I have try http://jsfiddle.net/u8zj5/19/ But my problem I want to get the id not label or value to pass into id="show" but I failed.

<input type="text" id="field1" name="field1" value=""/>
<span id="show">show ID here</span>

jQuery(document).ready(function(){
var availableTags = [{"id":"144","label":"Allicelabel","value":"Allice value"}];
jQuery("input#field1").each(function(){
    var target = jQuery(this);
    var currenttags = target.val();
    target.hide()        
          .after("<ul class=\"tags\"><li>"+currenttags+"</li></ul>");
    var instance = target.next();
    instance.tagit({
        tagSource:availableTags,
        tagsChanged:function () {
            var tags = instance.tagit('tags');
            var tagString = [];
            for (var i in tags){
                tagString.push(tags[i].value);
            }
            $("#show").html(tagString.join(','));
        },
        sortable:true,
        triggerKeys: ['enter', 'comma', 'tab']
    });
});

});

Anyone here that used jQuery Tagit (Demo Page) Could help me to solve this

Modred answered 26/7, 2012 at 8:27 Comment(0)
M
3

I had the same problem and I what did was modify tag-it.js. When you call the function select you need to send the ID through the function _addTag

self._addTag(ui.item.label, ui.item.value, ui.item.id);

Then youu just need to get the id:

_addTag: function(label, value, id) {
    ...
    this._addSelect(label, value, id);
    ...
}

And here append the ID on a hidden Select

_addSelect: function(label, value, id) {
        var opt = $('<option>').attr({
            'selected':'selected',
            'value':id
        }).text(label);
        this.select.append(opt);

With this you can have, one label for the autocomplete list, one value to show in the tag, and the ID on a hidden select.

Malloch answered 12/8, 2012 at 20:31 Comment(0)
W
9

I had the same problem, but didn't want to alter the default behavior of the plugin. So, instead, I used the provided hooks to add new behaivior.

var availableTags = [
    {
        label: "myTag",
        value: "myTag",
        id: 1
    },
    //etc...
];
var assignedTags = [];


$('#singleinputfield').tagit( {
                            tagSource: function (request, response) {
                                    //setup the search to search the label
                                    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                                    response($.grep(availableTags, function (value) {
                                        return matcher.test(value.label);
                                    }));
                            },
                            beforeTagAdded: function(event, ui){
//get id for that tag and signal if it was in the tagSource list
                                var id, result = false;
                                $.each(availableTags, function(){
                                    if(ui.tagLabel === this.label){
                                        result = true;
                                        id=this.id;
                                        return false;
                                    }
                                });
                                if(result){
//put id in the list of ids we are using
                                    assignedTags.push(id);
                                }
                                return result;
                            },
                            afterTagAdded: function(event, ui){
//replace the values in the single input field with the assigned ids
                                    $('#singleinputfield').val(assignedTags.join(','));
                            },
                            afterTagRemoved: function(event, ui){
                                $('#singleinputfield').val(assignedTags.join(','));
                            },
                            beforeTagRemoved: function(event, ui){
                                var id;
//get the id for the removed tag and signal if it was in the tagSource list
                                $.each(availableTags, function(){
                                    if(ui.tagLabel === this.label){
                                        result = true;
                                        id = this.id;
                                        return false;
                                    }
                                });
                                if(result){
//remove the unassigned tag's id from our list
                                    assignedTags = $.grep(assignedTags, function(el){
                                        return el != id;
                                    });
                                }
                            }
                        });
Wise answered 8/11, 2013 at 18:21 Comment(0)
M
3

I had the same problem and I what did was modify tag-it.js. When you call the function select you need to send the ID through the function _addTag

self._addTag(ui.item.label, ui.item.value, ui.item.id);

Then youu just need to get the id:

_addTag: function(label, value, id) {
    ...
    this._addSelect(label, value, id);
    ...
}

And here append the ID on a hidden Select

_addSelect: function(label, value, id) {
        var opt = $('<option>').attr({
            'selected':'selected',
            'value':id
        }).text(label);
        this.select.append(opt);

With this you can have, one label for the autocomplete list, one value to show in the tag, and the ID on a hidden select.

Malloch answered 12/8, 2012 at 20:31 Comment(0)
S
0

Use some other plugin like Select2. It has actually got support for this.

Spineless answered 28/11, 2013 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.