简体   繁体   English

如何摆脱挂毯循环?

[英]HowTo break out of a Tapestry loop?

So, i am trying to break out of tapestry loop here. 所以,我试图在这里摆脱挂毯循环。

This is my -more or less- simplified scenario: 这是我的或多或少简化的场景:

<ul>
    <t:loop source="firstSource" value="firstValue">
            <li>
                <t:loop source="firstValue" value="secondValue">
                    <p>${secondValue}</p>
                </t:loop>
                <t:loop source="secondSource" value="thirdValue">
                    <p>${thirdValue}</p>
                </t:loop>
            </li>
    </t:loop>
</ul>

What I do not want to have is: Tapestry loops through all entries in firstValue - then loops through all entries in secondSource. 我不想要的是:Tapestry遍历firstValue中的所有条目-然后遍历secondSource中的所有条目。 I do not want to iterate through secondSource inside the loop of fristValue as this would iterate through all entries in secondSource - and I just want to do 1 iteration at a time. 我不想遍历fristValue循环内的secondSource,因为这会遍历secondSource中的所有条目-我只想一次进行1次迭代。

What I want to have is: Tapestry enters the loop for firstValue and does some printing or whatever, then breaks after the first iteration and jumps into secondSource to do the first iteration . 我想拥有的是:Tapestry进入firstValue循环并进行一些打印或其他操作,然后在第一次迭代后中断并跳入secondSource进行第一次迭代。 After it has finished it jumps back to firstValue and repeats these steps. 完成后,它会跳回到firstValue并重复这些步骤。 This is what in Java the "break;" 这就是Java中的“中断”; would do. 会做。

I did not find a clue in the Tapestry documentation on how to do this, nor in their forums. 在Tapestry文档中没有找到有关如何执行此操作的线索,也没有在他们的论坛中找到任何线索。

But it has to be possible in some way. 但是它必须以某种方式成为可能。 I can not imagine I am the only one trying to do this. 我无法想象我是唯一尝试这样做的人。

Just put an if statement around the logic, probably using an index variable: 只需在逻辑周围放置一个if语句,可能使用一个索引变量:

   <t:loop source="firstSource" value="firstValue">
            <li>
                <t:loop source="firstValue" value="secondValue" index="firstValueIndex">
                   <t:if test="firstCondition">
                     <p>${secondValue}</p>
                   </t:if>
                </t:loop>
                <t:loop source="secondSource" value="thirdValue">
                  <t:if test="secondCondition">
                    <p>${thirdValue}</p>
                  </t:if>
                </t:loop>
            </li>
    </t:loop>

In the Java page: 在Java页面中:

@Property
private int firstValueIndex;

public boolean getFirstCondition() {
  // logic to determine whether to break out
  return firstValueIndex == 0;
}

public boolean getSecondCondition() {
  // logic
}

My guess is that you have three sources of data and are trying to output three columns, is this right? 我的猜测是您有三个数据源,并试图输出三列,对吗?

Sometimes you have to transform your data a little bit: For example, you might need to do some work to convert one value from each of the three inputs into a single value: 有时您需要稍微转换一下数据:例如,您可能需要做一些工作才能将三个输入中的每个输入的一个值转换为单个值:

public class Row {  

  Object col1, col2, col2;

}

In your Java code, you would build up a List of Row objects. 在Java代码中,您将建立一个Row对象列表。

In your template, you iterate over the Row objects, rendering the col1, col2 and col3 properties. 在模板中,您遍历Row对象,呈现col1​​,col2和col3属性。

(In Tapestry 5.3 and above, a public field can be treated as a property.) (在Tapestry 5.3及更高版本中,公共字段可被视为属性。)

I've used similar techniques to output a calendar, which can be very tricky to manage using conditionals and the like inside the template. 我使用了类似的技术来输出日历,要在模板中使用条件等进行管理可能非常棘手。

Remember the role of the Controller in MVC: its job to mediate between the model and the view; 记住Controller在MVC中的角色:在模型和视图之间进行中介的工作; sometimes that includes some simple transformations of the model data to fit in with the view. 有时包括对模型数据的一些简单转换以适合视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM