svante
svante

Reputation: 11

Get right field from json with jQuery UI autocomplete

OKey not sure are this called fields or what but json result what I have is something like (from firebug):

0 Object { name="Kau, 49460 Stock", value="123", more...}


1 Object { name="Kau 315, 2344 Öre", value="123", more...}

I am using the jquery UI autocomplete plugin and evertyhin works fine but it always gets/shows the value field="123". I would nee it to show the "name" field. How can I do this? Tried to look from jQuery UI documentiation but didnt find (most propably because i dont know the right term for this what im looking)

Thx.

EDIT: I have a from with input field and then this js

$("input#searc").autocomplete({
    source: site_url + "ajax/get_address",
});

And it returns correct data but I dont know how to show correct stuff on autocompele from that json.

Upvotes: 1

Views: 1255

Answers (1)

jk.
jk.

Reputation: 14435

You need a label field. From the jquery ui autocomplete documentation:

The local data can be a simple Array of Strings, or it contains Objects for each item in the array, with either a label or value property or both. The label property is displayed in the suggestion menu. The value will be inserted into the input element after the user selected something from the menu. If just one property is specified, it will be used for both, eg. if you provide only value-properties, the value will also be used as the label.

label displays in the suggestion menu and value is placed in the value of the text box. One or the other will cause it to be used for both. When you specify both, they act like they are supposed to.

You would return something like this:

{ label="Kau 315, 2344 Öre", value="123", more...}

And then just use the label variable where you used the name variable.

EDIT

Take a look at this fiddle: http://jsfiddle.net/jensbits/AmvsX/3/

It takes the name field and sets it as the label

Here is a tutorial on Using jQuery Autocomplete When Remote Source JSON Does Not Contain ‘Label’ or ‘Value’ Fields

Upvotes: 1

Related Questions