You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by bhorvat <ho...@gmail.com> on 2012/07/04 22:04:27 UTC

How to have every second value in the t:loop different

So here is the annoying problem, I have a loop and I need to make every even
rows different then odd rows. So the way I do this is I have a index and a
methods isEvenRow that returns if the row is even. Then based on that I have
one if withe else section. The only difference between content of both is in
one single tiny class called itrEven. 

  <t:loop t:source="selectedModule.settings" t:value="currentSetting"
t:index="index">
       <t:if test="evenRow">
             <div class=&quot;itRow &lt;b>itrEven*"></div>
             <p:else>
                  <div class="itRow"></div>
             </p:else>
       </t:if>
  </t:loop>

So how do I make this less verbose. I dont like the fact that I need to have
one extra field and methos in the page, as well as having the duplication of
the html code (there is a lot of code in between if else section I have just
removed it since it is not important) 

Is there a better solution? 

And yes I know that I am being pedantic about this, but it is really
annoying :)

--
View this message in context: http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: How to have every second value in the t:loop different

Posted by Neil Pierson <ne...@gmail.com>.
how about 

Java:
 
	public String getRowClass() {
		return (index % 2 == 0) ? "evenRow" : "oddRow";
	}


tml:

 <t:loop t:source="selectedModule.settings" t:value="currentSetting" t:index="index">
    ...

	<tr class="${rowClass}">

css:

.oddRow {
	background: etc...
}

.evenRow {
	background: etc....
}



On Jul 4, 2012, at 4:04 PM, bhorvat wrote:

> So here is the annoying problem, I have a loop and I need to make every even
> rows different then odd rows. So the way I do this is I have a index and a
> methods isEvenRow that returns if the row is even. Then based on that I have
> one if withe else section. The only difference between content of both is in
> one single tiny class called itrEven. 
> 
>  <t:loop t:source="selectedModule.settings" t:value="currentSetting"
> t:index="index">
>       <t:if test="evenRow">
>             <div class=&quot;itRow &lt;b>itrEven*"></div>
>             <p:else>
>                  <div class="itRow"></div>
>             </p:else>
>       </t:if>
>  </t:loop>
> 
> So how do I make this less verbose. I dont like the fact that I need to have
> one extra field and methos in the page, as well as having the duplication of
> the html code (there is a lot of code in between if else section I have just
> removed it since it is not important) 
> 
> Is there a better solution? 
> 
> And yes I know that I am being pedantic about this, but it is really
> annoying :)
> 
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


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


Re: How to have every second value in the t:loop different

Posted by Geoff Callender <ge...@gmail.com>.
Sure would. Very cool.

On 05/07/2012, at 9:08 PM, Bob Harner wrote:

> Yeah, the cycle binding prefix would be a cool thing to add to the core.
> On Jul 5, 2012 3:34 AM, "Lance Java" <la...@googlemail.com> wrote:
> 
>> This wiki page explains creating a custom binding prefix to achieve zebra
>> stripes
>> http://wiki.apache.org/tapestry/Tapestry5HowToAddBindingPrefixCycle
>> 
>> eg:
>> <t:loop ...>
>>    <div class="${cycle:odd,even}">...</div>
>> </tloop>
>> 
>> --
>> View this message in context:
>> http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278p5714298.html
>> Sent from the Tapestry - User mailing list archive at Nabble.com.
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>> 
>> 


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


Re: How to have every second value in the t:loop different

Posted by Lance Java <la...@googlemail.com>.
I'm not sure that this implementation of CycleBinding is fit for core as it
uses a thread local so nested bindings won't work (I don't think)

I don't think that the following would work.
<t:loop ...>
   <div class="${cycle:a,b}">...
      <t:loop ...>
         <div class="${cycle:d,e}">...
      </t:loop>
   </div>
</t:loop>

I'm thinking that ComponentResources.storeRenderVariable() and
componentResources.getRenderVariable() might be a better fit than
ThreadLocal?



--
View this message in context: http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278p5714306.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: How to have every second value in the t:loop different

Posted by Bob Harner <bo...@gmail.com>.
Yeah, the cycle binding prefix would be a cool thing to add to the core.
On Jul 5, 2012 3:34 AM, "Lance Java" <la...@googlemail.com> wrote:

> This wiki page explains creating a custom binding prefix to achieve zebra
> stripes
> http://wiki.apache.org/tapestry/Tapestry5HowToAddBindingPrefixCycle
>
> eg:
> <t:loop ...>
>     <div class="${cycle:odd,even}">...</div>
> </tloop>
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278p5714298.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: How to have every second value in the t:loop different

Posted by Lance Java <la...@googlemail.com>.
This wiki page explains creating a custom binding prefix to achieve zebra
stripes
http://wiki.apache.org/tapestry/Tapestry5HowToAddBindingPrefixCycle

eg:
<t:loop ...>
    <div class="${cycle:odd,even}">...</div>
</tloop>

--
View this message in context: http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278p5714298.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: How to have every second value in the t:loop different

Posted by Kristian Marinkovic <kr...@gmail.com>.
hi geoff,

Even Odd is extremely elegant; never thought of such a solution

g,
kris

On Thu, Jul 5, 2012 at 7:32 AM, Geoff Callender
<ge...@gmail.com> wrote:
> ...which is why injecting EvenOdd is such a good solution. It just works,
> everywhere.
>
>
> http://jumpstart.doublenegative.com.au/jumpstart/examples/tables/alternatingloop
>
>
> Cheers,
>
> Geoff
>
> On 5 July 2012 10:49, Bob Harner <bo...@gmail.com> wrote:
>
>> Remember that ":nth-child()" selector doesn't work in IE 8 and
>> earlier, if that matters.
>>
>> On Wed, Jul 4, 2012 at 5:47 PM, bhorvat <ho...@gmail.com> wrote:
>> > Yea I totally agree with that. I have implemented using the css which
>> worked
>> > for me.
>> > cheers all
>> >
>> > --
>> > View this message in context:
>> http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278p5714289.html
>> > Sent from the Tapestry - User mailing list archive at Nabble.com.
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> > For additional commands, e-mail: users-help@tapestry.apache.org
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>

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


Re: How to have every second value in the t:loop different

Posted by Geoff Callender <ge...@gmail.com>.
...which is why injecting EvenOdd is such a good solution. It just works,
everywhere.


http://jumpstart.doublenegative.com.au/jumpstart/examples/tables/alternatingloop


Cheers,

Geoff

On 5 July 2012 10:49, Bob Harner <bo...@gmail.com> wrote:

> Remember that ":nth-child()" selector doesn't work in IE 8 and
> earlier, if that matters.
>
> On Wed, Jul 4, 2012 at 5:47 PM, bhorvat <ho...@gmail.com> wrote:
> > Yea I totally agree with that. I have implemented using the css which
> worked
> > for me.
> > cheers all
> >
> > --
> > View this message in context:
> http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278p5714289.html
> > Sent from the Tapestry - User mailing list archive at Nabble.com.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: How to have every second value in the t:loop different

Posted by Bob Harner <bo...@gmail.com>.
Remember that ":nth-child()" selector doesn't work in IE 8 and
earlier, if that matters.

On Wed, Jul 4, 2012 at 5:47 PM, bhorvat <ho...@gmail.com> wrote:
> Yea I totally agree with that. I have implemented using the css which worked
> for me.
> cheers all
>
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278p5714289.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>

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


Re: How to have every second value in the t:loop different

Posted by bhorvat <ho...@gmail.com>.
Yea I totally agree with that. I have implemented using the css which worked
for me. 
cheers all

--
View this message in context: http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278p5714289.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: How to have every second value in the t:loop different

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Wed, 04 Jul 2012 17:22:32 -0300, bhorvat <ho...@gmail.com>  
wrote:

> My problem is that I have a lot of code duplication, also this seems to  
> me as a simple display problem so I dont like the fact that I have to  
> create logic in the java page for this just so that I can put one extra  
> css class in one place.

Logic in templates is considered an anti-pattern, a big no-no. for many  
years already. Just use Niel Pierson's solution. I think it's perfect, not  
using duplicated HTML. In addition, you can use CSS for that, as Dusko  
said.

-- 
Thiago H. de Paula Figueiredo

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


Re: How to have every second value in the t:loop different

Posted by Kristian Marinkovic <kr...@gmail.com>.
if it is only visual, and just about setting an css class: -> use the
css selector mentioned by dusko; no duplication

On Wed, Jul 4, 2012 at 10:22 PM, bhorvat <ho...@gmail.com> wrote:
> My problem is that I have a lot of code duplication, also this seems to me as
> a simple display problem so I dont like the fact that I have to create logic
> in the java page for this just so that I can put one extra css class in one
> place.
>
> Apart from that the 2 blocks in your case would be exactly the same. In php
> it would be easier to solve this (wow I am actually saying something is
> better to solve in php :) ) as I could just suround the class with simple
> if, but here I cant put the if logic in the middle of a tag so I have to
> duplicate html.
>
> Hope this explains it a bit better
>
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278p5714281.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>

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


Re: How to have every second value in the t:loop different

Posted by bhorvat <ho...@gmail.com>.
My problem is that I have a lot of code duplication, also this seems to me as
a simple display problem so I dont like the fact that I have to create logic
in the java page for this just so that I can put one extra css class in one
place. 

Apart from that the 2 blocks in your case would be exactly the same. In php
it would be easier to solve this (wow I am actually saying something is
better to solve in php :) ) as I could just suround the class with simple
if, but here I cant put the if logic in the middle of a tag so I have to
duplicate html. 

Hope this explains it a bit better 

--
View this message in context: http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278p5714281.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: How to have every second value in the t:loop different

Posted by Kristian Marinkovic <kr...@gmail.com>.
it would be easier to give you an advice if you'd outline the problem
you are having a little more.

but if you ask me i'd put a delegate inside the loop and let the "to"
binding decide what component/block to render. i use this approach to
render a list of "item" (types) that may have completely different
representations.

<t:loop t:source="selectedModule.settings" t:value="currentSetting"
t:index="index">
    <t:delegate to="decide" />
</t:loop>

<t:block>
    <div t:id="evenRowComp" />
    <div t:id="otherComp" />
</t:block>

as always it depends on what you wanna achieve

g,
kris

On Wed, Jul 4, 2012 at 10:04 PM, bhorvat <ho...@gmail.com> wrote:
> So here is the annoying problem, I have a loop and I need to make every even
> rows different then odd rows. So the way I do this is I have a index and a
> methods isEvenRow that returns if the row is even. Then based on that I have
> one if withe else section. The only difference between content of both is in
> one single tiny class called itrEven.
>
>   <t:loop t:source="selectedModule.settings" t:value="currentSetting"
> t:index="index">
>        <t:if test="evenRow">
>              <div class=&quot;itRow &lt;b>itrEven*"></div>
>              <p:else>
>                   <div class="itRow"></div>
>              </p:else>
>        </t:if>
>   </t:loop>
>
> So how do I make this less verbose. I dont like the fact that I need to have
> one extra field and methos in the page, as well as having the duplication of
> the html code (there is a lot of code in between if else section I have just
> removed it since it is not important)
>
> Is there a better solution?
>
> And yes I know that I am being pedantic about this, but it is really
> annoying :)
>
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>

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


Re: How to have every second value in the t:loop different

Posted by bhorvat <ho...@gmail.com>.
So this worked for me

.itRow {
    display: table-row;
}
#rowId:nth-child(even) {
    background-color:#8d8b8c;
}

<div id="rowId" class="itRow"></div>


If you have a neater way to do this, due tell me. But like I said thank you
all for great suggestions :D  

--
View this message in context: http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278p5714285.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: How to have every second value in the t:loop different

Posted by bhorvat <ho...@gmail.com>.
The css selector is what I need. But I am not sure how to use it, I have
tired 

.itRow {
    display: table-row;
}
.itRow:nth-child(even) {
    background-color:#8d8b8c;
}

but it is not working, will continue to try it till I figure it out, but if
you have an idea please let me know. Still tnx for the great advice. 

--
View this message in context: http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278p5714284.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: How to have every second value in the t:loop different

Posted by Dusko Jovanovski <du...@gmail.com>.
Hello there

If the change you want to apply to those elements is purely visual, I would
suggest the :nth-child(even) pseudo selector [1].

On the other hand, if you still want to apply that particular class to the
div, I would suggest this approach:

 <t:loop t:source="selectedModule.settings" t:value="currentSetting"
t:index="index">
             <div class="${divClass}"></div>
  </t:loop>

You return the wanted class in the getDivClass() method.

[1] http://www.w3.org/TR/css3-selectors/#nth-child-pseudo

On Wed, Jul 4, 2012 at 10:04 PM, bhorvat <ho...@gmail.com> wrote:

> So here is the annoying problem, I have a loop and I need to make every
> even
> rows different then odd rows. So the way I do this is I have a index and a
> methods isEvenRow that returns if the row is even. Then based on that I
> have
> one if withe else section. The only difference between content of both is
> in
> one single tiny class called itrEven.
>
>   <t:loop t:source="selectedModule.settings" t:value="currentSetting"
> t:index="index">
>        <t:if test="evenRow">
>              <div class=&quot;itRow &lt;b>itrEven*"></div>
>              <p:else>
>                   <div class="itRow"></div>
>              </p:else>
>        </t:if>
>   </t:loop>
>
> So how do I make this less verbose. I dont like the fact that I need to
> have
> one extra field and methos in the page, as well as having the duplication
> of
> the html code (there is a lot of code in between if else section I have
> just
> removed it since it is not important)
>
> Is there a better solution?
>
> And yes I know that I am being pedantic about this, but it is really
> annoying :)
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/How-to-have-every-second-value-in-the-t-loop-different-tp5714278.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>