Grails:如何消除 create.gsp 和 save.gsp 并将其移动到 list.gsp 中列表的第一行?

发布于 2024-09-29 03:12:21 字数 5126 浏览 0 评论 0 原文

我想摆脱 create.gsp 和 save.gsp 并将所有内容都放在同一页面上。 我所做的是将所有这些字段移至 list.gsp 中列表的第一行,

但现在我不知道如何将其与其余数据连接并使其工作。

我必须让创建按钮起作用。但随后我无法使更新按钮工作。

这是第一行的图片

所有内容都在第一行中创建,然后单击“创建”,将创建一个新行,其中所有数据都放入文本字​​段中。第一行没有 ID,但其他行都有。

我还让擦除按钮起作用了。所以基本上唯一缺少的是更新按钮。

有什么想法吗?

提前致谢!

编辑

这是我在控制器中的内容:

    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,'Rcommodity':params?.Rcommodity])
                    }
                }
                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 {
                    redirect(action: "list", id: densityInstance.id)
                }
            }
            else {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'density.label', default: 'Density'), params.id])}"
                redirect(action: "list")
            }
        }

由于grails按列进行操作,因此行有点混乱,但它是:

<g:each in="${densityInstanceList}" status="i" var="densityInstance">
                    <g:form action="update">
                        <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
                            <td>${fieldValue(bean: densityInstance, field: "id")}</td>
                            <td><g:textField name="Rcommodity" value="${densityInstance?.commodity}"  class="input" onKeyPress="return alpha(event)"/>&nbsp;<img src="${resource(dir:'images/skin',file:'information.png')}" ></td>
                            <td><g:textField name="Rorigin" value="${densityInstance?.origin}" maxlength="3" size="5" class="input" onKeyPress="return alpha(event)"/>&nbsp;<img src="${resource(dir:'images/skin',file:'information.png')}" ></td>
                            <td><g:textField name="Rshipper" value="${densityInstance?.shipper}" size="3" class="input" onkeypress="return IsNumeric(event)"/> <g:textField name="RshipperName" value="${densityInstance?.shipperName}" size="15" class="input-b" onfocus="this.blur()"/>&nbsp;<img src="${resource(dir:'images/skin',file:'information.png')}" ></td>
                            <td><g:textField name="Ragent" value="${densityInstance?.agent}" size="3" class="input" onkeypress="return IsNumeric(event)"/> <g:textField name="RagentName" value="${densityInstance?.agentName}" size="15" class="input-b" onfocus="this.blur()"/>&nbsp;<img src="${resource(dir:'images/skin',file:'information.png')}" ></td>
                            <td><g:textField name="Rdest" value="${densityInstance?.dest}" size="5" class="input" onKeyPress="return alpha(event)"/>&nbsp;<img src="${resource(dir:'images/skin',file:'information.png')}" ></td>
                            <td><g:textField name="Rdensity" value="${densityInstance?.density}" size="15" class="input" onkeypress="return IsNumeric(event)"/></td>
                            <td><g:textField name="RAM" value="${densityInstance?.AM}"  size="1" class="input"/></td>
                            <td width="100">
                            <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: 'Esta seguro que desea Eliminar?')}');" />
                            </g:form>
                            </td>
                        </tr>
                    </g:form></g:each>

I want to get rid of the create.gsp and save.gsp and have everything on the same page.
What I did is move all those fields into the first row of my list in list.gsp

But now I don't know how to connect it with the rest of the data and make it work.

I got to make the create button work. But then I cannot make the update button work.

Here is a picture of how the first rows look like

Everything is created in the first row then you click CREATE and a new row gets created with all the data put in the textfields. The first row does not have an ID, but all of the others do.

I also got the erase button to work. So basically the only thing missing is the UPDATE button.

Any ideas??

Thanks in advance!

EDIT

Here is what I have in the controller:

    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,'Rcommodity':params?.Rcommodity])
                    }
                }
                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 {
                    redirect(action: "list", id: densityInstance.id)
                }
            }
            else {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'density.label', default: 'Density'), params.id])}"
                redirect(action: "list")
            }
        }

And since grails does it by columns the row is kinda messy but here it is:

<g:each in="${densityInstanceList}" status="i" var="densityInstance">
                    <g:form action="update">
                        <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
                            <td>${fieldValue(bean: densityInstance, field: "id")}</td>
                            <td><g:textField name="Rcommodity" value="${densityInstance?.commodity}"  class="input" onKeyPress="return alpha(event)"/> <img src="${resource(dir:'images/skin',file:'information.png')}" ></td>
                            <td><g:textField name="Rorigin" value="${densityInstance?.origin}" maxlength="3" size="5" class="input" onKeyPress="return alpha(event)"/> <img src="${resource(dir:'images/skin',file:'information.png')}" ></td>
                            <td><g:textField name="Rshipper" value="${densityInstance?.shipper}" size="3" class="input" onkeypress="return IsNumeric(event)"/> <g:textField name="RshipperName" value="${densityInstance?.shipperName}" size="15" class="input-b" onfocus="this.blur()"/> <img src="${resource(dir:'images/skin',file:'information.png')}" ></td>
                            <td><g:textField name="Ragent" value="${densityInstance?.agent}" size="3" class="input" onkeypress="return IsNumeric(event)"/> <g:textField name="RagentName" value="${densityInstance?.agentName}" size="15" class="input-b" onfocus="this.blur()"/> <img src="${resource(dir:'images/skin',file:'information.png')}" ></td>
                            <td><g:textField name="Rdest" value="${densityInstance?.dest}" size="5" class="input" onKeyPress="return alpha(event)"/> <img src="${resource(dir:'images/skin',file:'information.png')}" ></td>
                            <td><g:textField name="Rdensity" value="${densityInstance?.density}" size="15" class="input" onkeypress="return IsNumeric(event)"/></td>
                            <td><g:textField name="RAM" value="${densityInstance?.AM}"  size="1" class="input"/></td>
                            <td width="100">
                            <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: 'Esta seguro que desea Eliminar?')}');" />
                            </g:form>
                            </td>
                        </tr>
                    </g:form></g:each>

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

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

发布评论

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

评论(2

冰火雁神 2024-10-06 03:12:21

为了摆脱 create.gsp 和 edit.gsp,控制器的操作应该与脚手架中的操作完全相同,除了 save{} 和 update{} 应该以

redirect(action: list)

In order for "Update" Buttons to work, every "Update" 结尾应将其表单(表格的一行)提交给“更新”操作。有关 update{} 的其他所有内容均应保持原样。

当然,该线条形式应该包含所有对象属性。

代码示例后更新

  1. (编辑:哦,我一直误读代码)为什么需要 2 个嵌套的 g:forms?我会尝试选择一个。只需调试提交给更新操作的内容 - params 是否包含对象字段。
  2. 我还将消除代码中对重定向(操作:“列表”)的过多调用。
  3. 看起来您没有在列表操作中使用 ${id} ?如果是这样,您不必将其作为列表操作参数传递。
  4. 我相信,有一天您会希望通过 Ajax 提交表格行。为此,将每一行括在
    中,将行提取到单独的模板中,然后提交到另一个操作,例如 ajaxUpdate,这将执行相同的操作,但结束与渲染(模板:'lineTemplate')。然后将 g:form 替换为 g:formRemote update="[success:'line${denyInstance.id}',failure:'line${denyInstance.id}']" 和 viola。这也适用于“撤消”操作。

In order to get rid of create.gsp and edit.gsp, your controller's actions should work exactly like in scaffolding, except for save{} and update{} should end with

redirect(action: list)

In order for "Update" buttons to work, every "Update" should submit its form (one line of the table) to "update" action. Everything else about update{} should remain as it was.

That line form should contain all the object properties, for sure.

UPDATE after code sample.

  1. (EDIT: oh, I keep misreading the code) Why do you need 2 nested g:forms? I'd try to go with one. Just debug what is being submitted to the update action - do params contain object fields.
  2. I'd also eliminate excess calls to redirect(action: "list") in code.
  3. Looks like you're not using ${id} in list action? If so, you don't have to pass it as list action parameter.
  4. I believe, some day you'll wish to submit table lines via Ajax. For that, enclose each line in a <div id="line${densityInstance.id}">, extract the line into separate template, and submit to another action, say, ajaxUpdate, which will do the same, but end with render(template: 'lineTemplate'). Then replace g:form with g:formRemote update="[success:'line${densityInstance.id}',failure:'line${densityInstance.id}']", and viola. This will also work for "undo" action.
彩扇题诗 2024-10-06 03:12:21

我认为问题在于您的 actionSubmits 正在提交包含它们的表单,并且您的数据元素位于外部表单中,因此您的更新操作调用不会收到任何要更新的数据。如果您在更新操作的顶部放置一些调试,您可以证明或反驳这个理论。 println“${params}”

I believe the issue is that your actionSubmits are submitting the form containing them and your data elements are in the outer form so your update action call is not receiving any data to be updated. If you put some debug in the top of the update action, you could prove or disprove this theory. println "${params}"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文