selectize.js icon indicating copy to clipboard operation
selectize.js copied to clipboard

Is there a way to share the same instance of options across several sectize controls?

Open mypark opened this issue 8 years ago • 4 comments

My use case is that I have a collection of tasks that I want to assign to several users.

Given each user is represented by a selectize input control.

When a task is selected by the input - it correctly hides that option from the single dropdown list. However I want that option to be hidden from the dropdownlist of the other selectize inputs as well - so that all the inputs share the same list of dwindling options.

Is there a way to synchronize the options list so that when an option is removed from one list it removes from all the selectize lists?

mypark avatar Dec 18 '17 18:12 mypark

I do have a simirlar req. did you find a solution for this one ?.

shamain avatar Jan 08 '18 05:01 shamain

Yeah I created a plugin - but it got a little messy since I also had to patch the search as well and couldn't do it in the plugin alone - thinking of creating a PR if you had suggestions on fixing it

Changing the core search

 if (settings.selectionGroupName || query !== self.lastQuery) {
 // bug where it wouldn't refresh on remvoing with my shared_selections plugin, so I directly modified it here
 
        self.lastQuery = query;
        result = self.sifter.search(query, $.extend(options, { score: calculateScore }));
        self.currentResults = result;
      } else {
        result = $.extend(true, {}, self.currentResults);
      }

Plugin

Selectize.define('shared_selections', function (options) {
    var self = this;
    
    Selectize.sharedSelections = Selectize.sharedSelections || {};

    self.setup = (function () {
      var original = self.setup;
      return function () {
        original.apply(this, arguments);
        if (!Selectize.sharedSelections[self.settings.selectionGroupName]) {
          Selectize.sharedSelections[self.settings.selectionGroupName] = [];
        }
      };
    })();


    self.removeItem = (function (value, silent) {
      var original = self.removeItem;
      return function (value, silent) {
        let sharedItems = Selectize.sharedSelections[self.settings.selectionGroupName];
        sharedItems.splice(sharedItems.indexOf(value), 1);
        return original.apply(this, arguments);
      };
    })();

    self.addItem = (function (value, silent) {
      var original = self.addItem;
      return function (value, silent) {
        Selectize.sharedSelections[self.settings.selectionGroupName].push(value);
        return original.apply(this, arguments);
      };
    })();

    self.search = (function (query) {
      var original = self.search;
      // any way to tell the original to run the search even if the lastQuery is the same?  Should I just inject a placeholder argument into the last query to trigger the search?
      return function (query) {

        // todo hacked a line in the original search to refresh list when
        // this plugin is set so that we can refresh lists on remove item
        // e.g. if (settings.selectionGroupName || query !== self.lastQuery) {
        var result = original.apply(this, arguments); //arguments

        // filter out selected items
        if (self.settings.hideSelected) {
          for (var i = result.items.length - 1; i >= 0; i--) {
            if (Selectize.sharedSelections[self.settings.selectionGroupName].indexOf(hash_key(result.items[i].id)) !== -1) {
              result.items.splice(i, 1);
            }
          }
        }
        return result;
      };
    })();

  });

mypark avatar Jan 09 '18 17:01 mypark

@mypark Great plugin!! But instead of hacking their code, simply nullify the last query each time you use their search function (like they do in their code in case they change any option etc...) Update your plugin and it should definitely go in their plugins page (Also removed that undefined 'hash_key()' function that you used)

self.search = (function (query) {
        var original = self.search;
        return function (query) {
            self.lastQuery = null;
            var result = original.apply(this, arguments); //arguments

            // filter out selected items
            if (self.settings.hideSelected) {
                for (var i = result.items.length - 1; i >= 0; i--) {
                    if (Selectize.sharedSelections[self.settings.selectionGroupName].indexOf(result.items[i].id) !== -1) {
                        result.items.splice(i, 1);
                    }
                }
            }
            return result;
        };
    })();

lyle45 avatar Jul 02 '18 11:07 lyle45

closing stale issues older than one year. If this issue was closed in error please message the maintainers. All issues must include a proper title, description, and examples.

risadams avatar Dec 01 '20 13:12 risadams