1

我正在尝试在 GSPg:select中选择第一个组合框 () 值时加载第二个组合框 () 值。g:select

领域类:

class Person {    
   String name
   static hasMany = [telephones:Telephone]
}

class Telephone {    
   String tNumber
   Person person

   static belongsTo = [person:Person]

}

普惠制:

<td>
<g:select id="person" name="selectedPersonId" from="${Person.list(sort:name, order:asc)}" value="name" optionValue="name" optionKey="id" noSelection="['0':'--Select--']" />
</td>
<td>
<g:select id="telephone" name="selectedTelephoneId" from ="${person.telephones}" value="tNumber" optionValue="tNumber" optionKey="id" noSelection="['0','--Select--']"/>
</td>

我怎样才能正确地做到这一点?

4

4 回答 4

2

页面渲染时不要填充第二个组合框中的项目,当第一个组合框中的值发生变化时填充它。

<td>
<g:select id="person" name="selectedPersonId" from="${Person.list(sort:name, order:asc)}" value="name" optionValue="name" optionKey="id" noSelection="['0':'--Select--']" />
</td>
<td>
<g:select id="telephone" name="selectedTelephoneId" from ="${[]}" value="tNumber" optionValue="tNumber" optionKey="id" noSelection="['0','--Select--']"/>
</td>

在第一个组合框(您可以使用 jquery 或纯 Javascript)上添加 onchange 事件,该事件将根据所选人员填充电话数据群。在这里,您可以使用对服务器的 ajax 调用来执行操作,例如:

def getTelephones = {
    def telephoneInstanceList = Telephone.findAllByPerson(Person.get(params.personId))
    def telephones = telephoneInstanceList.collect {[id: it.id, phone: it.tNumber]}
    render telephones as JSON
}
于 2011-01-14T16:09:15.720 回答
1

首先不要使用表格来格式化使用 div。在第一个 g:select 中使用 remoteFunction 将当前选择作为参数传递,调用看起来像

"${remoteFunction(action: 'methodName', update: 'DivToUpdate', params: '\'id=\'+this.value')}"

现在,在控制器上的方法中,您将渲染调用到包含第二个 g:select 的模板。这个 g:select 可以使用来自控制器的字段值或来自参数的信息。希望这可以帮助

于 2013-02-08T16:38:44.690 回答
0

这个问题与您的问题相似:Grails: Load data on one ComboBox取决于另一个。基本上和yogiebiz的回答是一样的,但是推荐一些不同的选择。

于 2011-01-16T01:03:04.737 回答
0

更新:Grails 已经在此发布了一个 wiki 页面: https ://grails.org/AJAX-Driven+SELECTs+in+GSP

于 2014-07-21T18:13:53.413 回答