Doubt with JSON in Jquery Autocomplete

Asked

Viewed 164 times

1

I have the following return JSON

{"cliente":[  
            {"id":"1","nome":"Fulano"},  
            {"id":"2","nome":"Ciclado"}  
]}

And I need popular man autocomplete,

$('#nome').autocomplete({  
      source: function(request, response){  
        $.ajax({  
            url:"http://192.168.254.103:12514/web-parbs/htdocs/json/clientes.jsp",  
            dataType:"json",  
            success: function(data){  
                response($.map(data, function(item){  
                    return {  
                        label: item.id,  
                        value: item.nome  
                    };  
                }))  
            }})  
      }  
    });

This was one of the solutions I researched, the problem is that the item returns a Vector, I’m having problems to go through in a way that stays label: the name of the client to be displayed in the field and value: the customer id. Can someone help me?

  • Why don’t you call the $.map passing by data.cliente instead of data? In fact the result of $.map this correct.

  • Man, that’s all it was, I did so much research and I didn’t see that mistake. Anyway, thanks for your attention!

1 answer

1


The function map translates an object (or list) into a list, where you must pass a function that does the transformation. It iterates over the properties and invokes its function to popular the new list.

The problem in your case is that you are passing an object with only one property instead of passing the customer list.

Just call the function $.map passing the data.cliente instead of data only.

That one Jsfiddle exemplifies the solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.