开发者

Groovy findAll and each tag question

I have the following domain classes:

class User = {
       String username
       ... 
       Company company
 }

class Company {
    String name
    ...
}

That is, there is a n:1 relationship between user and company. These classes are so and I cannot change them.

At the show.gsp I want to have the details of the company together with links to the u开发者_StackOverflowsers who belongs to this company.

I know that I can achieve this writing an own tag, but I am sure that this would be possible using the each tag or the the findAll tag.

If I do the following

<g:each in="${User.findAll('from User order by username')}" var="userInstance">
    <li><g:link controller="role" action="show"
         id="${userInstance.id}">${userInstance.encodeAsHTML()}</g:link>
   </li>
</g:each>

I've tried to pass the ${companyInstance} as a parameter but either I got an exception or it didn't work.

I've also tried using User.findAllByCompany.

When using:

<g:findAll in="${user}" expr="it.company == ${companyInstance}  ">

I am getting an empty set.

Is there an easy way to achieve this without writing a taglib?

Thanks in advance.

Luis


The <g:findAll> tag iterates over an object list but it does not query the DB as done with User.findAll(..)

The correct code is:

<g:each in="${User.findAll('from User as u where u.company=:company order by username', [company: companyInstance])}" var="userInstance">
    <li><g:link controller="role" action="show"
         id="${userInstance.id}">${userInstance.encodeAsHTML()}</g:link>
   </li>
</g:each>

If you absolutely want to use g:findAll, then you first need to build the list of users as following:

<% def users = User.listOrderByUsername()%>
<g:findAll in="${users}" expr="it.company == ${companyInstance}  "> 

I hope it helps,

Regards,

Fabien.


It's more simple that you think: ${userInstance.company.encodeAsHTML()}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜