2012-07-17 98 views
0

我试图通过使用数据绑定的表单更新hasMany关系,但我在params中看到的数据看起来不正确。Grails选择标记结果

域类:

class CustomerSite { 

static hasMany = [dhrs:DeviceHistoryRecord]; 

static mapping = { 
    id generator:'sequence', params:[sequence:'cs_seq'] 
} 
    ... 
} 

编辑观点:

... 
<g:select name="dhrs" id="dhrs" 
from="${DeviceHistoryRecord.list()}" 
multiple="multiple" 
optionKey="id" 
value="${customerSiteInstance?.dhrs}"/> 

控制器:

def update = { 
    def customerSiteInstance = CustomerSite.get(params.id) 
    if(customerSiteInstance) { 

     customerSiteInstance.properties = params 

     String flashMsg = new String(); 

     flash.message = ""; 

     if(!customerSiteInstance.hasErrors() && customerSiteInstance.save()) { 
      flash.message += "Customer Site ${customerSiteInstance.toString()} updated" 
      redirect(action:show,id:customerSiteInstance.id) 
     } 
     else { 
      flash.message = flashMsg 
      render(view:'edit',model:[customerSiteInstance:customerSiteInstance]) 
     } 
    } 
    else { 
     flash.message = "Customer Site not found with id ${params.id}" 
     redirect(action:edit,id:params.id) 
    } 
} 

这给了我一个错误:

Error 200: org.springframework.beans.NotReadablePropertyException: Invalid property 'currentDeviceData' of bean class [java.lang.String]: Bean property 'currentDeviceData' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

在这条线的控制器代码:

if(!customerSiteInstance.hasErrors() && customerSiteInstance.save()) { 

这并没有任何意义给我,但我做了一些嘴硬它(着呢),并最终想通了,该g:select在流逝在一系列参数索引中。

视图输出的代码,我觉得看起来是正确的:

<select name="dhrs" id="dhrs" multiple="multiple" > 
    <option value="2421" >801122</option> 
    <option value="2422" >801123</option> 
    ... 

如果我选择在索引0和列表的索引1项,它不是通过一组“2421” &“2422 “就像我所期望的那样。它传递“0”&“1”。更糟糕的是,在我运行之后,当我回到编辑页面并再次运行它时,这次在索引8处选择一些东西,它将具有“8”...而且还包括“0”和“1” “从上次开始。

放眼望去这里了一下,我发现Selecting multiple values from select tag - Grails,其中有一些其他的想法,包括做出这样的改变:

<g:select name="dhrs.id" 
    from="${DeviceHistoryRecord.list()}" 
    multiple="multiple" 
    optionKey="id" 
    value="${customerSiteInstance?.dhrs*.id}"/> 

但是,这给了我丢失的方法错误,虽然它没有解决的问题具有索引而不是返回实际值。

任何有关这里发生了什么的想法以及我如何解决它?

顺便说一句,我正在运行Grails的1.0.4版本。是的,我很想升级它,但我不能。

谢谢!

回答

1

我花了更多时间在此,并得到了部分解决方案。 这是我结束了:

<g:select name="dhrsX.id" 
      from="${DeviceHistoryRecord.list()}" 
      multiple="multiple" 
      optionKey="id" 
      value="${customerSiteInstance?.dhrs*.id}"/> 

注意这dhrsX - 不dhrs。这样,我可以在将它们设置在CustomerSite对象中之前手动查找每个对象。唯一复杂的是,如果用户选择一个项目,dhrsX包含String;如果用户选择多个项目,则dhrsX包含String的列表。为了解决这个问题,我需要在尝试直接使用它们之前重新包装选择结果。

def update = { 
    def customerSiteInstance = CustomerSite.get(params.id) 
    if(customerSiteInstance) { 

     customerSiteInstance.properties = params 
     customerSiteInstance.dhrs.clear() 

     for(thisDHR in params.dhrsX) { 
      def value = thisDHR.getValue() 
      def ArrayList<String> list = new ArrayList<String>(); 
      if (value instanceof String) { 
       list.add(value) 
      } else { 
       for(oneValue in value) { 
        list.add(oneValue) 
       } 
      } 

      for(aDHR in list){ 
       DeviceHistoryRecord rec = DeviceHistoryRecord.get(aDHR) 
       if (rec != null) { 
        customerSiteInstance.addToDhrs(rec) 
       } else { 
        print(thisDHR + " NOT FOUND!") 
       } 
      } 
     } 
... 

现在单一选择和多重选择工作......但仍然存在一个小问题。尽管在添加新选项之前在dhrs上调用clear(),但之前的选择仍然存在。然而,我不认为这样做会很难修复。

0

在G:选择使用:

value="${customerSiteInstance?.dhrs?.id}" 

问号,而不是明星。这就是我的“多重选择”。

+0

当我看到您的帖子时,我已经走到了我发布的解决方案的路径。就我而言,如果我使用'?'或'*',似乎没有什么区别,尽管我看到他们有不同的目的。所以,任何一个工作。谢谢! – 2012-07-18 11:50:24

+0

您的最终解决方案看起来太复杂了,应该不是这样,我相信问题隐藏在其他地方。 – 2012-07-18 13:13:00