如何在Jquery-UI自动完成中显示图像

时间:2012-10-25 10:03:58

标签: jquery asp.net-mvc asp.net-mvc-3 jquery-ui jquery-ui-autocomplete

我正在使用jquery-ui自动完成并在自动完成功能内部进行ajax调用我正在调用我的控制器动作返回Json,每件事情都工作正常现在我想在建议中显示图像下拉,我我也在json中拍摄图片网址如何在建议中显示图片以及名称

JavaScript

 function log(message) {
            $("<div>").text(message).prependTo("#log");
            $("#log").scrollTop(0);
        }

        $("#search").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/Home/GetCompanyNames",
                    dataType: "jsonp",
                    data: "searchterm=" + request.term,
                    success: function (data) {
                        response($.map(data, function (item) {
                            alert(item.Value);
                            return {
                                label: item.Name,
                                value: item.Name
                            };
                        }));
                    }
                });
            },
            minLength: 2,
            select: function (event, ui) {
                alert(ui.item.Name);
            alert(ui.item.Value);
            alert(ui.item.LogoUrl);
            },
            open: function () {
                $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
            },
            close: function () {
                $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
            }
        });

    });

控制器中的操作:

    public JsonResult GetCompanyNames (string searchterm)
    {
        var companies = context.companyService.Query().Where(x => x.Name.Contains(searchterm)).ToList();
        var list = companies.Select(item => new SearchJsonModel
                                                {
                                                    LogoUrl = item.Logo != null || item.Logo != "" ? "<img  src='/Upload/" + item.Logo + "' />" : "<img src='/home/image?image=" + item.Name + "' />", Name = item.Name, Value = item.InternetName
                                                }).Select(model => (model)).ToList();
        return Json(list, JsonRequestBehavior.AllowGet);
    }

SearchJsonModel:

 public class SearchJsonModel
{
    public string Name { get; set; }
    public string Value { get; set; }
    public string LogoUrl { get; set; }
}

如果您需要更多细节并提前致谢,请询问我。

1 个答案:

答案 0 :(得分:5)

documentation说明了如何自定义输出的示例:

$('#search').autocomplete({
    source: function (request, response) {
        $.ajax({
            url: '/Home/GetCompanyNames',
            data: { searchterm: request.term },
            success: function (data) {
                response($.map(data, function (item) {
                    return {
                        label: item.Name,
                        value: item.Name,
                        logoUrl: item.LogoUrl
                    };
                }));
            }
        });
    },
    minLength: 2,
    select: function (event, ui) {

    },
    open: function () {
        $(this).removeClass('ui-corner-all').addClass('ui-corner-top');
    },
    close: function () {
        $(this).removeClass('ui-corner-top').addClass('ui-corner-all');
    }
})
.data('autocomplete')._renderItem = function(ul, item) {
    return $('<li>')
        .data('item.autocomplete', item)
        .append(item.label + '<img src="' + item.logoUrl + '" alt="" />')
        .appendTo(ul);
};