简体   繁体   English

如何更改自动填充中的结果过滤器?

[英]How can I change the result filter in Autocomplete?

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: 有一种内置方法可以做到这一点:只为自动完成小部件中的source选项提供一个函数:

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/ http://jsfiddle.net/RSyrX/

Note that this example will always return "1234" because of the simple regular expression I used. 请注意,由于我使用的简单正则表达式,此示例将始终返回“1234”。 Something more useful would probably be to build the regex based on the term (also possible). 更有用的可能是根据术语(也可能)构建正则表达式。

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. 检查此行以获取过滤器功能和此行,以查看在提供数组作为source选项时如何调用它。

I've created an example in which only results containing the string 'jQuery' in the label are rendered: 我创建了一个示例,其中只呈现标签中包含字符串'jQuery'的结果:

var projects = [
    {
    value: "jquery",
    label: "jQuery",
    desc: "the write less, do more, JavaScript library",
    icon: "jquery_32x32.png"},
{
    value: "jquery-ui",
    label: "jQuery UI",
    desc: "the official user interface library for jQuery",
    icon: "jqueryui_32x32.png"},
{
    value: "sizzlejs",
    label: "Sizzle JS",
    desc: "a pure-JavaScript CSS selector engine",
    icon: "sizzlejs_32x32.png"}
];

$("input").autocomplete({
    source: projects
}).data("autocomplete")._renderItem = function(ul, item) {

    // this is where you can implement your regex
    if (item.label.indexOf("jQuery") !== -1) {
        return $("<li></li>").data("item.autocomplete", item).append("<a>" + item.label + "<br>" + item.desc + "</a>").appendTo(ul);
    }
};

Demo here. 在这里演示。

Ya you can override jQueryUI's autocomplete's default behaviour. 你可以覆盖jQueryUI的自动完成的默认行为。 In your controller you should write your server side logic to generate the result set. 在您的控制器中,您应该编写服务器端逻辑以生成结果集。 jQuery autcomplete by default uses q as parameter. jQuery autcomplete默认使用q作为参数。 Using which you can get the values and generate the result set which will be a list. 使用它可以获取值并生成将成为列表的结果集。 I think this will give you an idea. 我想这会给你一个想法。 Autocomplete just displays the result on each key storke. 自动填充只显示每个键盘上的结果。 You should take care of displaying logic 你应该注意显示逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM