2015-06-15 31 views
0

我在GSP這樣的循環:打破G:如果/ G:每個

<g:each in="${personInstance.followed}" var="c" > 
      <g:if test="${c.equals(person)}"> 
      <g:link id="${person.id}" action="unfollow" controller="message">unfollow</g:link> 
      </g:if> 
</g:each> 

我如何使用和g突破:每個或g:如果? 任何想法?

+1

在'g:if'或'g:each'的邏輯標籤中沒有'break'的概念。你想達到什麼目的?從您發佈的代碼中不清楚。 –

+0

當我第一次返回true時,我需要打破這個循環。 – tommy

+0

所以,你在'personInstance.followed'屬性中擁有'persons'集合,並且如果該集合包含'person',你需要顯示一些東西?是對的嗎?如果是的話,對此有更好的辦法(我可以在答案中解釋)。 –

回答

0

聽起來好像你正在尋找顯示一個或另一個基於實例是否在一個集合內。你最好的選擇是在收藏上使用contains。例如:

<g:if test="${personInstance.followed.contains(person)}"> 
    Display your unfollow stuff here ... 
</g:if> 
<g:else> 
    Display your follow stuff here ... 
</g:else> 
0

一般在Groovy和GSP你不需要break和你真正想要的是使用findAll標籤,該標籤上的過濾條件,以便您的邏輯將成爲(未經測試示例代碼):

<g:findAll in="${personInstance.followed}" expr="c.equals(person)" var="c" > 
     <g:link id="${person.id}" action="unfollow" controller="message">unfollow</g:link> 
</g:findAll>