0

Im trying to dynamically update jQuery UI source. I can do this fine with an array such as:

  var arrProducts = ['cheese' , 'bread' ,  'milk'];

But need to do it using an object. Before switching to using AJAX, this was working fine on first page load, passing in an array of objects from PHP into Twig:

 var arrProducts = [
                      {% for product in allproducts %}
                          {
                              title:  "{{ product.title }}",
                              url:  "{{ product.url }}",                                 
                              label: "{{ product.label }}"                              
                          },
                      {% endfor %}
              ];

So , how can I replicate this formatting within javascript? I've tried this:

                           var arrProducts = [];

                            $.each(data.products, function(index, product)
                            {
                                   prod['title'] = product.title;
                                   prod['url'] = product.url;
                                   prod['label'] = product.label;

                                   arrProducts.push(prod);
                            });                          

                           $('.searchBox' ).autocomplete( "option", "source", arrProducts );

But that produces nested objects , which then autocomplete seems not to be able to read properly .

1 Answer 1

1

The jQueryUI docs indicate that the source array should contain objects with both a label and value properties: http://api.jqueryui.com/autocomplete/#option-source; you don't have a value attribute on your objects.

I'm unclear why this would have worked previously, but here's a modification that worked for me: I changed your prod['title'] to prod['value'], and predeclared prod as a local variable so it wouldn't be autoinstantiated as a global.

Also note, I had to change the "options" call to use an anonymous object; for some reason, trying to invoke $('.searchBox').autocomplete("options", "source", arrProducts); caused an error in my testing fiddle.

HTML

​<input type="text" class="searchBox"/>​​​​​​​​​​​​​​​​​​​​​​​​​​​

JavaScript

var products = [
    {
      title: 'cheese',
      url: 'http://www.example.com',
      label: 'Swiss Cheese'
    },
    {
      title: 'bread',
      url: 'http://www.example.com',
      label: 'Wheat Bread'
    },
    {
      title: 'milk',
      url: 'http://www.example.com',
      label: '1% Milk'},
];

var arrProducts = [];

$.each(products, function(index, product) {
    var prod = {};
    prod['value'] = product.title;
    prod['url'] = product.url;
    prod['label'] = product.label;

    arrProducts.push(prod);
});

$('.searchBox').autocomplete({ source: arrProducts });
Sign up to request clarification or add additional context in comments.

1 Comment

Adding in the value was the issue. I then added in a title attribute too.

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.