How to make jQuery Chosen disregard accentuation at the time of the search?

3

I developed a site that uses jQuery Chosen , and I wish it did not consider accents at the time of the search.

For example, I have a list with several names, and I need to look for JOSE (digit without an accent), but I would like it to show JOSEPH (with an accent) too.

How to do this?

    
asked by anonymous 04.02.2014 / 01:22

2 answers

3

I suggest you use the select2 that has the same purpose, with a very similar layout, which already does what you do want, and still provides an option for you to programmatically change the search code.

    
04.02.2014 / 01:40
2

This library does not offer any easy option to customize the search ... I tried reading the first source code in GitHub ( they are in CoffeeScript, language that I do not master) later on the downloaded file (% with%), and I identified the part where he does the actual 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 chosen.jquery.js to ignore the accent. For this, I took the instance of get_search_text 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 Chosen function came from this @Leonardo Cardoso answer ; it is not complete for a version definite accompany the related question )

The search term now ignores the accent. The next step would be to ignore the term being searched as well. For this, I similarly modified the replaceSpecialChars :

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 code at the end). It works, but the problem is that when it comes to highlighting the searched term, it has problems - since the original regex does not match the original term. By the code above, I can not see a way to get search_string_match in a way that does not cause this problem, but when I have a while I look at it again. For now, it's a partial answer ... ( Edit: I gave up!) If I want to continue from where I left off, investigate the replaceSpecialChars method. directly ...)

    
04.02.2014 / 03:51