[prev in list] [next in list] [prev in thread] [next in thread] 

List:       tapestry-user
Subject:    Re: Component with mixed functionality?
From:       Kaspar Fischer <fischerk () inf ! ethz ! ch>
Date:       2007-12-17 19:59:22
Message-ID: 9A239AFF-9C41-4E62-A817-1B4349B9D454 () inf ! ethz ! ch
[Download RAW message or body]

Thanks, Matt, for your response.

On 16.12.2007, at 02:13, Matt Brock wrote:

> The problem is that I don't think your even/odd requirement will  
> work with
> any of the regular Tapestry components.  Normally you iterate over a
> collection of values (the @For component, for example), and use an  
> OGNL
> expression like class="ognl:(index % 2 == 0 ? 'even' : 'odd')" to  
> determine
> whether the row is odd or even.  But the problem is, not all of  
> your rows
> are going to be rendered.  So what happens when the first row's @If
> condition evaluates true, the second row's condition evaluates  
> false, then
> the third evaluates true?  You'll end up with two rows both marked  
> "even".

That's exactly what was happening in my case!

> The easiest way to do this would be to write your own renderComponent
> method.  That way you get complete control over the output.  This  
> has the
> added advantage of bypassing the OGNL parser.  You don't need a  
> component
> definition file (.jwc) or an HTML file, either.  Just a single java  
> class.
> Something like this:
>
> MyComponent.java
>
> public abstract class MyComponent extends AbstractComponent {
>
> &nbsp;&nbsp;@Parameter
> &nbsp;&nbsp;public abstract MyObject getMyObject();
>
> &nbsp;&nbsp;protected void renderComponent(IMarkupWriter writer,
> IRequestCycle cycle) {
> // You can also use the writer.begin/writer.attribute way of doing  
> things,
> but this is faster.
> &nbsp;&nbsp;&nbsp;&nbsp;StringBuffer output = new StringBuffer();
> &nbsp;&nbsp;&nbsp;&nbsp;output.append("&lt;table&gt;");
> &nbsp;&nbsp;&nbsp;&nbsp;int i = 0;
> &nbsp;&nbsp;&nbsp;&nbsp;if (getMyObject().getTitle() != null) {
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.append("&lt;tr").append 
> (i % 2 ==
> 0 ? " class=\"even\"" :
> "").append("&gt;&lt;td&gt;").append(getMyObject().getTitle()).append 
> ("&lt;/td&gt;&lt;/tr&gt;");
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i++;
> &nbsp;&nbsp;&nbsp;&nbsp;}
> &nbsp;&nbsp;&nbsp;&nbsp;if (getMyObject().getNextProperty() != null) {
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.append("&lt;tr").append 
> (i % 2 ==
> 0 ? " class=\"even\"" :
> "").append("&gt;&lt;td&gt;").append(getMyObject().getNextProperty 
> ()).append("&lt;/td&gt;&lt;/tr&gt;");
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i++;
> &nbsp;&nbsp;&nbsp;&nbsp;}
> &nbsp;&nbsp;&nbsp;&nbsp;...etc...
> &nbsp;&nbsp;&nbsp;&nbsp;output.append("
> ");
> &nbsp;&nbsp;&nbsp;&nbsp;writer.print(output,true);
> &nbsp;&nbsp;}
> }

I get it. -- I finally took Kristian's suggestion as it allowed me
to keep my markup in a HTML-file (even though I don't know whether
the latter is worth it).

Thanks!
Kaspar

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic