Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    164
    Thanks
    75
    Thanked 1 Time in 1 Post

    Jquery autocomplete, html struggling to render

    Hi guys, slight problem. I have a basic autocomplete
    Code:
    $("input.my_selector").autocomplete({
         source: mySelectSource,
         minLength: 2
    })
    So this calls the following
    Code:
    var mySelectSource = function(request, response) {
            var onSuccess = function(data) {
                var tpl = '' +
                    '<p class="location-row">\n' +
                    '    <span class="location-code">{code}</span>\n' +
                    '    <span class="location-airport">{airport}</span>\n' +
                    '</p>\n' +
                    '<p class="location-georow">\n' +
                    '    <span class="location-city">{city}</span>\n' +
                    '    <span class="location-county">{county}</span>\n' +
                    '    <span class="location-country">{country}</span>\n' +
                    '</p>\n';
    
                var expr = new RegExp("(" + $.trim(request.term) + ")", "gi");
                var replacement = "<strong>$1</strong>";
    
                var replaceInTemplate = function(heystack, placeholder, value) {
                    if (typeof value == "string" && value.length > 0) {
                        value = value.replace(expr, replacement);
                    }
                    else {
                        value = "";
                    }
                    var reg = new RegExp("{" + placeholder + "}");
                    return heystack.replace(reg, value);
                }
    
                var fillTemplate = function(data) {
                    var output = new String(tpl);
                    output = replaceInTemplate(output, "code", data.code);
                    output = replaceInTemplate(output, "airport", data.name);
                    output = replaceInTemplate(output, "city", data.city);
                    output = replaceInTemplate(output, "county", data.county);
                    output = replaceInTemplate(output, "country", data.country);
                    return output;
                }
    
                var airports = [];
                for (var i = 0; i < data.length; ++i) {
                    var label = fillTemplate(data[i])
                    var value = data[i].code + ' - ' + data[i].name;
                    airports.push({
                        label: label,
                        value: value
                    });
                }
                response(airports);
            }
            var onFailed = function(jxhr, status) {
                response("Error! " + status);
            }
            $.ajax({
                url: '/booking/enquiryform_ajax.php?function=my_selector',
                type: "POST",
                data: {
                    term: request.term
                },
                dataType: "json",
                cache: false,
                success: onSuccess,
                fail: onFailed
            });
    }
    Now all the data gets injected without any problems. The problem is that when my list displays, it shows all the html code in the output. So it appears that the HTML is not being rendered.

    Is there anything in my code that may be causing this to happen?

    Thanks
    Last edited by nick2price; 07-17-2014 at 12:44 AM.

  • #2
    Regular Coder
    Join Date
    May 2009
    Posts
    164
    Thanks
    75
    Thanked 1 Time in 1 Post
    Seemed to have figured it out. New versions of autocomplete use the text function, unlike the old version which used the html function. So just got to add the following to the autocomplete
    Code:
    .data("ui-autocomplete")._renderItem = function (ul, item) {
            return $("<li></li>")
                .data("item.autocomplete", item)
                .append("<a>" + item.label + "</a>")
                .appendTo(ul);
        };

  • #3
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Facbook

    <a href="http://www.kickmovie14.in/" target="_blank">KICK MOVIE 2014</a>

  • #4
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •