1

我一直在遵循 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] 
    }

城市.groovy

package rentspace

class City {
    //static belongsTo = [country:Country]
    static belogsTo = Country
    Country  country
    String name
    static constraints = {}
}

普惠制页面:

<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 属性。

在标签库中,有一个段被注释掉,它成功地返回或更新 hiddenState 字段的值,但无论我如何尝试更新 cid 值都失败了?

        //out << "  \$('#citySearch').attr('cid',ui.item.id);},"

有没有人成功地做这样的事情?

E2A:

https://github.com/vahidhedayati/grailscountrycity

项目可以从上面的链接下载,自述文件中有更多关于问题的信息

4

1 回答 1

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

于 2012-12-27T12:24:37.527 回答