Grails: How to make a SAVE button inside the same table and make it work?
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 ve开发者_如何学编程rsion = 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!
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>
精彩评论