Jquery自动完成匹配字符串中的多个无序单词 [英] Jquery Autocomplete matching multiple unordered words in a string

查看:126
本文介绍了Jquery自动完成匹配字符串中的多个无序单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示输入的搜索字词的最佳匹配。例如

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

现在Jquery没有给我想要的效果。当我输入:今天一个时,自动填充功能将不显示任何内容,但如果我输入一天,则会显示以该顺序中的这两个单词开头的搜索结果。我想今天一个来显示一天是第一天,也就是今天。我希望搜索结果显示在其中包含这些单词的顺序并不重要。我在这里看了一下,却找不到这样的东西,这似乎是一种常见的搜索方法,我看不出为什么没有人问过这个问题。是否有处理此问题的内置方法?

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>


推荐答案

尝试覆盖自动完成提供的默认过滤器逻辑。

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

这篇关于Jquery自动完成匹配字符串中的多个无序单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆