0

I am trying to use a JSON output from the server as the source for my autocomplete function. I read the Autocomplete documentation and it does say that Array of Objects is accepted as a source type. Can someone please let me know where am I going wrong with this?

            jq( document ).ready(function() {
            jq("body").css({overflow:'hidden'});

            jq.getJSON("<?php echo Mage::getBaseUrl() . "setsession/index/getarea"; ?>",
             function(data) {

                jq( "#autocomplete-1" ).autocomplete({
                source: data,
                select: function(event, ui) {
                    alert(ui.item.area_id);
                    jq("#splash_area").val(ui.item.area_id);
                    return false;
                }
                });
            }
            );              
            });

This is what I am getting back from the server (JSON encoded):

[{"area_id":"1","area_name":"DLF Phase 1"},{"area_id":"2","area_name":"DLF Phase 2"}]
2
  • you mean your autocomplete dropdown doesn't show anything ? Commented May 19, 2016 at 3:41
  • thats right. The autocomplete is not showing the dropdown. When I supply a single dimesional array then it works perfectly. Commented May 19, 2016 at 3:42

1 Answer 1

1

From the documentation it states an An array of objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ].

http://api.jqueryui.com/autocomplete/#option-source

Your objects are not defined this way. So for your example something like

[{value:"1",label:"DLF Phase 1"},{value:"2",label:"DLF Phase 2"}]

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

1 Comment

Worked. I never suspected them to take away the independence of naming the data type!

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.