angry kiwi
angry kiwi

Reputation: 11485

chosen - search accented words

I'm using this plugin to beautify the select box. It has search function that when type it will filter out the result. The problem is that, if my select has words like á, é, í, ó, ú, ü, ñ, the plugin won't understand. For example: if I type a, it should assume I'm typing á, therefore give the results with words like a and á. But it did not. only gave me words with a.

So, doesn't anybody know how to make it understand accented word?

Upvotes: 2

Views: 2892

Answers (3)

Matěj Štágl
Matěj Štágl

Reputation: 1037

One solution would be to modify get_search_regex (input search value) and after that search_string_match (loops trough dropdown items).

Should you modify only the latter, you would filter matches out when accented characters are used in the input string.

Steps:

  1. normalize input in get_search_regex -> at the beginning of the function add t = t.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
    (in minified js search for t.prototype.get_search_regex=function(t){)
  2. normalize dropdown items in search_string_match -> at the beginning of the function add t = t.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
    (in minified js search for t.prototype.search_string_match=function(t,e))

Explanation:

normalize("NFD")

Normalizes a string according to unicode normalization form. NFD stands for Canonical Decomposition and it "decomposes" unicode characters. For example š is decomposed into ˇ + s, into ff and into d. Hence this is able to handle way more cases than a table one could come up with. Might be an overkill and you can always compose a table instead.

After we've normalized the string we are left with some characters we don't want (as in case of š we have ˇ). This is solved with regex that replaces all occurances of characters in range of u0300 - u036f (which are known diacritical marks) with nothing ("").

replace(/[\u0300-\u036f]/g, "")

2021+ alternative of this would be to:

replace(/\p{Diacritic}/gu, "")

Upvotes: 0

Jasp Salgado
Jasp Salgado

Reputation: 1

I solved this issue by adding this code:

var t=t.normalize("NFD").replace(/[\u0300-\u036f]/g, "");

to the function t.prototype.search_string_match=function(t,e)

Upvotes: 0

bmunslow
bmunslow

Reputation: 370

What you are asking for is currently a feature request in the project:

https://github.com/harvesthq/chosen/issues/536

Although there isn't an official release that includes this functionality at the moment, some users have proposed working solutions you can try.

For example:

http://fiddle.jshell.net/whqb5/1/

Upvotes: 4

Related Questions