tag:blogger.com,1999:blog-1551941183578948330.post3554101253412143713..comments2016-06-28T06:46:56.967-07:00Comments on Alidad's Groovy and Grails Adventures: Enabling jQuery autocomplete in Grails using Tag and ServiceAlidadhttp://www.blogger.com/profile/13966190403425970088noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-1551941183578948330.post-13473360764156089972016-06-28T06:46:56.967-07:002016-06-28T06:46:56.967-07:00Awesome TagLib, thanks for the share.Awesome TagLib, thanks for the share.HACK TO LEARN.!!https://www.blogger.com/profile/00081241642531182572noreply@blogger.comtag:blogger.com,1999:blog-1551941183578948330.post-44832236700874370232013-08-23T13:02:45.409-07:002013-08-23T13:02:45.409-07:00Hi Alidad, I have made this into a more generic pl...Hi Alidad, I have made this into a more generic plugin :<br /><br />http://grails.org/plugin/ajaxdependancyselection<br /><br />Thanks for all your help, I have mentioned you<br /><br />Regards<br />Vahid<br /><br /><br />Anonymoushttps://www.blogger.com/profile/09891330379007480558noreply@blogger.comtag:blogger.com,1999:blog-1551941183578948330.post-68209746199124349782012-12-26T09:06:36.130-08:002012-12-26T09:06:36.130-08:00Awesome Thanks Alidad.
Warm regards
VahidAwesome Thanks Alidad.<br /> <br />Warm regards<br />Vahidvvvhttps://www.blogger.com/profile/02205379488428260603noreply@blogger.comtag:blogger.com,1999:blog-1551941183578948330.post-77713567412818006642012-12-25T04:48:06.057-08:002012-12-25T04:48:06.057-08:00running app is also here: countrycity.cloudfoundry...running app is also here: countrycity.cloudfoundry.comAlidadhttps://www.blogger.com/profile/13966190403425970088noreply@blogger.comtag:blogger.com,1999:blog-1551941183578948330.post-86554996718583860652012-12-25T04:25:52.777-08:002012-12-25T04:25:52.777-08:00Hey Vahid, take a look at here, if you had any que...Hey Vahid, take a look at here, if you had any question let me know. <br />https://github.com/alidadasb/CountryCityAutoComplete<br />Alidadhttps://www.blogger.com/profile/13966190403425970088noreply@blogger.comtag:blogger.com,1999:blog-1551941183578948330.post-67199596421376415972012-12-23T13:03:40.919-08:002012-12-23T13:03:40.919-08:00HI Alidad
https://github.com/vahidhedayati/grails...HI Alidad<br /><br />https://github.com/vahidhedayati/grailscountrycity<br /><br />I have managed to return both the countryid and countryname to the web page but as mentioned within the readme and aparent whilst running the code the countryid attribute does not appear to get updated,<br />I could not figure out how to return either the javascript variable countryValue to the autocomplete tag lib for city or a way of getting the countryid attribute to update.<br /><br />The hiddenField does update with no issues<br /><br />All of the code is within the git project above and should be ready to use once downloaded<br /><br />Regards<br />Vahid vvvhttps://www.blogger.com/profile/02205379488428260603noreply@blogger.comtag:blogger.com,1999:blog-1551941183578948330.post-3366598811736957342012-12-03T18:25:36.624-08:002012-12-03T18:25:36.624-08:00"Label" is just how autocomplete expects..."Label" is just how autocomplete expects the json data to be. before returning your list format it as label and values, like <br />//results = results.collect { [label:it."${params.collectField}"] }.unique()Alidadhttps://www.blogger.com/profile/13966190403425970088noreply@blogger.comtag:blogger.com,1999:blog-1551941183578948330.post-78417788402342421082012-12-03T14:25:14.734-08:002012-12-03T14:25:14.734-08:00Thanks Alidad and I did get the above to work, but...Thanks Alidad and I did get the above to work, but this is not what I was trying to achieve, basically the above so long as I provide a number to text field a field in the autocomplete can be set as that value, great to know how now but here is where I am stuck and perhaps you can shed more light on this:<br /><br />In the autoCompleteService I have written a new function:<br /><br /><br />def autocompleteTest (params) {<br /> def domainClass = grailsApplication.getDomainClass(params.domain).clazz<br /> def query = { <br /> or{<br /> ilike params.searchField, params.term + '%'<br /> <br /> }<br /> projections { // good to select only the required columns.<br /> property(params.collectField)<br /> property(params.searchField)<br /> } <br /> maxResults(Integer.parseInt(params.max,10))<br /> order(params.searchField, params.order)<br /> }<br /> def query1 = {<br /> or{<br /> ilike params.searchField, "%${params.term}%"<br /> }<br /> <br /> projections { // good to select only the required columns.##<br /> property(params.collectField)<br /> property(params.searchField)<br /> <br /> }<br /> maxResults(Integer.parseInt(params.max,10))<br /> order(params.searchField, params.order)<br /> }<br /> <br /> def results =domainClass.createCriteria().list(query)<br /> if (results.size()< 5){<br /> results = domainClass.createCriteria().list(query1)<br /> }<br /> <br /> def countrySelectList = []<br /> results.each {<br /> def countryMap = [:]<br /> //countryMap.put(params.collectField, it[0])<br /> <br /> // countryMap.put(params.searchField, it[1])<br /> countryMap.put('temp', it[0])<br /> countryMap.put('label', it[1])<br /> countrySelectList.add(countryMap)<br /> }<br /> //return countrySelectList<br /> //results = results.collect { [label:it."${params.collectField}"] }.unique()<br /> return countrySelectList as JSON<br /> }<br /><br /><br />The thing I have not understood is why return field mu be labelled 'label' and if I understood how it was reading this field in I could probably answer my own problem, because as you can see when the user selects a country the country id is also returned, initiall i tried calling them the field names so its dynamic according to what is set to collect and searchFields and although JSON returns both values only if set to label can I ee the auto complete function working.<br /><br />The question is how do i return the value set to temp currently to the second auto complete being country vvvhttps://www.blogger.com/profile/02205379488428260603noreply@blogger.comtag:blogger.com,1999:blog-1551941183578948330.post-59912134044341170532012-12-02T17:03:10.817-08:002012-12-02T17:03:10.817-08:00What you can do is when you change the value of yo...What you can do is when you change the value of your first box 'country id' put that id in the parameter of the second autocomplete (city) using a javascript code. look at this example, there is a javascript function that will be called upon the change in the value of textfield in your case this is your first autocomplete, so when you make any changes to country id this method will set the attribute of 'myval' to the value of the country id. from that point in the tag library you can get that value from the attrs.myval and do whatever you want to do with it. <br /><br />Hope this helps. <br /><br /><br /><br />$('#message').change(function() {<br /> val = $('#message').val();<br /> $("#demo").attr('myval',val)<br />});<br /><br /><br /> < g:textField id='message' name='message' value=""/ ><br /> < g:autoComplete id="demo" ... myval=""/ ><br /><br />Alidadhttps://www.blogger.com/profile/13966190403425970088noreply@blogger.comtag:blogger.com,1999:blog-1551941183578948330.post-28253887208567171742012-12-02T06:48:54.977-08:002012-12-02T06:48:54.977-08:00I have managed to make it work by physically defin...I have managed to make it work by physically defining country id:<br />--------------------------<br /><br /><label>Country:</label><br /><g:autoComplete id="id"<br /> action='autocompleteAction'<br /> controller='any'<br /> domain='rentspace.Country'<br /> searchField='name'<br /> value=''<br />/><br /> <br /><label>City:</label><br /><g:autoComplete id="countryId"<br /> countryid="2"<br /> action='autocompleteCityAction'<br /> controller='any'<br /> domain='rentspace.City'<br /> searchField='name'<br /> value=''<br />/><br /><br /><br />-----------------------------<br /> <br />class AnyController {<br /> def autoCompleteService<br /> //AutoCompleteService autoCompleteService<br /> def autocompleteAction= {<br /> render autoCompleteService.autocompleteAction(params)<br /> }<br /><br /> def autocompleteCityAction= {<br /> render autoCompleteService.autocompleteCityAction(params)<br /> }<br />}<br /><br />-----------------<br /><br />import grails.converters.JSON<br /><br />class AutoCompleteService {<br /> def grailsApplication<br />..<br /><br /> def autocompleteCityAction (params) { <br /> def domainClass = grailsApplication.getDomainClass(params.domain).clazz<br /> def results = domainClass.createCriteria().list {<br /> eq 'country.id', (Long.valueOf(params.countryid))<br /> ilike params.searchField, params.term + '%'<br /> maxResults(Integer.parseInt(params.max,10))<br /> order(params.searchField, params.order)<br /> }<br /> if (results.size()< 5){<br /> results = domainClass.createCriteria().list { <br /> eq 'country.id', (Long.valueOf(params.countryid))<br /> ilike params.searchField, "%${params.term}%"<br /> maxResults(Integer.parseInt(params.max,10))<br /> order(params.searchField, params.order)<br /> }<br /> }<br /> results = results.collect { [label:it."${params.collectField}"] }.unique()<br /> return results as JSON<br /> }<br /> <br />}<br /><br />---------------------------------<br /><br />class AutoCompleteTagLib {<br /> ...<br /> if (attrs.searchField == null)<br /> throwTagError("Tag [autoComplete] is missing required attribute [searchField]")<br /> <br /> def clazz = ""<br /> def name = ""<br /> def cid=""<br /> ....<br /> if (attrs.style) styles = " styles='${attrs.style}'"<br /> <br /> if (attrs.countryid) <br /> cid="&countryid="+attrs.countryid<br /> else<br /> cid=""<br /> <br /> ..<br /> out << "&searchField="+attrs.searchField<br /> out << "&max="+attrs.max<br /> out << "&order="+attrs.order<br /> out << ""+cid<br /> out << "&collectField="+attrs.collectField<br /> ...<br /><br />-----------------<br /><br />The only thing left now is how to get that defined countryid value set on 2nd drop box to be automatically picked from the first box..<br /><br />vvvhttps://www.blogger.com/profile/02205379488428260603noreply@blogger.comtag:blogger.com,1999:blog-1551941183578948330.post-50185037769728246332012-12-02T06:40:36.279-08:002012-12-02T06:40:36.279-08:00Thanks Alidad, an amazing and very clean way of do...Thanks Alidad, an amazing and very clean way of doing auto complete. I have got it working and I have one question. How easy would it be to make the second box of selection choice be based on value selected in first. Scenario country / city = once country selected to base city search on country idvvvhttps://www.blogger.com/profile/02205379488428260603noreply@blogger.com