开发者

Grails: How do I eliminate the create.gsp and save.gsp and move it to the first line of the list in list.gsp?

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

Grails: How do I eliminate the create.gsp and save.gsp and move it to the first line of the list in list.gsp?

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)"/>&nbsp;<img src="${resource(dir:'images/skin',fi开发者_StackOverflow社区le:'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>


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜