0

I am using grails 2.1.0. I have a autocomplete field in my view page. For autocomplete field I have used richui plugin. And the result is shown in the box. Now I want to work with the id of the value of that field not with the string. But I am failing to get / set the id. I have no idea how to get the id from that field. Can anyone please help me on this please. Here is my source code below :

my view page >>>

    <g:form controller="autocomplete" action="doSomething">
      <richui:autoComplete id="countryName" name="countryName" action="${createLinkTo('dir': 'autocomplete/searchCountry')}" />
    <br/>
    <g:submitButton name="doSomething" />
</g:form>

my controller action for autocomplete >>>

def searchCountry = { 
    def country = Country.findAllByNameLike("${params.query}%")        
    //Create XML response 
    render(contentType: "text/xml") {
        results() {
            country.each { countries -> 
                result(){
                    name(countries.name) 
                    id(countries.id)
                }
            } 
        } 
    } 
}

my desire action where I want to work with id >>>

def doSomething(){
    def val = "Country Id is -- > " + params.countryName
    render val
}
Sumon Bappi
  • 1,937
  • 8
  • 38
  • 82

2 Answers2

1

Hey I have solve the issue, just call a javascript function and add a hidden field. as follows :

<richui:autoComplete id="countryName" name="countryName" action="${createLinkTo('dir': 'autocomplete/searchCountry')}" 
                       onItemSelect="callCountry(id)"/>

my hidden field >>>

<g:hiddenField id ="countryId" name ="countryId" value=""/>

my js function in the head section >>>

    <script type="text/javascript">
    function callCountry(countryId){
      document.getElementById('countryId').value = countryId;
  }
</script>
Sumon Bappi
  • 1,937
  • 8
  • 38
  • 82
  • I am using richui auto-complete plug-in. I want to get value of item selected in autocomplete field. Suppose If I select "Stack" in this control, so I want to get value of "Stack". Any idea how to get this. Like you wanted to get id but I want to get value here. – Vieenay Siingh Aug 26 '13 at 10:29
  • Great! Thank you very much, it worked for me also (Grails 2.3.5), but for some reason I get 1 line break when I use the richui, did it happen to you? – Alberici May 13 '14 at 18:04
0

Per the plugin documentation, you can access the countries.id (e.g. id) within onItemSelect

<richui:autoComplete id="countryName" name="countryName" 
  action="${createLinkTo('dir': 'autocomplete/searchCountry')}" 
  onItemSelect="document.location.href='${createLinkTo(dir: 'doSomething')}/' + id;" 
/>

Configured like this, the action (with id) will get called as soon as the user selects an item from the autocomplete list vs having to explicitly click on submit.

ikumen
  • 11,275
  • 4
  • 41
  • 41
  • thanks for your reply, I understand. onItemSelect works fine. But I want to keep the id value in a variable and use it on button click.Can you tell me how to do it please. – Sumon Bappi Jun 23 '13 at 05:09