3

カスタム select2 (バージョン 4.0) マッチャーを作成して、検索の実行時に無視されるようにdotsしたいと考えています。whitespaces

例えば。オプションの 1 つが の場合、検索ボックスに入力するだけでDr. R. N. Pandey検索できるはずです。Dr. R. N. Pandeyrnp

4

2 に答える 2

1

すでに提供されている回答に触発されました:

  function ignoreDotAndWhitespace(params, data) {
    params.term = params.term || '';

    term = params.term.toUpperCase();
    text = data.text.toUpperCase().replace(/\./g, '').replace(/\s+/g, '');

    if (text.indexOf(term) > -1 ) {
      return data;
    }
    return false;
  }

  $('.select2').select2({
    matcher: function(params, data) {
      return ignoreDotAndWhitespace(params, data);
    }
  });
于 2016-09-06T05:52:27.393 に答える
1

これを試してください:

HTML:

<select class='select2'>
    <option>Select</option>
    <optgroup label="Vegetables">
        <option>A. Celery</option>
        <option>Green R.P Pepper</option>
        <option>Kale</option>
    </optgroup>
    <optgroup label="Fruits">
        <option>Apple</option>
        <option>Orange</option>
        <option>Banana</option>
    </optgroup>
    <optgroup label="Nuts" disabled>
        <option>Almond</option>
        <option>Walnut</option>
        <option>Pine</option>
    </optgroup>
</select>

JS:

(function() {
    function matcher(term, text) {

        term = term.toUpperCase();
        text = text.toUpperCase().replace(/\./g, '').replace(/\s+/g, '');

        if (text.indexOf(term) > -1 ) {
            return true;
        }

        return false;
    }

    $(".select2").select2({
        matcher: matcher
    });
})();

デモから見てください(<4.0):

https://jsfiddle.net/xfw4tmbx/22/

2 バージョン 4.0 を選択

https://jsfiddle.net/11a998kw/1/

于 2016-09-05T19:38:24.963 に答える