【问题标题】:Updating many item using same id in grails在grails中使用相同的ID更新许多项目
【发布时间】:2011-11-30 06:15:51
【问题描述】:

我将值保存在两个表资产和资产历史中。在创建资产时,如果有任何更新,我将在资产表中保存值,以便将其存储在资产和资产历史中以 id 为基础。现在我想在编辑页面中获取两个表值以获取asset_history 我使用sql查询来获取asset_history中的值。所有工作都很好,但它出现在数组列表值中(所有更新列表都显示在单行中)。当我更新了编辑页面中的值,它应该保存并显示在asset_history的不同行中。为此,我使用了 for 循环,但它没有获取值。

资产表我有这些字段:-

     id
      asset_title
      asset_description
      client_id
      comment
      status
etc...

在asset_history 字段中:-

id
comment
update_on
update_by
status

如果资产字段有任何更新。更新列表应该保存在资产和资产历史中。我使用查询来更新(如下所示)。但它正在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中并在单行中显示更新列表。但我想在单独的行中显示它,当我每次更新时。我为此使用了for循环,但它不起作用。请指导我来解决这个问题。

【问题讨论】:

  • 为了得到更好的答案,您可能对这个帖子感兴趣:meta.stackexchange.com/a/81648。尝试写较短的问题,并努力写出好的英语。

标签: grails groovy grails-controller


【解决方案1】:

您的问题是您只能从 Groovy 中的函数返回一个值(以及我知道的所有其他语言)。因此,您的 for 循环中的第一次迭代将返回,而随后的迭代(您希望返回更多实例的地方)不会执行。

您必须返回一个列表并在您的 GSP 文件中使用 &lt;g:each&gt; 标记(相当于 GSP 中的 for 循环):

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]
}

还有你的普惠制:

<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 Criteria。这将为您提供 Grails 对象而不是 raw SQL 行
  • 正确缩进代码以提高可读性。很有可能您的 IDE 具有可以为您执行此操作的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 2023-03-15
    • 2015-06-24
    • 2020-05-04
    • 1970-01-01
    • 2013-09-29
    相关资源
    最近更新 更多