How to make Chosen ignore accents in the search?

Asked

Viewed 448 times

3

I’m using the Chosen on my forms, but I’d like him to ignore the accents when filtering.

For example: In a city listing, filter the correctly to:

- africa

But it does not filter to (because the words differ by the accent):

- Africa

In the project’s Github, there are already discussions on this since 12/03/2012 (https://github.com/harvesthq/chosen/issues/536), but have not yet implemented this in the official code.

Reading the existing discussions, I found this functional example, posted by @felpasl:

However, I intend to implement its operation, but without changing the original sources, to minimize future complications when updating the code from the official repository.

I thought about replacing the method, externally, but I’m not able to do it. Below the code I tried, to replace the method...

var Chosen = $('#campo_select_com_chosen').chosen();
Chosen.prototype.search_string_match = function (search_string, regex) {
    alert('Método substituído com sucesso');
};

But I get this mistake:

Uncaught Typeerror: Cannot set Property 'search_string_match' of Undefined

My difficulty is in how to replace a lib method that I am using without altering the lib sources. If it were in PHP I would inherit the class and replace the method... but in Javascript I do not know how to do.

Could someone help me on how to do this implementation, Plugin type, on Chosen?

  • 1

    http://answall.com/questions/3994/como-fazer-uma-busca-ignorando-acentuação-em-javascript

  • @bfavaretto once again thank you for your support. My difficulty is in how to replace a lib method that I am using without altering the lib sources. If it were in PHP I would inherit the class and replace the method... but in Javascript I do not know how to do.

  • it seems that there is no way, Chosen does not expose anything for you to change from outside (at least by the code that is in your jsfiddle).

1 answer

2

Stackoverflow has a similar question, and there is an open Issue on Github requesting this change.

In this Issue has several users putting ways to change the code, one of them posted in this Stack answer and should already solve your problem.

Is in the Jsfiddle: http://fiddle.jshell.net/whqb5/1/
Should work by importing just below Chosen on account of prototypes.

Or if you want a more direct solution, in the latest answers in Issue Hanoii user has created a version of Chosen that is able to do this type of research: https://github.com/hanoii/chosen/releases

Question: https://stackoverflow.com/questions/31960166/chosen-search-accented-words
Issue: https://github.com/harvesthq/chosen/issues/536

  • Thank you for your attention, ma as said in the question, I am looking for a form of deployment without changes in the sources of Chosen.

  • 1

    Oh yes, I put it in the edit and forgot. So it’s like @bfavaretto said, there’s nothing to move. The file is a direct function that is called right at the end, there is not even the same name. To use the prototype if I’m not mistaken you would have to have an object. But even so, you would have to tamper with the code to create a class and instantiate Chosen (which would not be so easy).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.