Grails:如何在同一个表中创建一个“保存”按钮并使其工作?

发布于 2024-09-26 20:19:48 字数 3139 浏览 0 评论 0原文

我在 Grails 提供的表/列表中进行了创建。

这是我所拥有的图片

正如您所看到的,我在表格的第一行上创建了所有内容,然后从第二行开始是实际的列表。

在第二行的最后一列中,您可以看到我有更新按钮和删除按钮。

删除按钮似乎工作正常,但我在使用更新按钮时遇到问题。

这是最后一栏的代码

<g:form>
    <g:hiddenField name="id" value="${densityInstance?.id}" />
    <g:actionSubmit class="editar" action="update" value="${message(code: 'default.button.editar.label', default: '&nbsp;&nbsp;&nbsp;')}" />
    <g:actionSubmit class="eliminar" action="delete" value="${message(code: 'default.button.eliminar.label', default: '&nbsp;&nbsp;&nbsp;')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure you want to delete?')}');" />
</g:form>

这是我在控制器中删除和更新的

def delete = {
    def densityInstance = Density.get(params.id)
    if (densityInstance) {
        try {
            densityInstance.delete(flush: true)
            flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'density.label', default: 'Density'), params.id])}"
            redirect(action: "list")
        }
        catch (org.springframework.dao.DataIntegrityViolationException e) {
            flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'density.label', default: 'Density'), params.id])}"
            redirect(action: "show", id: params.id)
        }
    }
    else {
        flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'density.label', default: 'Density'), params.id])}"
        redirect(action: "list")
    }
}

代码删除似乎工作正常,这是更新,(也许是我需要编辑的保存定义,我'我不太确定,这就是我问的原因,

这是更新:

def update = {
    def densityInstance = Density.get(params.id)
    if (densityInstance) {
        if (params.version) {
            def version = params.version.toLong()
            if (densityInstance.version > version) {

                densityInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'density.label', default: 'Density')] as Object[], "Another user has updated this Density while you were editing")
                render(view: "list", model: [densityInstance: densityInstance])
                return
            }
        }
        densityInstance.properties = params
        if (!densityInstance.hasErrors() && densityInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.updated.message', args: [message(code: 'density.label', default: 'Density'), densityInstance.id])}"
            redirect(action: "list", id: densityInstance.id)
        }
        else {
            render(view: "list", model: [densityInstance: densityInstance])
        }
    }
    else {
        flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'density.label', default: 'Density'), params.id])}"
        redirect(action: "list")
    }
}

提前致谢!

I have the create inside the table/list that Grails provides.

Here is a pictures of what I have

As you can see, I create everything on the first row of my table, and then from the 2nd row and on is the actual list.

In the last column of the 2nd row, you can see I have the UPDATE button and a delete button.

The delete button seems to be working fine, but I am having problems with the UPDATE button.

Here is the code for that last column

<g:form>
    <g:hiddenField name="id" value="${densityInstance?.id}" />
    <g:actionSubmit class="editar" action="update" value="${message(code: 'default.button.editar.label', default: '   ')}" />
    <g:actionSubmit class="eliminar" action="delete" value="${message(code: 'default.button.eliminar.label', default: '   ')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure you want to delete?')}');" />
</g:form>

And here is what I have for the delete and update in the controller

def delete = {
    def densityInstance = Density.get(params.id)
    if (densityInstance) {
        try {
            densityInstance.delete(flush: true)
            flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'density.label', default: 'Density'), params.id])}"
            redirect(action: "list")
        }
        catch (org.springframework.dao.DataIntegrityViolationException e) {
            flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'density.label', default: 'Density'), params.id])}"
            redirect(action: "show", id: params.id)
        }
    }
    else {
        flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'density.label', default: 'Density'), params.id])}"
        redirect(action: "list")
    }
}

The DELETE seems to be working fine, and here is the UPDATE, (maybe is the SAVE def that I need to edit, I'm not so sure and that's why I'm asking.

Here is the UPDATE:

def update = {
    def densityInstance = Density.get(params.id)
    if (densityInstance) {
        if (params.version) {
            def version = params.version.toLong()
            if (densityInstance.version > version) {

                densityInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'density.label', default: 'Density')] as Object[], "Another user has updated this Density while you were editing")
                render(view: "list", model: [densityInstance: densityInstance])
                return
            }
        }
        densityInstance.properties = params
        if (!densityInstance.hasErrors() && densityInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.updated.message', args: [message(code: 'density.label', default: 'Density'), densityInstance.id])}"
            redirect(action: "list", id: densityInstance.id)
        }
        else {
            render(view: "list", model: [densityInstance: densityInstance])
        }
    }
    else {
        flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'density.label', default: 'Density'), params.id])}"
        redirect(action: "list")
    }
}

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2024-10-03 20:19:48

您拥有的 g:form 仅位于最终表列上,带有一个隐藏的表单参数,即 id。删除作品,因为它只需要一个 id。更新需要表单条目的其余部分。每个可编辑字段都有一个表单条目,但它们没有包含在该 g:form 中,因此它们的数据不会随表单一起提交。

您需要使 g:form 包含表格行的所有列。例如:

<g:form>
  <tr>
    <td>${densityInstance?.id}<g:hiddenField name="id" value="${densityInstance?.id}" /></td>
    <td><g:textField name="commodity" value="${...}"/></td>
    ...
    <td>
      <g:actionSubmit class="editar" action="update" value="${message(code: 'default.button.editar.label', default: '   ')}" />
      <g:actionSubmit class="eliminar" action="delete" value="${message(code: 'default.button.eliminar.label', default: '   ')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure you want to delete?')}');" />
    </td>
  </tr>
</g:form>

The g:form you have is only on the final table column, with one hidden form parameter, the id. Delete works, since all it needs is an id. The update requires the rest of the form entries. The editable fields each have a form entry, but they are not enclosed in that g:form, so their data isn't submitted with the form.

You need to make the g:form enclose all the columns of the table row. For example:

<g:form>
  <tr>
    <td>${densityInstance?.id}<g:hiddenField name="id" value="${densityInstance?.id}" /></td>
    <td><g:textField name="commodity" value="${...}"/></td>
    ...
    <td>
      <g:actionSubmit class="editar" action="update" value="${message(code: 'default.button.editar.label', default: '   ')}" />
      <g:actionSubmit class="eliminar" action="delete" value="${message(code: 'default.button.eliminar.label', default: '   ')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure you want to delete?')}');" />
    </td>
  </tr>
</g:form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文