Skip to content

Commit

Permalink
Merge pull request #903 from unepwcmc/fix/diacritic-searching-trade-db
Browse files Browse the repository at this point in the history
fix: allow searching for countries with non-standard characters in tr…
  • Loading branch information
lucacug authored Oct 21, 2022
2 parents d624f49 + 723ccd0 commit 0e23dee
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions app/assets/javascripts/cites_trade/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,35 @@ $(document).ready(function(){
unLock('initSourcesObj');
}

removeCasingAndDiacritics = function (value) {
return value.toString()
.normalize('NFD')
.replace(/[\u0300-\u036F]/g, '')
.toLowerCase()
}

matchWithDiacritics = function (term, text) {
// If there are no search terms, return all of the data
if ($.trim(term) === '') {
return text;
}

// Do not display the item if there is no 'text' property
if (typeof text === 'undefined') {
return null;
}

var searchTerm = removeCasingAndDiacritics(term)
var optionText = removeCasingAndDiacritics(text)

if (optionText.indexOf(searchTerm) > -1) {
return text;
}

// Return `null` if the term should not be displayed
return null;
}

initExpctyImpcty = function (data) {
var args = {
data: data.geo_entities,
Expand All @@ -272,7 +301,8 @@ $(document).ready(function(){
$('#expcty').select2({
width: '75%',
allowClear: false,
closeOnSelect: false
closeOnSelect: false,
matcher: matchWithDiacritics
}).on('change', function(e){
var selection = "";
if (e.val.length == 0) {
Expand All @@ -298,7 +328,8 @@ $(document).ready(function(){
$('#impcty').select2({
width: '75%',
allowClear: false,
closeOnSelect: false
closeOnSelect: false,
matcher: matchWithDiacritics
}).on('change', function(e){
selection = "";
if (e.val.length == 0) {
Expand Down

0 comments on commit 0e23dee

Please sign in to comment.