2013-06-20 45 views
0

我正在使用grails 2.1.0。我的查看页面中有一个自动填充字段。对于自动填充字段,我使用了richui插件。结果显示在框中。现在我想使用该字段的值的id而不是字符串。但我无法获得/设置ID。我不知道如何从该领域获得身份证。任何人都可以请帮我。这里是我下面的代码:grails从自动填充字段的值中获取ID

我的视图页面>>>

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

我的自动完成控制器动作>>>

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) 
       } 
      } 
     } 
    } 
} 

我的愿望的行动,我想用ID工作> >>

def doSomething(){ 
    def val = "Country Id is -- > " + params.countryName 
    render val 
} 

回答

1

嘿我已经解决了这个问题,只需调用一个javascript函数并添加一个隐藏字段。如下:

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

我的隐藏字段>>> head部分>>>

<script type="text/javascript"> 
    function callCountry(countryId){ 
     document.getElementById('countryId').value = countryId; 
    } 
</script> 
+0

我正在使用richui自动完成插件。我想获得在自动填充字段中选择的项目的值。假设如果我在这个控件中选择了“Stack”,那么我想要得到“Stack”的值。任何想法如何得到这个。就像你想获得身份证,但我想在这里获得价值。 –

+0

太棒了!非常感谢你,它也适用于我(Grails 2.3.5),但由于某种原因,当我使用richui时出现了1个换行符,这是否发生在你身上? – Alberici

0

通过插件文档,您可以访问内的countries.id(例如id)

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

配置这样的动作(ID为)将获得一旦用户选择从自动完成列表中的项目VS不必明确点击提交调用。

+0

感谢您的回复,我明白

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

我的js函数。 onItemSelect工作正常。但是我想将id值保存在一个变量中并在按钮单击时使用它。请告诉我如何去做。 –