Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery tag-it only allow available tags

Tags:

jquery

tag-it

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?

like image 628
user3077624 Avatar asked Dec 08 '13 09:12

user3077624


2 Answers

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;
            }

        },
like image 111
halfer Avatar answered Nov 15 '22 23:11

halfer


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
like image 26
Bassie Avatar answered Nov 15 '22 23:11

Bassie