1

我在两个表asset和asset_history中保存值。在创建资产时,如果有任何更新我希望将其存储在asset和asset_history中,我将值保存在资产表中。现在我想在编辑页面中获取两个表值以获取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循环,但它不起作用。请指导我解决这个问题。

4

1 回答 1

2

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

您必须返回一个列表并<g:each>在您的 GSP 文件中使用一个标签(这相当于 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>

As a bonus, here are a few tips that will make your application look better:

  • Don't return values if you don't use them in your GSP, like assertInstance here
  • Avoid using SQL and prefer relying on Grails built-in access methods (findBy) or Hibernate Criteria if your assert history are Grails domain objects. This will give you Grails objects backs instead of raw SQL rows
  • Indent your code correctly for readability. Chances are that your IDE have a feature that can do that for you.
于 2011-11-30T07:31:49.327 回答