Select2 TokenSeparators not working for S2MultiCheckboxes Extension

Viewed 227

I am using the S2MultiCheckboxes with Select2 4.0.3, taken from here: https://github.com/wasikuss/select2-multi-checkboxes

The plugin is working great. However, when adding the "tokenSeparator" option, it is not being implemented in Select2.

Here is the S2MultiCheckboxes code that extends the Select2 plugin:

<script>
    (function($) {
      var S2MultiCheckboxes = function(options, element) {
        var self = this;
        self.options = options;
        self.$element = $(element);
        var values = self.$element.val();
        self.$element.removeAttr('multiple');
        self.select2 = self.$element.select2({
          allowClear: true,
          minimumResultsForSearch: options.minimumResultsForSearch,
          placeholder: options.placeholder,
          tokenSeparators: options.tokenSeparators,
          closeOnSelect: false,
          templateSelection: function() {
            return self.options.templateSelection(self.$element.val() || [], $('option', self.$element).length); 
          },
          templateResult: function(result) {
            if (result.loading !== undefined)
              return result.text;
            return $('<div>').text(result.text).addClass(self.options.wrapClass);
          },
          matcher: function(params, data) {
            var original_matcher = $.fn.select2.defaults.defaults.matcher;
            var result = original_matcher(params, data);
            if (result && self.options.searchMatchOptGroups && data.children && result.children && data.children.length != result.children.length) {
              result.children = data.children;
            }
            return result;
          }
        }).data('select2');
        self.select2.$results.off("mouseup").on("mouseup", ".select2-results__option[aria-selected]", (function(self) {
          return function(evt) {
              
            var $this = $(this);
            var data = $this.data('data');
    
            if ($this.attr('aria-selected') === 'true') {
              self.trigger('unselect', {
                originalEvent: evt,
                data: data
              });
              return;
            }
    
            self.trigger('select', {
              originalEvent: evt,
              data: data
            });
          }
        })(self.select2));
        self.$element.attr('multiple', 'multiple').val(values).trigger('change.select2');
      }
    
      $.fn.extend({
        select2MultiCheckboxes: function() {
          var options = $.extend({
            placeholder: 'Choose elements',
            templateSelection: function(selected, total) {
              return selected.length + ' of ' + total + ' selected';
            },
            wrapClass: 'wrap',
            minimumResultsForSearch: -1,
            searchMatchOptGroups: true
          }, arguments[0]);
    
          this.each(function() {
            new S2MultiCheckboxes(options, this);
          });
        }
      });
    })(jQuery);
</script>

I added the line: tokenSeparators: options.tokenSeparators, at line 13. When I pass the tokenSeparator in as a | character, it still outputs the value as a comma-separated list, not a pipe when I get/output the select2 values.

I have also tried:tokenSeparators: '|' and tokenSeparators: ['|'] I have also tried adding the tags:true option which did not work. I have also tried using separator: '|' which did not work.

Any idea why this isn't working or what needs to be done to get the tokenSeparator changed to a pipe character?

Thanks.

2 Answers

After reading the select2 documentation, tokenSeparators is not about the output but splitting/tokenising the free text input.

Could it be that your library returns/outputs an array of strings that you use as a string ? Converting an array of strings to a string will put commas between each strings. If so, you could do something like data.join('|').

From doc:

tagging

In addition to a prepopulated menu of options, Select2 can dynamically create new options from text input by the user in the search box. This feature is called "tagging". To enable tagging, set the tags option to true

templateSelection

The appearance of selected results can be customized by using the templateSelection configuration option. This takes a callback that transforms the selection data object into a string representation or jQuery object

templateResult

By default, Select2 will display the text property of each data object within the list of results. The appearance of search results in the dropdown can be customized by using the templateResult option

tokenSeparators options is used with tagging mode for taking input.

select2-multi-checkboxes library customize the select options using the templateResult and selection result which is show in input using templateSelection.

If you want to use tokenSeparators then you may need to either use the select2 library directly or If still you want to use it with select2-multi-checkboxes library then remove options and code from library which is possibly the cause why you are not able to use tokenSeparators option.

Related