2011-11-30 145 views
1

我將值保存在兩個表中asset和asset_history.On創建資產我將值保存在資產表中如果有任何更新,我想讓它存儲在資產和asset_history上的id基礎上。現在我想在編輯頁面中獲取兩個表值,以獲取asset_history我使用sql查詢來獲取asset_history.all中的值正在運行良好,但它正在進入數組列表值(所有更新列表都顯示在單行中)。我在編輯頁面的updata值應該保存並顯示在asset_history的不同行中。因爲我用於循環,但它沒有得到值。在grails中使用相同的ID更新許多項目

資產表,我有這些領域: -

 id 
     asset_title 
     asset_description 
     client_id 
     comment 
     status 
etc... 

在asset_history場: -

id 
comment 
update_on 
update_by 
status 

如果資產field.The更新列表中的任何更新應在資產和asset_history.I保存已經使用查詢來更新(如下所示)。但它正在asset_history表中獲取arraylist。

編輯操作

def dataSource 
def edit={ 
    def assetInstance = Asset.get(params.id) 
     if (!assetInstance) { 

      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'asset.label', default: 'Asset'), params.id])}" 
      redirect(action: "list") 
     } 
     else { Sql sql = new Sql(dataSource) 
      def result = sql.rows("SELECT * from asset_history where id ='"+params.id+"' ") 
      //def n=result 

      /* def arr = (String[])result 
         for(i in 0 .. result.size()-1) 
         { 

      return [assetInstance: assetInstance,result:i] 
         }*/ 
        return [assetInstance: assetInstance,result: result] 
     } 

    } 

在edit.gsp

<tbody> 

         <tr> 


          <td>${result.id}</br></td> 
          <td>${result.comment}</br></td> 

          <td>${result.update_on}</br></td> 
          <td>${result.update_time}</br></td> 
           <td>${result.update_by}</br></td> 

         </tr> 

        </tbody> 

在asset_history表中的值越來越在ArrayList和顯示單row.But我想表明它在單獨一行更新列表,當我每次更新。我用for循環這個,但它不工作。請指導我解決這個問題。

+1

爲了得到更好的答案,你可能會對這篇文章感興趣:http://meta.stackexchange.com/a/81648。嘗試寫出較短的問題,並努力寫出好的英語。 – Antoine

回答

2

你的問題是,你只能從Groovy中的一個函數中返回一個值(以及我知道的所有其他語言)。因此,for循環中的第一次迭代返回並且以下迭代(您期望返回更多實例的地方)不會執行。

您必須返回一個列表,並在您的GSP文件使用<g:each>標籤(這是相當於一個for循環GSP):

def edit = { 
    def assetInstance = Asset.get(params.id) 
    if (!assetInstance) { 
    flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'asset.label', default: 'Asset'), params.id])}" 
    redirect(action: "list") 
    } 
    Sql sql = new Sql(dataSource) 
    def results = sql.rows("SELECT * from asset_history where id ='" + params.id + "' ") 
    return [results: results] 
} 

而且你的GSP:

<tbody> 
    <g:each var="result" in="${result}"> 
    <tr> 
     <td>${result.id}</br></td> 
     <td>${result.comment}</br></td> 

     <td>${result.update_on}</br></td> 
     <td>${result.update_time}</br></td> 
     <td>${result.update_by}</br></td> 
    </tr> 
    </g:each> 
</tbody> 

由於一個獎金,這裏有一些提示,將使您的應用程序看起來更好:

  • 如果您不使用不返回值他們在您的GSP中,如assertInstance這裏
  • 如果您的斷言歷史記錄是Grails域對象,請避免使用SQL並更願意依賴Grails內置訪問方法(findBy)或Hibernate標準。這會給你Grails對象而不是raw SQL行
  • 爲了可讀性正確縮進你的代碼。有可能你的IDE有一個可以爲你做的功能。
相關問題