Grails:如何在同一个表中创建一个“保存”按钮并使其工作?
我在 Grails 提供的表/列表中进行了创建。
这是我所拥有的图片
正如您所看到的,我在表格的第一行上创建了所有内容,然后从第二行开始是实际的列表。
在第二行的最后一列中,您可以看到我有更新按钮和删除按钮。
删除按钮似乎工作正常,但我在使用更新按钮时遇到问题。
这是最后一栏的代码
<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>
这是我在控制器中删除和更新的
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您拥有的
g:form
仅位于最终表列上,带有一个隐藏的表单参数,即 id。删除作品,因为它只需要一个 id。更新需要表单条目的其余部分。每个可编辑字段都有一个表单条目,但它们没有包含在该g:form
中,因此它们的数据不会随表单一起提交。您需要使
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 thatg: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: