json - JQuery Autocomplete array of objects -
i'm using spring mvc , try send array of objects controller use int jquery autocomplete. have managed array list<string>
. can't work array of objects. need label , value different strings in jquery autocomplete.
i want following, according autocomplete jquery ui:
[ { label: "label1", value: "value1" }, ... ]
i have managed working within js
var obj = jquery.parsejson('[{"label":"label1","value":"value1"},{"label":"label2","value":"value2"}]');
but when create in controller can't send string autocomplete (due success in autocomplete not trigger , response(data) not run). try creating below.
@requestmapping(params = {"type=itemtype"}) public @responsebody list<dataobject> returnitemtype(@requestparam("itemtype") string itemtype) { list<dataobject> objlist = new arraylist<dataobject>(); objlist.add(new dataobject("label1", "value1")); objlist.add(new dataobject("label2", "value2")); return objlist; } private static class dataobject { private string label; private string value; public dataobject(string label, string value) { this.label = label; this.value = value; } public string tostring() { return "label = " +label+ ", value = " +value; } }
if add gson return gson.tojson(objlist);
the json looks in firebug. success in autocomplete not run , response(data)
not run either cause of string gson.
the js autocomplete
$(function() { $("#itemtype").autocomplete({ source: function(request, response) { $.ajax({ url: "autocomplete.do", datatype: "json", cache: true, contenttype: "application/json; charset=utf-8", data: { "type" : "itemtype", "itemtype" : $("#itemtype").val() }, success: function(data) { response(data); } }); }, select: function(event, ui) { $("#itemtype").val(ui.item.value); $("#orderitemfilterform").submit(); }, minlength: 0 }); $('#itemtype').click(function(){ $(this).val(""); $(this).data("autocomplete").search($(this).val()); }); });
any suggestions?
in web application,i used json data javascript below:
success:function(data){ data=eval("("+data+")"); console.log(data.label+"\t"+data.value); }
Comments
Post a Comment