grails 实现级联下拉列表

不多说,可以实现多级, 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" />


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

你可能感兴趣的:(grails 实现级联下拉列表)