我想编写一个自定义select2(版本4.0)匹配器,以便在执行搜索时忽略dots
和whitespaces
。
EG。如果其中一个选项是Dr. R. N. Pandey
,我应该只需在搜索框中输入Dr. R. N. Pandey
即可找到rnp
。
答案 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
答案 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);
}
});