aehlke/tag-it

Duplicate values gets deleted when removing a tag even when allowDuplicates: true

Opened this issue · 1 comments

With a text field initial value set to "1,2,1,2" deleting the last tag with value 2 will also delete the first occurrence of 2. Though you will still see the tag still existing, refreshing the page will load only "1,1"

Please see below console.log results log
screenshot 2017-03-02 11 47 01

Traced JS code and found that the reason of the issue is that grep was used to remove all occurrence with the same value.

Fixed issue by adding a few lines in removeTag to find the exact index of which tag is actually being deleted

        tag = $(tag);
        tag_index = tag.index(); //ADDED TO GET THE INDEX OF THE ITEM BEING DELETED
        

        // DEPRECATED.
        this._trigger('onTagRemoved', null, tag);

        if (this._trigger('beforeTagRemoved', null, {tag: tag, tagLabel: this.tagLabel(tag)}) === false) {
            return;
        }

        if (this.options.singleField) {
            var tags = this.assignedTags();
            var removedTagLabel = this.tagLabel(tag);

            /*
            OLD buggy implementation  
            tags = $.grep(tags, function(el){
                return el != removedTagLabel;
            });
            */

            tags.splice(tag_index, 1); //ADDED THIS TO REMOVE THE VALUE IN THE CORRECT INDEX

            this._updateSingleTagsField(tags);

Now getting below console.log() logs
screenshot 2017-03-02 17 10 05