【问题标题】:Grails fields plugin - custom collection display members as linksGrails fields plugin - 自定义集合显示成员作为链接
【发布时间】:2016-11-24 12:36:10
【问题描述】:

场景:带有传递字段的订单显示视图,总成本。默认字段生成视图不会呈现传递字段,因此我必须手动指定要查看的字段。以下工作正常:

<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>

问题是,我如何从集合的成员中获取控制器名称,而集合是通过 ${value} 获得的?

我已经安装了视图模板,但没有运气。同样,我查看了字段插件代码,也没有运气。

有什么想法吗?

【问题讨论】:

    标签: grails field


    【解决方案1】:

    10 分钟后,我完成了。解决方案的本质在于Grails控制器名称可以从对应的Grails域类中派生出来。以下是相关的 _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>

    也可以编写一个自定义标签来做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 2022-09-27
      • 2020-01-20
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多