sourceforge > tacos
 

PartialFor

Extension of Tapestry's For component which allows for more efficient looping over lists of elements during ajax request/responses. The parameters provided by For are left out of this spec, for those values/meanings see the referenced tapestry page.

This component will not render the contents of a particular loop during an ajax request if the key value of the element/component doesn't exist as an updateComponent to the original request. The values will still be iterated over, just not rendered.

See also: For , Live Demo

Parameters

Name Type Direction Required Default Description
keyProvider IKeyProvider in no IdentityKeyProvider Converter from elements to keys. Used by tree to identify node ids.
partialBlock PartialRenderBlock in no   Provides ability to have callbacks performed on a component during partial renders in case component requires maintaing some sort of internal state even if each item specified in the source isn't actually rendered.
evenOdd org.apache.tapestry.bean.EvenOdd in no   The evenodd bean that should be incremented for each loop iteration, mainly used to maintain state on non-rendered items.

Body: rendered

Informal parameters: allowed

Reserved parameters:

Examples

Using the refresh/effects demo page

HTML template

          
<div id="countries">
  <span jwcid="foreachItem" >
    <div jwcid="@Any" class="ognl:beans.evenOdd.next" id="ognl:currItem.toString()" >
      <div>
        <a jwcid="linkToggle" class="toggle">
          <span jwcid="@Insert" value="ognl:currSelected ? 'Hide' : 'Details'"/>
        </a>
        <span jwcid="@Insert" value="ognl:currItem.displayCountry"
                      class="ognl:currSelected ? 'detailHeading' : 'heading'"/>
      </div>
      
      <span jwcid="@If" condition="ognl:currSelected">
        <div class="countryDetail">
          <div class="flag">
            <img jwcid="flagImage" width="34" height="18"/><br/>
            <span jwcid="@Insert" value="ognl:currItem.country"/>
          </div>
         
          <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
          In consequat lectus a eros. Vestibulum tortor. Cras sapien.
          Morbi sed ipsum. Class aptent taciti sociosqu ad litora
          torquent per conubia nostra, per inceptos hymenaeos. Fusce
          aliquam auctor tortor.</p>

          <p style="color: #666">Rendered on: <span jwcid="insertTime"/>.</p>
        </div>
      </span>

    </div>
  </span>
</div>
          
        

Page specification

          
<component id="foreachItem" type="tacos:PartialFor">
  <binding name="source" value="items"/>
  <binding name="value" value="currItem"/>
  <binding name="evenOdd" value="ognl:page.beans.evenOdd" />
</component>
          
        

Java sources

          
public abstract class PartialPrototype extends BasePage {

  public static final DateFormat DATE_FORMAT = 
         DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG);

  public List getItems() {
      Set countries = new HashSet();

      Locale[] locales = Locale.getAvailableLocales();
      List items = new ArrayList(locales.length);
      for (int i = 0; i < locales.length; i++) {
          Locale l = locales[i];
          if (!"".equals(l.getDisplayCountry()) 
              && !countries.contains(l.getCountry())) {
              items.add(l);
              countries.add(l.getCountry());
          }
      }
      Collections.sort(items, new Comparator() {
          public int compare(Object o1, Object o2) {
              Locale l1 = (Locale) o1;
              Locale l2 = (Locale) o2;
              return l1.getDisplayCountry().compareTo(l2.getCountry());
          }

      });
      return items;
  }

  public void toggleItem(IRequestCycle cycle) {
      String item = (String) cycle.getListenerParameters()[0];
      Set s = getSelectedItems();
      if (s.contains(item)) {
          s.remove(item);
      } else {
          s.add(item);
      }
      setSelectedItems(s);
      AjaxWebRequest ajax =
          (AjaxWebRequest)cycle.getAttribute(AjaxWebRequest.AJAX_REQUEST);
      if (ajax != null) ajax.addStatusResponse("File selected..");
  }

  public boolean isCurrSelected() {
      return getSelectedItems().contains(getCurrItem().toString());
  }

  public abstract Locale getCurrItem();

  public abstract Set getSelectedItems();

  public abstract void setSelectedItems(Set selectedItems);

}