我如何更改自动完成的结果过滤器? [英] How can I change the result filter in Autocomplete?

查看:127
本文介绍了我如何更改自动完成的结果过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而不是让文字比赛,我想选择定期EX pression结果。

Instead of making a literal match I'd like to select results by regular expression.

我可以覆盖自动完成的默认行为做到这一点还是我需要一个替代结构?

Can I override Autocomplete's default behavior to accomplish this or do I need an alternate structure?

推荐答案

有一个内置的方式做到这一点:仅仅提供一个功能的的 中的自动完成构件选项:

There is a built-in way to do this: just provide a function for the source option in the autocomplete widget:

var items = ['Foo', 'Bar', 'Hello', 'Goodbye', '1234'];


$("#autocomplete").autocomplete({
    source: function(request, response) {
        // The term the user searched for;
        var term = request.term;

        // Extract matching items:
        var matches = $.grep(items, function(item, index) {
            // Build your regex here:
            return /\d+/.test(item);
        });

        // let autocomplete know the results:
        response(matches);
    }
});

http://jsfiddle.net/RSyrX/

请注意,这个例子总是返回1234因为我用简单的常规的前pression的。更有用的东西很可能是基于这个词(也有可能)来构建正则表达式。

Note that this example will always return "1234" because of the simple regular expression I used. Something more useful would probably be to build the regex based on the term (also possible).

这实际上是非常相似的插件本身筛选结果的方式。请查看此行,在过滤器的功能和< A HREF =htt​​ps://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.autocomplete.js#L290>此行来看看它是如何被称为如果提供数组作为选项。

This is actually very similar to the way that the widget itself filters results. Check out this line for the filter function and this line to see how it's called if you supply an array as the source option.

这篇关于我如何更改自动完成的结果过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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