如何编写忽略空格和点的自定义select2(4.0)匹配器?

时间:2016-09-05 18:40:43

标签: jquery-select2 select2 jquery-select2-4 select2-rails

我想编写一个自定义select2(版本4.0)匹配器,以便在执行搜索时忽略dotswhitespaces

EG。如果其中一个选项是Dr. R. N. Pandey,我应该只需在搜索框中输入Dr. R. N. Pandey即可找到rnp

2 个答案:

答案 0 :(得分:1)

请试试这个:

HTML:

@RequestMapping(value = "/entervehicle", method = RequestMethod.PUT)
public ResponseEntity<Void> enterNewVehicle(@RequestParam("licenceplate") String licenceplate, @RequestParam("placeType") String placeType, @RequestParam("levelNumber") int levelNumber, @RequestParam("placeNumber") int placeNumber){
....
}

JS:

<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>

请查看演示(&lt; 4.0):

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

选择2版本4.0

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

答案 1 :(得分: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);
    }
  });