grails - Grails字段插件-自定义集合显示成员作为链接

标签 grails field

场景:带有传递字段的订单显示 View ,总成本。默认字段生成 View 不会呈现可传递字段,因此我必须手动指定要查看的字段。以下工作正常:
<f:with bean="customerOrder"> <f:display property='token' wrapper="displayText"/> <f:display property='lineItems' wrapper="displayCollection"/> <f:display property='total' wrapper="displayMoney"/> <f:display property='dateCreated' wrapper="displayDate"/> <f:display property='customer' wrapper="displayLink"/> </f:with>
displayCollection部分由views / _fields / displayCollection /中的_displayWrapper.gsp文件处理。它看起来像这样:
<li class="fieldcontain" style="list-style-type: none;"> <span class="property-label">${label}</span> <span class="property-value" aria-labelledby="${property}-label"> <ul> <g:each in="${value}" var="item"> <li>${item?.toString()}</li> </g:each> </ul> </span> </li>
这是通用集合显示字段。它将适用于购物车中的文章,用户的帖子等。唯一的是,收集成员仅显示为文本,而不是链接。

如果$ {value}是单个类成员而不是集合,则下面的_displayWrapper.gsp可以正常工作。
<li class="fieldcontain" style="list-style-type: none;"> <span class="property-label">${label}</span> <span class="property-value" aria-labelledby="${property}-label"> <g:link action="show" controller="${property}" id="${value?.id}">${value?.toString()}</g:link> </span> </li>
问题是,如何从集合的成员中导出 Controller 名称,该集合是通过$ {value}获得的?

我已经安装了 View 模板,那里没有运气。同样,我查看了fields插件代码,也没有运气。

有任何想法吗?

最佳答案

10分钟后,我完成了。解决方案的本质在于,可以从相应的Grails域类派生Grails Controller 名称。相关的_displayWrapper如下所示:
<li class="fieldcontain" style="list-style-type: none;"> <span class="property-label">${label}</span> <span class="property-value" aria-labelledby="${property}-label"> <ul> <g:each in="${value}" var="item"> <li> <g:link controller="${item.class.getSimpleName()[0].toLowerCase() + item.class.getSimpleName().substring(1)}" action="show" id="${item.id}"> ${item?.toString()} </g:link> </li> </g:each> </ul> </span> </li>
也可以编写一个自定义标签来执行此操作。

关于grails - Grails字段插件-自定义集合显示成员作为链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40777730/

相关文章:

grails - 对Grails中的Elastic Search结果调用findBy…时发生TransientObjectException

grails - 如何在grails中调用具有动态名称的mysql存储过程

grails - Grails:Spring Security Core自定义身份验证getUserByUserName返回空对象

java - 如何检查一个对象是否是某种类型的数组

c# - 为什么没有人接受 C# 中的公共(public)字段?

javascript - 如何使用 jQuery 获取传递的 html 元素的 Id?

class - 如何将 Grails 中域字段的默认设置更改为可为空

PHP - 从数组中获取元素的索引

c# - 反序列化时忽略过时的字段

python - 如何检查 django 表单数据是否为 ​​None、 ' '(空字符串)或 False