我一直在关注Alidad博客的指南,以便在grails中启用jquery自动完成功能:
http://alidadasb.blogspot.co.uk/2011/12/enabling-jquery-autocomplete-with.html
Country.groovy
package rentspace
class Country {
String name
static hasMany = [cities:City]
}
City.groovy
package rentspace
class City {
//static belongsTo = [country:Country]
static belogsTo = Country
Country country
String name
static constraints = {}
}
GSP页面:
<g:autoComplete id="countrySearch"
action='autocompleteCountry'
controller='any'
domain='rentspace.Country'
searchField='name'
collectField='id'
value=''
/>
<g:textField id="hiddenState" name="hiddenState" value=""/>
<label>City:</label>
<g:autoComplete name="citySearch" id="citySearch"
cid=""
action='autocompleteCityAction'
controller='any'
domain='rentspace.City'
searchField='name'
value=''
/>
AutoCompleteTagLib.groovy
package rentspace
class AutoCompleteTagLib {
..
if (attrs.style) styles = " styles='${attrs.style}'"
if (attrs.cid)
cid="&cid="+attrs.cid
else
cid=""
.......
out << "&order="+attrs.order
out << ""+cid
out << "&collectField="+attrs.collectField
out << "',select: function(event, ui) {"
out << " \$('#hiddenState').val(ui.item.id);},"
//out << " \$('#citySearch').attr('cid',ui.item.id);},"
out <<" search: function() {"
out << "\$('#hiddenState').val('');"
//out << "\$('#citySearch').attr('cid','');"
out <<"}"
out << ", dataType: 'json'"
out << "});"
out << " });"
out << "</script>"
}
def autoCompleteHeader = {
out << "<style>"
out << ".ui-autocomplete-loading"
out << " { background: white url(${resource(dir:'images',file:'ajax-loader.gif')}) right center no-repeat }"
out << " </style>"
}
}
我的问题与jquery返回的双值有关,如果我将值返回到隐藏或文本字段框,它似乎确实有效。我要做的是将国家/地区ID返回到第二个自动完成框,即citySearch。 因此,一旦用户自动完成国家/地区,国家/地区ID将返回为cid ='1'或者id为citySearch自动完成框的cid属性。
在标记lib中有一个注释掉的段,它是成功返回或更新hiddenState字段的值的地方,但是无论是什么尝试更新我继续失败的cid值?
//out << " \$('#citySearch').attr('cid',ui.item.id);},"
有没有人成功做过这样的事情?
E2A:
https://github.com/vahidhedayati/grailscountrycity
可以从上面的链接下载项目,在自述文件中有关于该问题的更多信息
答案 0 :(得分:1)
问题在这里由Alidad解决: {countryid:\ $('#hiddenField')。val()}
https://github.com/alidadasb/CountryCityAutoComplete
out << "\$('#" + attrs.id+"').autocomplete({ "
out << " source: "
out << " function(request, response) { "
out << " \$.getJSON(' "
out << createLink(link)
out << "?"
out << "term=' + request.term + '"
out << "&domain="+ attrs.domain
out << "&searchField="+attrs.searchField
out << "&max="+attrs.max
out << "&order="+attrs.order
out << "&collectField="+attrs.collectField
out << "', { countryid: \$('#hiddenField').val() }, "
out << " response); } "
out << ", dataType: 'json'"
out << "});});"
out << "</script>"
可以在这里看到工作版本: http://countrycity.cloudfoundry.com