Grails一对多数据绑定与动态表单 [英] Grails one-to-many databinding with dynamic forms

查看:476
本文介绍了Grails一对多数据绑定与动态表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为了达到这个目的,我使用了这样的方法,使用了懒惰的列表,例如描述于: http://omarello.com/2010/08 / grails-one-to-many-dynamic-forms /



表单很好,所有必需的参数都在params-map中,
但绑定到列表不起作用。



我在过去两天阅读了很多关于这个主题的内容,并在stackoverflow上找到了很好的答案,
但是我认为这些方法只适用于较旧的Grails版本。



为了说明我的问题,一些代码:



House.groovy

  class House {

....删除属性以最小化示例


列表<地址> adresses = [] .withLazyDefault {new Adress()}
//列表地址= ListUtils.lazyList(new ArrayList<地址>,FactoryUtils.instantiateFactory(Adress.class));
static hasMany = [adresses:Adress]


// def getAdresses(){
// return LazyList.decorate(adresses,FactoryUtils.instantiateFactory(Adress。 class))
//}

静态映射= {
地址级联:全删除孤儿
}

动态表单模板 - >正确创建

 < div id =adress $ {i}class =adresses-divstyle =< g:if test =$ {hidden}> display:none;< / g:如果> margin-bottom:10px;> 
< g:hiddenField name ='adresses [$ {i}]。id'value ='$ {adresses?.id}'/>
< g:hiddenField name ='adresses [$ {i}]。deleted'value ='false'/>
< g:hiddenField name ='adresses [$ {i}]。neu'value =$ {adresses?.id == null?'true':'false'}/>

< g:textField name ='adresses [$ {i}]。street'value ='$ {adresses?.street}'/>
< g:textField name ='adresses [$ {i}]。land'value ='$ {adresses?.land}'/>

< span class =del-adresses>
< img src =$ {resource(dir:'images / skin',file:'database_delete.png')}
style =vertical-align:middle;/>
< / span>



HouseController - 编辑动作



  houseInstance.properties = params 

因此,正确创建表单模板,并在参数映射中存在输入值。



我的问题现在是从一个窗体创建的多个地址的数据绑定。
根据上述链接提供的示例项目,参数绑定应自动创建新的地址对象,并将其另存为子房。



调试我的应用程序我可以看到有正确的参数,但似乎列表无法创建一个新的地址对象。



'地址'列表包含一个空绑定后的值 - > [null]



如上所述,我为此问题尝试了一些解决方案,但无法解决。
可能在grails 2.3.5中不支持lazylist的方法,只适用于旧版本。



我希望有同样的问题,可以给我提示



提前感谢

解决方案

同样的问题。
参考这个: grails 2.3一使用动态表单的数据绑定



尝试删除参数中的 id:,如果它是新手机。用grails 2.3.11测试,并在我的项目中使用它。



这是我在 _phone.gsp

 < div id =phone $ {i}class =phone-div< g:if test = $ {hidden}> style =display:none;< / g:if>> 
< g:if test =$ {phone?.id!= null}>
< g:hiddenField name ='phones [$ {i}]。id'value ='$ {phone?.id}'/>
< / g:if>
...
< / div>

感谢@ hakuna1811


I'm using Grails 2.3.5 and try to persist multiple domains coming from a dynamic form.

To achieve this, I used the approach with lazy lists like described in: http://omarello.com/2010/08/grails-one-to-many-dynamic-forms/

The forms are genereated well and all the neccessary parameters are in the params-map, but the binding to the list does not work.

I read a lot about this topic the last two days and found good answers on stackoverflow, but I think these methods only work for older grails versions.

To illustrate my problem, some code:

House.groovy

class House {

....attributes removed to minimize example


List<Adress> adresses = [].withLazyDefault { new Adress() }
// List adresses = ListUtils.lazyList(new ArrayList<Adress>,FactoryUtils.instantiateFactory(Adress.class)); 
static hasMany = [adresses:Adress]


      //def getAdresses(){
      //return LazyList.decorate(adresses, FactoryUtils.instantiateFactory(Adress.class))
      //}   

static mapping = {
    adresses cascade:"all-delete-orphan"
}

Template for dynamic forms --> Are created correctly

<div id="adress${i}" class="adresses-div" style="<g:if test="${hidden}">display:none;</g:if>margin-bottom:10px; ">
<g:hiddenField name='adresses[${i}].id' value='${adresses?.id}'/>
<g:hiddenField name='adresses[${i}].deleted' value='false'/>
<g:hiddenField name='adresses[${i}].neu' value="${adresses?.id == null?'true':'false'}"/>

<g:textField name='adresses[${i}].street' value='${adresses?.street}' />
<g:textField name='adresses[${i}].land' value='${adresses?.land}' />    

<span class="del-adresses">
    <img src="${resource(dir:'images/skin', file:'database_delete.png')}" 
        style="vertical-align:middle;"/>
</span>

HouseController - edit action

houseInstance.properties = params

So, the form templates are created correctly and the the input values are existent in the parameter map.

My problem is now the databinding for multiple adresses created from one form. According to the example project provided by the link above, the parameter binding should automatically create new Adress-Objects and save them as childs for a house.

When debugging my application I can see that there are the right parameters but it seems that the list cannot create a new Adress-Object.

The 'adresses' list, contains a null value after binding--> [null]

Like mentioned above, I tried a few solutions for this problem, but couldn't resolve it. Probably the lazylist approach is not supported in grails 2.3.5 and only works for older version.

I hope someone had the same same problem and can give me a hint

Thanks in advance

解决方案

I have experience with the same problem. Refer to this: grails 2.3 one-to-many databinding with dynamic forms

Try to remove id: in params if it's a new phone. Tested with grails 2.3.11 and using it in my project.

Here is what I changed in _phone.gsp

<div id="phone${i}" class="phone-div" <g:if test="${hidden}">style="display:none;"</g:if>>
     <g:if test="${phone?.id != null}">
           <g:hiddenField name='phones[${i}].id' value='${phone?.id}'/>
     </g:if>
     ...
</div>

Thanks to @hakuna1811

这篇关于Grails一对多数据绑定与动态表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆