Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Autocomplete matching multiple unordered words in a string

I am trying to show the best match of the search term typed in. For example

Right now Jquery does not give me the desired effect. When I type: one today the autocomplete will show nothing but if I type one day it will show the search results that start with those two words in that order. I want one today to show up one day is the first and last today. I want the search results to show up that have those words in them the ordering is not important. I have looked through here and could find nothing like this, it seems like such a common searching method I cannot see why no one has asked this question. Is there a built in method that handles this?

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <script>
  $(function() {
    var availableTags = [

          "one day is the first and last today" , "tuesday is today" , "one" , "one day is tomorrow"


    ];
    $( "#tags" ).autocomplete({
      source: availableTags, multiple: true,
        mustMatch: false
    });
  });
  </script>
</head>
<body>

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags" />
</div>


</body>
</html>
like image 213
Wonton_User Avatar asked Sep 30 '13 00:09

Wonton_User


1 Answers

Try overriding the default filter logic provided by auto complete.

// Overrides the default autocomplete filter function to search for matched on atleast 1 word in each of the input term's words
$.ui.autocomplete.filter = function (array, terms) {
    arrayOfTerms = terms.split(" ");
    var term = $.map(arrayOfTerms, function (tm) {
         return $.ui.autocomplete.escapeRegex(tm);
    }).join('|');
   var matcher = new RegExp("\\b" + term, "i");
    return $.grep(array, function (value) {
       return matcher.test(value.label || value.value || value);
    });
};

Fiddle

Or create your own filter function and handle the search's return, so keeping complete's filter function as it is.

function customFilter(array, terms) {
    arrayOfTerms = terms.split(" ");
    var term = $.map(arrayOfTerms, function (tm) {
         return $.ui.autocomplete.escapeRegex(tm);
    }).join('|');
   var matcher = new RegExp("\\b" + term, "i");
    return $.grep(array, function (value) {
       return matcher.test(value.label || value.value || value);
    });
};

$("#tags").autocomplete({
    source: availableTags,
    multiple: true,
    mustMatch: false
    ,source: function (request, response) {
        response(customFilter(
        availableTags, request.term));
    },
});

Fiddle

like image 168
PSL Avatar answered Nov 01 '22 12:11

PSL