Grails 正在为父级保存域集
我拥有以下域:Grails 正在为父级保存域集,grails,gorm,Grails,Gorm,我拥有以下域: class Attribute { static hasMany = [attributeParameters: AttributeParameter] } class AttributeParameter { String value Integer sequenceNo static belongsTo = [attribute: Attribute] } 我有一个表单,希望在其中显示某个属性的所有现有属性参数,并允许用户填充其值并单击保存。保存时
class Attribute {
static hasMany = [attributeParameters: AttributeParameter]
}
class AttributeParameter {
String value
Integer sequenceNo
static belongsTo = [attribute: Attribute]
}
我有一个表单,希望在其中显示某个属性的所有现有属性参数,并允许用户填充其值并单击保存。保存时,每个AttributeParameter(已经有ID)都需要更新
我目前在如何创建HTML以使其正常工作方面还处于空白状态。我试过这个:
代码简化为清晰:
<form>
<input type="hidden" name="attributeParameters[0].id" value="1" />
<input type="text" name="attributeParameters[0].value" value="1234567" />
<input type="hidden" name="attributeParameters[1].id" value="2" />
<input type="text" name="attributeParameters[1].value" value="name" />
</form>
def save() {
def attribute = Attribute.get(params.id)
attribute.properties = params
}
这很有效。我唯一关心的是参数的输入顺序。如果它们总是以它们在表单上显示的相同顺序进入params对象,那么我就可以了。但如果它们以不同的方式出现,我可能修改了错误的AttributeParameter的值
因此,仍然在寻找更好的方法或某种验证,以确保它们的参数始终是先进先出的
更新2:
我遇到了它,这是我想要的,但我无法将Attribute.attributeParameters更改为列表。他们需要作为一组人待在一起。你能这样做吗:
<form>
<input type="text" name="attributeParameter.1" value="1234567" />
<input type="text" name="attributeParameter.2" value="name" />
</form>
这样,您就可以直接获得每个参数的实际id,而不管它们的处理顺序如何
params.list('attributeParameters').each {
def ap = AttributeParameter.get(it.id)
ap.value = it.value
ap.save()
}
<form>
<input type="text" name="attributeParameter.1" value="1234567" />
<input type="text" name="attributeParameter.2" value="name" />
</form>
<g:textField name="attributeParameter.${attributeParameterInstance.id}" value="${attributeParameterInstance.value}" />
params.attributeParameter.each {id, val->
def ap = AttributeParameter.get(id)
ap.value = val
ap.save()
}