tag-it icon indicating copy to clipboard operation
tag-it copied to clipboard

jQuery Tag-It Access to instance from function

Open indapublic opened this issue 12 years ago • 5 comments

I want to disallow new tags.

$('#my').tagit({
    tagSource: ['One', 'Two'],
    beforeTagAdded: function (e, ui) {
        return (-1 !== ???.indexOf(ui.tagLabel))
    }
});

How I can to get instance tagSource in beforeTagAdded function?

indapublic avatar Nov 18 '13 09:11 indapublic

Or maybe need to pass tagSource or availableTags in preprocessTag function?

if(this.options.preprocessTag) {
                value = this.options.preprocessTag(value, this.options.tagSource);
            }

indapublic avatar Nov 18 '13 10:11 indapublic

I needed this too. This is how I did it:

$(function() {
    var availableTags = ['one', 'two'];
    $('#myTagUL').tagit({
        singleField: true,
        singleFieldNode: '#myHiddenInputForPostingToServer',
        availableTags: availableTags,
        beforeTagAdded: function(event, ui) {
            return availableTags.indexOf(ui.tagLabel) > -1;
        }
    });
});

Because the plugin doesn't expose the options, you can only pass them when initializing and you can no longer access them afterwards (explained here - search for "options outside"). That's why I need to declare an availableTags variable outside the tagit() call.

You might not need the first two options. What the beforeTag does is return a boolean to indicate if the tag should be added.

Check out the documentation on beforeTagAdded.

petermorlion avatar Dec 13 '13 09:12 petermorlion

Yeah, I know this solution. But I want to use multiple tacit instances. And separately availableTags is not uncomfortably.

indapublic avatar Dec 13 '13 10:12 indapublic

Well, in my case I'm using an ASP.NET UserControl, so what it essentially adds to the page is:

$(function() {
    var availableTags = ['one', 'two'];
    // etc.
});

$(function() {
    var availableTags = ['apple', 'orange'];
    // etc.
});

These variable will not conflict with each other as they stay in the scope of their function. So you could use $(function() {}) separately for each tagit call. I don't believe this has any effect on jQuery's performance.

petermorlion avatar Dec 13 '13 10:12 petermorlion

Facing the same issue.

rohitkhatri avatar May 25 '16 07:05 rohitkhatri