12

If I have a array of objects like

var arrLinks = [
    { key: 1, url: "http://google.com" },
    { key: 2, url: "http://yahoo.com", title: "Yahoo" },
    { key: 2, url: "http://microsoft.com" }
];

Can I use it as the source for autocomplete? I tried implementing in following http://jqueryui.com/demos/autocomplete/#custom-data but didn't get it http://jsfiddle.net/mvNNj/

1 Answer 1

15

You need to:

1 - Actually include jQuery + UI on your test page.

2 - Incorporate the use of 'labels' which the Autocompleter uses to find matches:

$(function() {
    var arrLinks = [
        {
        key: 1,
        url: "http://google.com",
        label: 'google'},
    {
        key: 2,
        url: "http://yahoo.com",
        title: "Yahoo",
        label: 'yahoo'},
    {
        key: 2,
        url: "http://microsoft.com",
        label: 'microsoft'}
    ];
    $("input[name=url]").autocomplete({
        source: arrLinks
    }).data("autocomplete")._renderItem = function(ul, item) {
        return $("<li>").data("item.autocomplete", item).append("<a>" + item.url + "</a>").appendTo(ul);
    };
});

Your test page, working.

Sign up to request clarification or add additional context in comments.

4 Comments

I see that I still need to add a label to my array?
@naveen, yes I usually use the Add Resources accordian and paste in the google cdn css link imgur.com/K6bha.jpg
you mean adding the jQuery UI CSS into jsFiddle? See jsfiddle.net/FHEYt
I think with the latest version of jquery-ui you need to use .data("ui-autocomplete").renderItem = ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.