我在Grails提供的表/列表中创建了。
以下是我所拥有的图片
http://i56.tinypic.com/25jwmdf.jpg
正如您所看到的,我在表格的第一行创建了所有内容,然后从第二行开始创建了实际列表。
在第二行的最后一列,您可以看到我有UPDATE按钮和删除按钮。
删除按钮似乎工作正常,但我遇到UPDATE按钮问题。
以下是最后一栏的代码
<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")
}
}
DELETE似乎运行正常,这里是UPDATE,(可能是我需要编辑的SAVE def,我不太确定,这就是我要问的原因。
这是更新:
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")
}
}
提前致谢!
答案 0 :(得分:2)
您拥有的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>