pawelczak / EasyAutocomplete

JQuery autocomplete plugin
http://easyautocomplete.com
MIT License
729 stars 242 forks source link

No result for multiple words #226

Open eguvenc opened 8 years ago

eguvenc commented 8 years ago

This is my countries.xml file.

`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

France AgenFR34459

`

When i search like "France" its ok When i search like "France Agen" its ok (but if data contains comma "," no result) When i search like "Agen France" no result.

ram79 commented 8 years ago

I added following piece of code under "match" in "list" option and now it shows results matching the words in any order:

method: function(value, phrase) {
    phraseSplit = phrase.split(' ');
    if (value.match(phrase)) return true;
    for (var j=0; j<phraseSplit.length; j++) {
        if (!value.match(phraseSplit[j])) return false;
    }
    return true;
}
sfazekas commented 8 years ago

For what I needed, this should be re-worked to add to the config, however I modified the "findMatch" function for multiple words having a minimum of 4 chars.

            for(var i = 0, length = list.length; i < length; i += 1) {

                value = config.get("getValue")(list[i]);
                if ($.trim(phrase.length) > 3) {

                  var words = phrase.split(' ');
                  var listMatchArr = [];
                  $.each(words, function(idx, word){
                    //console.log("chk match: " + word + " | " + match(value, word));
                    var W = word.replace(/[\W_]+/g,""); // match on alphaNum chars only
                    if (match(value, W) && W.length > 3 && $.inArray(i,listMatchArr) == -1) {  //phrase  
                        preparedList.push(list[i]);
            listMatchArr.push(i);                         
                    };
                  });
                };

            }

ALSO for highlighting the words.

        function highlightPhrase(string, phrase) {
            var escapedPhrase = escapeRegExp(phrase);
            var words = escapedPhrase.split(' ');
            //console.log("words.length: " + words.length);
            var retStr = string;
            $.each(words, function(idx, word){
              var W = word.replace(/[\W_]+/g,"");
      if (W.length > 3) {
                retStr = retStr.replace(new RegExp("(" + W + ")", "gi") , "<b>$1</b>");
              } 
            });
            return retStr;
        }
vikas-bedwal commented 7 years ago

@eguvenc It was really helpful. It returns even I search in reverse order but not highlighted. Can you please help me to know how can I highlight those text.

@sfazekas Where do I need to put these both snippets?

idmeneo commented 2 years ago

I added following piece of code under "match" in "list" option and now it shows results matching the words in any order:

method: function(value, phrase) {
    phraseSplit = phrase.split(' ');
    if (value.match(phrase)) return true;
    for (var j=0; j<phraseSplit.length; j++) {
        if (!value.match(phraseSplit[j])) return false;
    }
    return true;
}

Thank you so much!