Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching through international characters with Chosen.js

Has anyone found a fix for searching through international characters using chosen.js?

for example if I have a multi-select field with the following options: - A French Château - An English Chateau

And I search for "Cha"

Only the english will show up.

like image 419
Gareth Jones Avatar asked Oct 20 '25 06:10

Gareth Jones


1 Answers

I don't know, if it is not too late for answering, but it could help someone else probably.

The point is to strip diacritics from entered string and from matched option string during comparison. My modification is working with jquery Chosen plugin in version 1.1.0, downloaded from github.

First you have to have a function to strip diacritics (i've added it after winnow_results())

// strip czech diacritics from string
AbstractChosen.prototype.strip_diacritics= function(string) {
  var 
    translationTable= [
      // input with diacritics - add your characters if necessary
      "éěÉĚřŘťŤžŽúÚůŮüÜíÍóÓáÁšŠďĎýÝčČňŇäÄĺĹľĽŕŔöÖ",
      // stripped output
      "eeEErRtTzZuUuUuUiIoOaAsSdDyYcCnNaAlLlLrRoO",
    ],
    output= '';

  // scan through whole string
  for (var i= 0; i < string.length; i++) {
    var charPosition= translationTable[0].indexOf(string[i]);
    output+= charPosition == -1 ? string[i] : translationTable[1][charPosition];
  }

  return output;
}

Then use it in appropriate places in winnow_results() function. Referencing chosen.jquery.js lines:

line #315

searchText = this.get_search_text();
// replace with
searchText = this.strip_diacritics(this.get_search_text());

line #339

option.search_match = this.search_string_match(option.search_text, regex);
// replace with
option.search_match = this.search_string_match(this.strip_diacritics(option.search_text), regex);

and finally line #345

startpos = option.search_text.search(zregex);
// replace with
startpos = this.strip_diacritics(option.search_text).search(zregex);

And you're done :-).

(I feel like writing some "savegame byte replacement" instruction for extra lives in old DOS game)

Whole modified file at pastebin.

Sept 29th, 2016: Modifications made in Chosen 1.6.2, tested ok.

like image 112
Jonez1 Avatar answered Oct 22 '25 19:10

Jonez1