开发者

jquery-ui autocomplete sporadic behavior

Below is the code for my autocomplete. The problem is that it mostly works. Say I have a bunch of results that are like test1, test2, test3, etc. If I type "t" they popup, when I put an e in "te" they disappear. Then if I put in the "s" it narrows it down further. it's not always the second letter either. It just seems sporadic. Please help. I have confirmed the data coming back is solid, so it's nothing on the backend.

        //Server autocomplete
        $("#txtSearchServer").keyup(function (event) {
            $.ajax({
                url: 'edit/EditService.svc/SearchServers',
                type: 'GET',
                data: { 'term': $("#txtSearchServer").val() },
                dataType: 'json',
                success: function (data) {
                    var listServers = [];
                    $.map(data.d, function (item) {
                        ///working here to do server autocomplete!!!!!!!
                        listServers.push(item.ServerName);
                    开发者_如何学JAVA    $("#txtSearchServer").autocomplete({
                            source: listServers
                        });
                    });
                },
                error: function (a, b, c) {
                    $('.Toast').html('Error Retreiving Servers for autocomplete!');
                }
            });
        });


Looking at your code you are doing asp.net

SO this should work for you:

function AutocompleteJSONParse(data) {
    var rows = new Array();
    var rowData = null;
    for (var i = 0, dataLength = data.length; i < dataLength; i++) {
        rowData = data[i];
        rows[i] = {
            value: rowData.ServerName,
            label: rowData.ServerName
        };
    }
    return rows;
};
$("#txtSearchServer").autocomplete({
    source: function(request, response) {
        var pString = '{"term":"' + request.term + '"}';
        $.ajax({
            url: 'edit/EditService.svc/SearchServers',
            type: 'GET',
            dataType: "jsond",
            type: "POST",
            contentType: "application/json",
            converters: {
                "json jsond": function(msg) {
                    return msg.hasOwnProperty('d') ? msg.d : msg;
                }
            },
            data: pString,
            success: function(data) {
                var rows = AutocompleteJSONParse(data);
                response(rows);
            }
        });
    },
    error: function(a, b, c) {
        $('.Toast').html('Error Retreiving Servers for autocomplete!');
    },
    minLength: 2,
    delay: 1000
});


Here is the answer: https://gist.github.com/1140631 that should solve it.

EDIT: I was wondering also why are you dynamically prefetching results, can't you get full list right away? Or is it to much data?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜