【问题标题】:How to populate 2nd combobox(g:select) values on basis of 1st combobox(g:select)?如何根据第一个组合框(g:select)填充第二个组合框(g:select)值?
【发布时间】:2011-01-14 12:44:05
【问题描述】:

我正在尝试在 GSP 中选择第一个组合框 (g: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>

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

【问题讨论】:

    标签: grails grails-domain-class grails-controller


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案3】:

        这个问题和你的类似:Grails: Load data on one ComboBox depending on another。基本上和yogiebiz的回答是一样的,但是推荐一些不同的选择。

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-04-14
            • 2021-01-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多