javascript - Jquery UI autocomplete: both labels and values are behaving like values -


i'm working jquery ui's autocomplete.

the problem i'm having this: when work witih external data source, both label , value behave values.

in other words, when reference external data source, single label , value pair, both label , value appear in dropdown together, 2 choices.

in other words, both appear values, instead of appearing label , value.

when click on 1 of them, alert alert(ui.item.label); shows value, whether label, or value.

i can't see why label , value both behaving values, since i've marked in json source.

the problem not occur local data source, external one.

here's local code:

<script>     $(document).ready(function() {         $("#inputbox").autocomplete({             source: "sd/aa.php",             minlength: 3,             select: function(event, ui) {                 alert(ui.item.label);                 alert(ui.item.value);             }         });     }); </script>  <input id="inputbox" /> 

the why...maybe

when using jquery autocomplete, expects specific properties label , value when using complex objects.

for example, object has custom properties, id , description, autocomplete label , value property. if define label or value property, use property both label , value displayed.

the external service should return object looks so:

[{     //your custom properties     id: 78923,     description: 'a nice widget',     //below autocomplete specific properties     label: 'nice widget a',     value: '$19.99' }] 

possible solutions

  1. the first way work adjust service add label , value property json returned client.
  2. the second way, if don't have control on service, make ajax call instead , in success event function, can manually map results , extend objects new properties.

    ... source: function (request, response) {     $.ajax({         url: "/my/service?term=" + escape(request.term),         type: "get",         contenttype: "application/json",         datatype: "json",         success: function (data) {             var mappedresults = $.map(data, function (item) {                 //extend service data label , value property autocomplete uses                 return $.extend(item, { label: item.description, value: item.id });             });              response(mappedresults);         }     }); } ... 

this useful if wish use other values provided service else. these can accessed through of events of autocomplete function in form of ui.item.[yourproperty]. if gave callback select event of autocomplete widget, access description via ui.item.description.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -