asdtiang的博客 感谢blogjava提供的博客交流平台

grails 实现级联下拉列表

Posted on 2010-08-10 11:22 asdtiang 阅读(1639) 评论(1)  编辑  收藏 所属分类: grails study
不多说,可以实现多级,http://xihuyu2000.javaeye.com/blog/349722,这个地方可以实现两级,但我这里可以实现多级,直接上代码吧。

说明,手机充值有服务商,面值,和区域,现在数据库设计有点问题,不过能说明问题就行。

class TelDeltaPro {
    String id 
    String name//服务商
    String area//区域
    String price//面值
    String goodId
    
int sum//冲值总次数
    static mapping={
        id generator:
'uuid'
    }

}

页面主要代码:
<tr>
            
<td><span class="indexText2">服务商</span></td>
            
<td><g:select name="proName"
                from
="${com.likai.asdtiang.domain.platform.TelDeltaPro.list()}"
                optionKey
="name" optionValue="name"
                onchange
="${remoteFunction(controller:'telDeltaPro', action:'price', params:'\'name=\'+this.value', update:[success:'price'])}"></g:select>
            
</td>
        
</tr>
        
<tr>
            
<td><span class="indexText2">面值</span></td>
            
<td>
            
<div id="price"></div>
            
            
</td>
        
</tr>
        
<tr>
            
<td><span class="indexText2">地区</span></td>
            
<td>
            
<div id="area"></div>
            
</td>
        
</tr>
        
<tr>
            
<td></td>
            
<td><input type="submit" value="提交"></td>
        
</tr>

主要是onchange="${remoteFunction(controller:'telDeltaPro', action:'price', params:'\'name=\'+this.value', update:[success:'price'])}"这个地方,remoteFunction查下文档就可以了。

contorller主要代码如下:
def price={
        log.info params.name
+"name"
        session.putValue 
"telName",params.name
        def str
="""<select id="proName" name="price" onchange="${remoteFunction(controller:'telDeltaPro',
         action:
'area', params:'\'price=\'+this.value', update:[success:'area'])}">"""
        def li
=TelDeltaPro.findAllByName(session.telName)
        li.each{
            str
=str+"""<option value="${it.price}">${it.price}</option>"""
        }
        str
=str+"""</select>"""
        render(text:str,contentType:
"text/html",encoding:"UTF-8")
    }
    def area
={
        log.info params.price
+"    price"
        def str
="""<select  name="area" >"""
        def li
=TelDeltaPro.findAllWhere(name:session.telName, price:params.price)
        li.each{
            str
=str+"""<option value="${it.area}">${it.area}</option>"""
        }
        str
=str+"""</select>"""
        render(text:str,contentType:
"text/html",encoding:"UTF-8")
        
    }
代码应该很简单的,看下就懂了。
最后应该注意下:导入AJAX脚本,因为remoteFunction用到了AJAX
<g:javascript library="prototype" />


天苍苍,野茫茫,风吹草底见牛羊

Feedback

# re: grails 实现级联下拉列表  回复  更多评论   

2013-04-18 16:03 by 陈小龙
it.area 能获得值吗 好像获得不了

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 80, comments - 24, trackbacks - 0, articles - 32

Copyright © asdtiang

asdtiang的博客 PaidMailz
点击广告网赚A(每天4个广告,每个0.0025美元,一个搜索广告0.03美元)