服务器端自动建议不过滤值

时间:2018-04-27 07:27:21

标签: c# jquery .net ajax asp.net-mvc

我有一个建议框,当有客户端源绑定时,它完美地工作(例如:availableTags)

$(function () {
    var availableTags = [
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"];
    $("#tags").autocomplete({
        source: availableTags
    });


});

http://jsfiddle.net/fwaq3dkz/28/

但是当我将源更改为服务器端时,它仍会列出建议的值,但它不会过滤我在建议框中输入的值

$("#tags").autocomplete({
    source: function (request, response) {
        $.getJSON('/User/GetUsersJSONAsync', function (data) {
            var users = [];
            for (let key in data) {

                users.push(data[key].userPrincipalName);
            }
            response(users);
        });
    },    
});

http://jsfiddle.net/fwaq3dkz/31/

PS:上面的小提琴不会给出任何JSON回复,因为它在我的代码中击中了一个MVC控制器,我不能在小提琴中使用,这仅仅是为了表示目的。

意思是,当我输入“A”时,它会在第一种情况下列出所有带有“A”和“a”的标签,这是客户端来源。 但是,它不会在第二种情况下过滤,即服务器端源,而是会列出所有内容。

2 个答案:

答案 0 :(得分:0)

通过向severside添加参数来修复,感谢@ user1672994的提示

  

var textInputObj = {textValue:$(“#search”)。val()};

// Sets up the multicolumn autocomplete widget.
$("#search").mcautocomplete({
    // These next two options are what this plugin adds to the autocomplete widget.
    showHeader: false,
    columns: columns,   
    source: function (request, response) {

        var textInputObj = { textValue: $("#search").val() };

        $.getJSON('/User/GetUsersJSONAsync', textInputObj, function (data) {
            var users = [];

            for (let key in data) {
                   users.push(new Array(data[key].surname ? data[key].surname : "Blank", data[key].displayName, data[key].userPrincipalName));                 

            }          
            response(users);
        });
    },    

答案 1 :(得分:-1)

可能是因为这是异步发生的,您是否尝试在返回JSON响应后设置自动完成?例如:

$(document).ready(function() {
    $.getJSON('/User/GetUsersJSONAsync', function (data) {
        var users = [];
        for (let key in data) {
            users.push(data[key].userPrincipalName);
        }
        $("#tags").autocomplete({
            source: users
        });
    });
});