2013-09-30 148 views
0

有沒有支持非select的數據綁定的標籤? 。我用它的一對多關係Grails:支持數據綁定的標籤?

似乎不切實際,如果你有大量的數據和滾動會更長

這將是真棒,如果它只是一個複選框列表,然後會有一個分頁

回答

0

g.select只是Grails默認值,但您可以自定義視圖並使用任何元素,因爲信息在那裏。例如:

class Parent { 
    String name 
    static hasMany = [childrens: Child] 
} 

class Child { 
    String name 
} 

class ParentController { 
    def create() { 
    Parent parentInstance = new Parent() 
    List<Children> childrens = Children.list() 
    [parentInstance : parentInstance, childrens: childrens] 
    } 

    def save() { 
    def childrens = params.list('childrens') 
    println childrens //will output all checkbox marked... 
    } 

} 

form.gsp 

<ul> 
<g:each in="${childrens}" var="child"> 
    <li><g:checkBox name="childrens" value="${child in parentInstance.childrens}" /></li> 
</g:each> 
</ul> 

一些關鍵點的位置:

  • 你可能重新考慮這一方式,如果你的hasMany側可以有很多的記錄;
  • 您的所有複選框必須具有相同的名稱才能被視爲列表;
  • 更新父項時,您需要在添加新項之前刪除關係;

相關主題:Grails - Simple hasMany Problem - Using CheckBoxes rather than HTML Select in create.gsp