2012-07-17 100 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