This library offers no easy option to customize the search no... I tried to read the sources first on Github (they are in Coffeescript, language I do not understand) then in the downloaded file (chosen.jquery.js
), and identified the part where he actually does the search:
AbstractChosen.prototype.winnow_results = function() {
...
searchText = this.get_search_text();
escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
regexAnchor = this.search_contains ? "" : "^";
regex = new RegExp(regexAnchor + escapedSearchText, 'i');
...
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
...
option.search_match = this.search_string_match(option.search_text, regex);
...
startpos = option.search_text.search(zregex);
text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length);
option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos);
My first attempt was to modify the get_search_text
to ignore the accentuation. For this, I took the instance of the Chosen
and replaced the method:
var Chosen = $("#meu-select").chosen().data("chosen");
var get_search_text = Chosen.get_search_text;
Chosen.get_search_text = function() {
return replaceSpecialChars(get_search_text.apply(this));
};
(the function replaceSpecialChars
vein of @Leonardo Cardoso’s reply; is not complete, for a definitive version accompany the related question)
With that, the search term now ignores the accent. The next step would be to make the term being sought also ignore. For this, I modified in a similar way the search_string_match
:
var search_string_match = Chosen.search_string_match;
Chosen.search_string_match = function(search_string, regex) {
return search_string_match(replaceSpecialChars(search_string), regex);
}
Here is the result (see the code at the end). It works, but the problem is that at the time of highlight the term sought, it presents problems - since the original regex does not match the original term. From the code above, I can’t see a way to interpose replaceSpecialChars
in a way that does not cause this problem, but when I have time I will look at it again. For now, it remains as a partial response... (Editing: I quit! If you want to continue where I left off, investigate the method result_add_option
. But I think the most "sane" way would be to edit the sources directly...)
This question is very broad, because it involves solving two distinct problems: 1) how to make a search ignoring the accentuation; 2) how integrate this search to jQuery Chosen. I asked a separate question for the general case, and I will try to answer the specific case [of jQuery Chosen] here.
– mgibsonbr