Finally, I did it. Thanks to all my friends, and special thanks to Mr. https://stackoverflow.com/users/87015/salman-a because of his code, I was able to solve it correctly. finally, my code looks the way I use groovy grails, I hope this helps someone out there .. Thanks a lot
html code looks like this on my gsp page
<input id="populate-dropdown" name="nameofClient" type="text"> <input id="wilhaveid" name="idofclient" type="text">
script Function is the same as on my gsp page
<script> $( "#populate-dropdown").on('input', function() { $.ajax({ url:'autoCOmp', data: {inputField: $("#populate-dropdown").val()}, success: function(resp){ $('#populate-dropdown').autocomplete({ source:resp, select: function (event, ui) { $("#populate-dropdown").val(ui.item.label); $("#wilhaveid").val(ui.item.value); return false; } }) } }); }); </script>
And my controller code is like this
def autoCOmp(){ println(params) def c = Client.createCriteria() def results = c.list { like("nameOfClient", params.inputField+"%") } def itemList = [] results.each{ itemList << [value:it.id,label:it.nameOfClient] } println(itemList) render itemList as JSON }
One more thing that I did not set in the id field hidden, because at first I checked that I get the exact identifier, you can keep it hidden, just put type = hidden instead of the text for the second input element in html
Thank!
Aadil Masavir Feb 27 '17 at 14:30 2017-02-27 14:30
source share