简体   繁体   English

无法获取jQuery autoComplete进行过滤

[英]Can't get jQuery autoComplete to filter

Hi I'm trying to get jQuery's autoComplete ui to work. 嗨,我正在尝试使jQuery的autoComplete ui正常工作。

Currently I have a asp.net TextBox and I'm calling a webservice and chaining the results of this list to the textbox. 目前,我有一个asp.net文本框,正在调用Web服务并将此列表的结果链接到文本框。 The webservice that I'm calling gets an unfiltered list of results from an SQL database. 我正在调用的Web服务从SQL数据库获取未经过滤的结果列表。

public List<string> getAutoCompleteList() {
    DataSet dsAutoList = getAutoList();
    DataTable dtAutoList = dsAutoList.Tables[0];
    List<string> lstTitles = new List<string>();
    foreach (DataRow drAutoList in dtAutoList.Rows)
    {
        //zAutolist = zAutolist + drAutoList["course_title"].ToString();
        lstTitles.Add(drAutoList["course_title"].ToString());
    }

    return lstTitles;
}

The javascripts that assigns this list to the textbox is (on load) 将此列表分配给文本框的javascript是(加载时)

function autoComplete() {
    $(".txtSearch").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "/webservice/wsJQueryAutoComplete.asmx/getAutoCompleteList",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                //dataFilter: function(data) { return data; },
                success: function(data) {
                    response($.map(data.d, function(item) {
                        return { value: item }
                    }))
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },
        minLength: 3    // MINIMUM 3 CHARACTER TO START WITH.
    });
}

All is well as in if I start typing in the text box then the results appears but the problem is that the list of results isn't filtered based upon what I type in the search box it just shows every item in the list and doesn't filter it regardless of what I type in. 一切都很好,如果我开始在文本框中输入内容,则会显示结果,但问题是结果列表未根据我在搜索框中键入的内容进行过滤,它只显示列表中的每个项目,而不会不管输入什么内容,都对其进行过滤。

Could someone spare a few minutes and lemme know what I'm doing wrong please? 有人可以花几分钟时间让我知道我在做什么错吗?

Thanks, Craig 谢谢,克雷格

Since you don't want to dynamically populate the search terms based on input, you must populate your search items in an array prior to initialising the autocomplete, ie move the $.ajax call outside of the call to autocomplete: 由于您不想基于输入动态填充搜索词,因此必须在初始化自动完成功能之前将搜索项填充到数组中,即将$ .ajax调用移至自动完成功能之外:

$.ajax({
    url: "/webservice/wsJQueryAutoComplete.asmx/getAutoCompleteList",
    dataType: "json",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    // Changed here:
    success: function(data) {
        $(".txtSearch").autocomplete({
             source: data.d
        });
    }
});

So, when initialising the jquery autocomplete, you are passing a pre-populated array as the source parameter which will cause the filter to act on the static list. 因此,在初始化jquery自动完成时,您将传递一个预填充的数组作为源参数,这将导致过滤器作用于静态列表。

$(".txtSearch").autocomplete({
    source: prepopulatedList
});

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

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