简体   繁体   English

如何在旧版jQuery UI中使jQuery UI自动完成功能正常工作?

[英]How can I make jQuery UI Autocomplete work in an older version of jQuery UI?

I'm trying to add an autocomplete widget to my webpage, a personalized one. 我试图将自动完成的小部件添加到我的网页中,这是一种个性化的小部件。 After having several problems with my own configuration, I decided to try the official example directly in my webpage, doing everything I read there, and replacing this 在对自己的配置遇到一些问题之后,我决定直接在网页中尝试该官方示例 ,做所有我在该网页上阅读的内容,然后替换掉

.autocomplete( "instance" )._renderItem = function( ul, item ) {
  return $( "<li>" )
    .append( "<div>" + item.label + "<br>" + item.desc + "</div>" )
    .appendTo( ul );
};

with this 有了这个

.data("uiAutocomplete")._renderItem = function (ul, item) {
    return $("<li>")
    .append("<div>" + item.label + "<br>" + item.desc + "</div>")
    .appendTo(ul);
};

because I'm using jQuery UI 1.10.4 and I can't update it (my boss doesn't want me to do that). 因为我使用的是jQuery UI 1.10.4,但无法更新它(我的老板不希望我这样做)。 I couldn't make the example work, and I know that if I make it work, I won't have problems to adapt it to what it has to do. 我无法使该示例正常工作,并且我知道,如果我使该示例正常工作,那么我将不会遇到任何使它适应其需要执行的工作的问题。

Please, do you know how to make it work? 拜托,你知道如何使它工作吗? Thanks in advance. 提前致谢。

Ok, finally I gave up. 好吧,最后我放弃了。 The way I overcome the situation was adapting the DTO I was transforming to JSON and sending to the browser to the format that Autocomplete expects. 我克服这种情况的方法是将DTO转换为JSON,然后将其发送到浏览器,使其具有Autocomplete期望的格式。 The format has three fields: 格式包含三个字段:

{
    id: ...,
    label: ...,
    value: ...
}

After that, I just configured the autocomplete in this way: 之后,我以这种方式配置了自动完成功能:

$("#<%= nuevoPadrePem.ClientID %>").autocomplete({
    minLength: 6,
    source: function (request, response) {
        var term = request.term;
        var result = [];

        $.ajax({
            url: 'PMENUEVAVERS.aspx/GetResultadosBusqueda',
            data: JSON.stringify({ texto: term }),
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (dtos) {
                response(dtos.d);
            }
        });
    },
    select: function (event, ui) {
        var dto = ui.item;
        $("#<%= hdSelectedNuevoPadrePem.ClientID %>").val(dto.id);
        $("#<%= nuevoPadrePem.ClientID %>").css('color', '#00BE00');
    }
})

And it worked without problems. 它的工作没有问题。

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

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