Alp
Alp

Reputation: 29739

Select2: Extend matcher for tag data

I use Select2 for tagging purposes. It works like a charm, but i have an additional requirement: The matcher should match more than only the text of the items.

Let me explain. The matcher function takes two parameters, term and text. This allows us to compare the given tags and the entered text. But if i define additional data for each tag (via the tags function) i am unable to access it in the matcher.

Example code:

// #myinput is a hidden input field
$('#myinput').select2({
  matcher: function(term, text) {
      return text.toUpperCase().indexOf(term.toUpperCase()) >= 0;
  },
  tags: function() {
      var tags = [];
      $('#myselect').find('option').each(function(index, option) {
          option = $(option);
          tags.push({
              id: option.val(),
              text: option.text(),
              category: option.data('category')
          });
      });
      return tags;
  }
})

I set the mandatory id and text properties for each tag. In addition, there is a string named category. I want to match against the text and the category instead of only the text.

Is that possible?

Upvotes: 0

Views: 2702

Answers (2)

user3801025
user3801025

Reputation: 1

Using 3.4.2, still not implemented. bu tthere is quite simple hack, really.

find in select2.(min).js line where matcher is called with 2 args ("e.matcher(");

on my version, i have something like this:

{var a=this.text!==b,c=a?this.text:this;(""===f||e.matcher(f,c) 

add "this" to third arg

e.matcher(f,c,this);

as result, entire tag object would be passed :)

Upvotes: 0

igor.vaynberg
igor.vaynberg

Reputation: 1453

this is not currently possible (as of 3.3), file an enhancement request with a jsfiddle in select2's github issue tracker.

Upvotes: 1

Related Questions