jquery 1.8.2 autocomplete + .live() + .create() , not working together

705 views Asked by At

I am working inside an asp.net MVC web application, and I am using jQuery version 1.8.2 & jQuery-ui 1.8.24. I have the following autocomplete code:-

$("#ServerTag,#SDTag,#FirewallTag,#RouterTag,#SwitchTag").live("focus.autocomplete", null, function () {
        var URL ="@Url.Content("~/Server/AutoComplete")";

        $(this).autocomplete({
            minLength: 1, delay: 1000,
            source: function (request, response) {
                $.ajax({
                    url: URL,
                    cacheLength:1,
                dataType: "json",

                data: {
                    term: request.term,
                    rackid: "@Model.Rack.TMSRackID.ToString()",
                    },
                success: function (data) {
                    response(data);
                },
                select: function (event, ui) {
                    return false;
                    },
                create: function () {
                    $(this).data("autocomplete")._renderItem = function (ul, item) {

                        return $('<li>').data('item.autocomplete', item).append('<a>' + '<b>' + item.label + '</b><br>' + '<span style="color: #737373;font-size: 12px;font-weight: 300;line-height: 16px;font-family: Helvetica,Arial,sans-serif;white-space: owrap;">' + item.resourcename + ' | ' + item.customername + ' | ' + item.sitename + '<hr style="padding: 0px; margin: 0px;">' + '</span></a>')
                                        .appendTo(ul);
                    };
                }
            });
        },

        });
    });

And the following action method, which return the json:-

public ActionResult AutoComplete(string term,int? rackid)
        {

            var query = from techItems in ad
                        join resourcesItems in resources
                        on techItems.Technology.IT360ID.Value equals resourcesItems.RESOURCEID // join based on db2ID
                        orderby techItems.Technology.PartialTag
                        select new {  label = techItems.Technology.Tag.ToString(), customername = resourcesItems.ResourceLocation.SiteDefinition.AccountDefinition.ORG_NAME.ToString(), resourcename = resourcesItems.RESOURCENAME.ToString(), sitename = resourcesItems.ResourceLocation.SiteDefinition.SDOrganization.NAME };

            return Json(query, JsonRequestBehavior.AllowGet);


        }

The problem I am facing is that the Autocomplete results will contain only a single label and not the concatenated string defined inside the create section .

Can anyone advice ?, seems that my .create does not have any effect.

Thanks

2

There are 2 answers

0
Arun P Johny On BEST ANSWER

One possible problem I can see is in your configuration where the create and select options are passed to the ajax settings instead of autocomplete.. so try

$("#ServerTag,#SDTag,#FirewallTag,#RouterTag,#SwitchTag").live("focus.autocomplete", null, function () {
    var URL = "@Url.Content("~ / Server / AutoComplete ")";

    $(this).autocomplete({
        minLength: 1,
        delay: 1000,
        source: function (request, response) {
            $.ajax({
                url: URL,
                cacheLength: 1,
                dataType: "json",

                data: {
                    term: request.term,
                    rackid: "@Model.Rack.TMSRackID.ToString()",
                },
                success: function (data) {
                    response(data);
                }
            });
        },
        select: function (event, ui) {
            return false;
        },
        create: function () {}
    });

    //this should happen outside the ajax callback
    $(this).data("autocomplete")._renderItem = function (ul, item) {
        return $('<li>').data('item.autocomplete', item).append('<a>' + '<b>' + item.label + '</b><br>' + '<span style="color: #737373;font-size: 12px;font-weight: 300;line-height: 16px;font-family: Helvetica,Arial,sans-serif;white-space: owrap;">' + item.resourcename + ' | ' + item.customername + ' | ' + item.sitename + '<hr style="padding: 0px; margin: 0px;">' + '</span></a>')
            .appendTo(ul);
    };
});