[英]Grails Field Plugin: Select tag for one-to-many relationships
I'm using Grails 2.2.3 and Fields plugin 1.3. 我正在使用Grails 2.2.3和Fields插件1.3。 I want to customize fields to manage one-to-many relationships using select tag.
我想自定义字段以使用select标签管理一对多关系。
In views/_fields/oneToMany/_input.gsp
I have: 在
views/_fields/oneToMany/_input.gsp
我有:
<g:select name="${property}.id" from="${type.list()}" optionKey="id" value="${value}" class="form-control one-to-many" noSelection="['null': "${label}"]" />
But type
is a set, so I can't use list
function. 但是
type
是一个集合,所以我不能使用list
函数。
How can I retrieve target domain class? 如何检索目标域类?
As long as you use a Map to declare the relationship, for example: 只要您使用Map声明关系,例如:
static hasMany = [ books: Book ]
You can get the list of the referenced domain, which is the key from the hasMany
property of the bean , so the from
attribute should change to 您可以获取引用域的列表,它是bean的
hasMany
属性的关键字,因此from
属性应该更改为
from="${bean.hasMany[property].list()}"
Alternatively you can pass the list to the _input.gsp
template prefixing the variable name with input-
, eg 或者,您可以将列表传递给带有
input-
的变量名前缀的_input.gsp
模板,例如
<f:field property="books" input-domainList="${bookInstaceList}" />
In the _input.gsp
template you can use the variable as follows: 在
_input.gsp
模板中,您可以使用以下变量:
from="${domainList}"
or mixing both methods: 或混合两种方法:
from"${domainList ?: bean.hasMany[property].list()}"
I found this solution for the problem, this code work fine with one to many and one to one 我找到了这个问题的解决方案,这个代码可以正常工作,一对多,一对一
<div class="input-group">
<g:select name="${persistentProperty.toString().contains('one-to-many')?property:property+'.id'}" value="${value?.id}" required="${required}"
noSelection="${['null':'Escriba...']}" class="select2 col-md-6"
multiple="${persistentProperty.toString().contains('one-to-many')?true:false}" from="${grailsApplication.getDomainClass(persistentProperty.properties['associatedEntity'].toString()).clazz.list()}"
optionKey="id"></g:select>
</div>
This solution can be helpful. 这个解决方案很有帮助。 I found in the documentacion of Fields Plugin, the persistentProperty is a class type DomainProperty but their methods doesn't works and I found the correct class is org.grails.datastore.mapping.model.MappingFactory
我发现在Fields Plugin的文档中,persistentProperty是一个类类型DomainProperty但它们的方法不起作用,我发现正确的类是org.grails.datastore.mapping.model.MappingFactory
you can use .toArray() method on your Set instead of .list(). 你可以在你的Set而不是.list()上使用.toArray()方法。
ale 麦酒
You can use the code below to identify the Element of your collection 您可以使用以下代码来标识集合的元素
def className = bean.hasMany[property].properties.typeName
See my "views/_fields/oneToMany/_input.gsp" implementation: 看我的“views / _fields / oneToMany / _input.gsp”实现:
<%@ page defaultCodec="html" %>
<%
def className = bean.hasMany[property].properties.typeName
def simpleName = Class.forName(className).simpleName
def beanSimpleName = bean.class.simpleName
def createUri = "/${simpleName.toLowerCase()}/create?${beanSimpleName.toLowerCase()}.id=${bean.id}"
%>
<a id="add${simpleName}" name="add${simpleName}" href="${createLink(uri: "${createUri}")}">
Add ${simpleName}
</a>
<ul>
<g:each var="item" in="${value}">
<li>
<a href="${createLink(uri: "/${simpleName.toLowerCase()}/show/${item.id}")}">${item}</a>
</li>
</g:each>
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.