Skip to content

Feature: return $item on onItemRemove callback (easy fix) #585

Closed
@tseven

Description

@tseven

Problem:
When acting on the onItemRemove callback, only the value of the option is passed. This doesn't allow us to know which selectized element this remove was performed on.

Other callbacks like onItemAdd(value, $item) return the $item.
I believe all the callbacks should return this information when available wether it's usefulness is immediately obvious or not.

Adding this information to callback is extremely trivial.
Simply adding , $item to the self.trigger('item_remove', value); function will do the trick:

        /**
         * Removes the selected item matching
         * the provided value.
         *
         * @param {string} value
         */
        removeItem: function(value) {
            var self = this;
            var $item, i, idx;

            $item = (typeof value === 'object') ? value : self.getItem(value);
            value = hash_key($item.attr('data-value'));
            i = self.items.indexOf(value);

            if (i !== -1) {
                $item.remove();
                if ($item.hasClass('active')) {
                    idx = self.$activeItems.indexOf($item[0]);
                    self.$activeItems.splice(idx, 1);
                }

                self.items.splice(i, 1);
                self.lastQuery = null;
                if (!self.settings.persist && self.userOptions.hasOwnProperty(value)) {
                    self.removeOption(value);
                }

                if (i < self.caretPos) {
                    self.setCaret(self.caretPos - 1);
                }

                self.refreshState();
                self.updatePlaceholder();
                self.updateOriginalInput();
                self.positionDropdown();
                self.trigger('item_remove', value, $item);
            }
        },

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions