i trying imeplement auto complete feature in web application. data loaded autocomplete features server. using jquery autocomplete
purpose - http://jqueryui.com/autocomplete/#remote-jsonp
below jquery code
$(document).ready(function() { $(function() { $("#adddrugtxt").autocomplete({ source: function(request, response) { $.ajax({ url: "autocomplete", type: "post", data: { term: request.term }, datatype: "json", success: function(data) { response(data); } }); }, minlength: 2, select: function( event, ui ) { log( "selected: " + ui.item.label ); } }); $( "#adddrugtxt" ).autocomplete( "option", "appendto", ".form-horizontal" ); }); }); function log( message ) { document.getelementbyid("brandtxt").value = message; alert("damn"); }
below java code provides data jquery code.
public class autocomplete extends httpservlet { private final list<string> druglist = new arraylist<string>(); int iduser=0; @override public void init() { } /** * processes requests both http <code>get</code> , <code>post</code> * methods. * * @param request servlet request * @param response servlet response * @throws servletexception if servlet-specific error occurs * @throws ioexception if i/o error occurs */ protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); iduser=integer.parseint(request.getsession(false).getattribute("userid").tostring()); try { druglist.clear(); drugnamestable table = new drugnamestable(); list<drugnamesbean> drugnames = table.getdrugnames(iduser); system.out.println("drug names taken"); // map real data json response.setcontenttype("application/json"); final string param = request.getparameter("term"); final list<autocompletedata> result = new arraylist<autocompletedata>(); (final drugnamesbean data: drugnames) { if (data.getdrugname().tolowercase().startswith(param.tolowercase())) { result.add(new autocompletedata(string.valueof(data.getiddrugname()), data.getdrugname())); } } response.getwriter().write(new gson().tojson(result)); } { out.close(); } } }
in above java code used bean called autocompletedata
. it's code below.
public class autocompletedata { private final string label; private final string value; public autocompletedata(string _label, string _value) { super(); this.label = _label; this.value = _value; } public final string getlabel() { return this.label; } public final string getvalue() { return this.value; } }
however want display autocompletedata._value
when auto complete suggestions coming up. unfortunately autocompletedata._label
.
of course autocompletedata._label
important me because need dynamically change id
of adddrugtxt
.
how can fix issue?
answer simply. had below.
autocompletedata { private final string label; private final string value; private string id; public autocompletedata(string _label, string _value, string _id) { super(); this.label = _label; this.value = _value; this.id = _id; } public final string getlabel() { return this.label; } public final string getvalue() { return this.value; } /** * @return id */ public string getid() { return id; } }
then in autocomplete.java
sent drug name
in both 2 parameters , id
in last parameter.
result.add(new autocompletedata(data.getdrugname(),data.getdrugname(),string.valueof(data.getiddrugname())));
in jquery
easy take other values this..
select: function( event, ui ) { log(ui.item.id ); }
Comments
Post a Comment