There is a solution to that in http://www.t-deli.com , in form of splitting a list to sub lists.
However, I found my solution better.
I copied the ForEach component, and added an "every" parameter.
This one addes the "tag" parameter every X iterations so it would look like this
<span jwcid="@ForEach" ... tag="tr" every="ognl:5">
<td> img... </td>
</span>a TR tag will be added to groups of 5 cells.
I offered to submit it at the dev list, but was ignored... :(
Here is the code:
=== START CLASS ===
//Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.
package what.dahell.package
import java.util.Iterator;
import org.apache.tapestry.AbstractComponent; import org.apache.tapestry.IBinding; import org.apache.tapestry.IMarkupWriter; import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.Tapestry;
/**
* Repeatedly renders its wrapped contents while iterating through
* a list of values.
*
* [<a href="../../../../../ComponentReference/Foreach.html">Component Reference</a>]
*
* <p>
* While the component is rendering, the property
* [EMAIL PROTECTED] #getValue() value} (accessed as
* <code>components.<i>foreach</i>.value</code>
* is set to each successive value from the source,
* and the property
* [EMAIL PROTECTED] #getIndex() index} is set to each successive index
* into the source (starting with zero).
*
* @author Howard Lewis Ship
* @version $Id: Foreach.java,v 1.6 2004/04/06 18:51:56 ehatcher Exp $
*
**/
public abstract class Foreach extends AbstractComponent
{
private Object _value;
private int _index;
private boolean _rendering;public abstract IBinding getIndexBinding();
public abstract int getEvery();
/**
* Gets the source binding and returns an [EMAIL PROTECTED] Iterator}
* representing
* the values identified by the source. Returns an empty [EMAIL PROTECTED] Iterator}
* if the binding, or the binding value, is null.
*
* <p>Invokes [EMAIL PROTECTED] Tapestry#coerceToIterator(Object)} to perform
* the actual conversion.
*
**/
protected Iterator getSourceData()
{
Object source = getSource();
if (source == null)
return null;
return Tapestry.coerceToIterator(source);
}public abstract IBinding getValueBinding();
/**
* Gets the source binding and iterates through
* its values. For each, it updates the value binding and render's its wrapped elements.
*
**/
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
{
Iterator dataSource = getSourceData();
// The dataSource was either not convertable, or was empty.
if (dataSource == null)
return;
try { _rendering = true; _value = null; _index = 0;
IBinding indexBinding = getIndexBinding();
IBinding valueBinding = getValueBinding();
String element = getElement();boolean hasNext = dataSource.hasNext();
while (hasNext)
{
_value = dataSource.next();
hasNext = dataSource.hasNext(); if (indexBinding != null)
indexBinding.setInt(_index); if (valueBinding != null)
valueBinding.setObject(_value); if (element != null)
{
if (getEvery() == 0 || _index % getEvery() == 0 ) {
writer.begin(element);
renderInformalParameters(writer, cycle);
}
}renderBody(writer, cycle);
if (element != null) {
if (getEvery() == 0 || !hasNext ||
_index % getEvery() == getEvery() -1 ) writer.end();
}
_index++;
}
}
finally
{
_value = null;
_rendering = false;
}
}/**
* Returns the most recent value extracted from the source parameter.
*
* @throws org.apache.tapestry.ApplicationRuntimeException if the Foreach is not currently rendering.
*
**/
public Object getValue()
{
if (!_rendering)
throw Tapestry.createRenderOnlyPropertyException(this, "value");
return _value;
}public abstract String getElement();
public abstract Object getSource();
/**
* The index number, within the [EMAIL PROTECTED] #getSource() source}, of the
* the current value.
*
* @throws org.apache.tapestry.ApplicationRuntimeException if the Foreach is not currently rendering.
*
* @since 2.2
*
**/
public int getIndex()
{
if (!_rendering)
throw Tapestry.createRenderOnlyPropertyException(this, "index");
return _index;
}}
=== END CLASS ===
<?xml version="1.0" encoding="UTF-8"?> <!-- Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- $Id: Foreach.jwc,v 1.4 2004/02/29 18:49:13 harishkswamy Exp $ -->
<!DOCTYPE component-specification PUBLIC
"-//Apache Software Foundation//Tapestry Specification 3.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
<component-specification
class="what.dahell.package.ForEach"
allow-informal-parameters="yes">
<description>
Loops over a collection of source values. May also emulate an element (like an Any
component).
</description>
<parameter name="source" type="java.lang.Object" direction="in" required="yes">
<description>
The source of values, a Java collection or array.
</description>
</parameter>
<parameter name="value" direction="custom">
<description>
If provided, then on each iteration, the value property is updated.
</description>
</parameter> <parameter name="index" type="int" direction="custom">
<description>
If provided, then the index of the loop is set on each iteration.
</description>
</parameter> <parameter name="element" type="java.lang.String" direction="in">
<description>
If provided, then the Foreach creates an element wrapping its content.
Informal parameters become attributes of the element.
</description>
</parameter> <parameter name="every" type="int" direction="in" default-value="1">
<description>
If provided in conjuction with the element parameter, will
create the element every X iterations.
Default is 1.
</description>
</parameter></component-specification>
××××× Henry Chen:
Hi,
I use @Foreach to output some images into a table and wanna put 5 pics in each row. I tried this:
<span jwcid="photoLoop">
<span jwcid="@conditional" condition=""ognl:listIndex%5==1"><tr></span>
<td align=center>
<img jwcid="pic"/>
</td>
<span jwcid="@conditional" condition=""ognl:listIndex%5==0"></tr></span>
</span>
Tapestry complaint "</tr> is improperly nested with tag <span>", but it
didn't say anything for the "<tr>". Isn't this weird? Can anybody help me
out?
Thanks a ton.
Henry
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
