RE: Barracuda: 2 Iterations with 1 model

"Christian Cryder" <[email protected]>
Newsgroups gmane.comp.java.enhydra.barracuda.general
Message-ID <[email protected]>
Hi Thuong,

Sure, this is very doable. The real question is how you want to iterate
through the data on the server...

a) if you have 2 loops in your model, like this:

  for (int i=0; i<categories.length(); i++) {
      for (int j=0; j<books.length(); j++) {
          ...
      }
  }

b) if you just have one loop through all the books, which just happen to be
sorted by category, in which case you'd want to subtotal every time the
category changes.

From the way you describe it, I'm guessing you are dealing with scenario b.
If that's the case then you can simply create a piece of markup like this:

<ul>
  <li class="Dir::Iterate_Start.BooksModel Dir::Iterate_Next.BooksModel">
    ...
    <span class="Dir::Get_Data.BooksModel.SomeData">...</span>
    ...
  </li>
  <li class="Dir::ShowSubtotal.BooksModel">
    ...
    <span class="Dir::Get_Data.BooksModel.Subtotal">...</span>
    ...
  </li>
  <li class="Dir::Iterate_Next.BooksModel Dir::Discard">&nbsp;</li>
</ul>

In this case, ShowSubtotal is a custom directive which gets called after
every row has been displayed; you'd only want to actually process it when
its time to show the subtotal. For instance, your model might look like
this:

  class BookModel ... {
      ...
      //control which nodes display
      public boolean processDirective(TemplateDirective td) {
          if (td.getCommand().equals("ShowSubtotal")) {
              return needToShowSubtotal;
          } else {
              return true;
          }
      }

      //provide items by key
      public Object getItem(String key) {
          if (key.equals("Subtotal")) {
              needToShowSubtotal = false;
              ...
          } else if (...) {
              ...
          }
      }
      ...
  }

So, this approach is using a custom directive; if its not time to show the
subtotal, you just return false, and that piece of the DOM gets dropped. The
alternative to using a custom directive like this is to just use a regular
Get_Data directive, and have the getItem() call for that key simply return a
plain old BComponent whose visibility will control whether or not its
visible. This latter approach is slightly less efficient as it leaves the
subtotal row in the DOM (it won't get dropped until its actually rendered).

Does that help? Or have I misunderstood your question?

Christian
----------------------------------------------
Christian Cryder [[email protected]]
Internet Architect, ATMReports.com
Barracuda - http://barracuda.enhydra.org
----------------------------------------------
"Coffee? I could quit anytime, just not today"


> -----Original Message-----
> From: [email protected] [mailto:[email protected]]On
> Behalf Of Thuong Nguyen
> Sent: Thursday, December 05, 2002 8:52 PM
> To: [email protected]
> Subject: Barracuda: 2 Iterations with 1 model
>
>
> Hi,
>
> I wonder if someone has done something similar to what I'm trying to do
> below (or have any ideas on how to do it).
>
> In the HTML below, I'm trying to render a table that includes a row for
> every items (books) and, for each category of items, a "sub-total" row.
> The very last row contains the grand total.  I can have a model
> implementing IterativeModel to iterate over each item.  However, I
> couldn't figure out how to render (handle) the "sub-total" row.  There
> could n number of items per category .. So I can't just have a
> pre-determined number for <tr>'s.
>
> One option I can think of is to have 2 iterations, but still using only
> one model.  I'm not sure that would work ... But that's the idea of what
> I'm trying to do.  However, I would like to see if I can take advantages
> of directives like Veto_If_Null (or other custom directives) ... And to
> hide or display the correct rows.
>
> Hope you know what I'm talking about (or want).  If not, I can explain
> further.
>
> Thanks in advance!
> -tn
>
>
> <table>
> <tr>
> 	<td>Category</td><td>Title</td><td>Quantity</td><td>Price</td>
> </tr>
> <tr>
> 	<td>Sports</td><td>MLB: The
> History</td><td>1</td><td>$10.00</td>
> </tr>
> <tr>
> 	<td>Sports</td><td>The 2003 Seattle
> Mariners</td><td>2</td><td>$29.00</td>
> </tr>
> <!-- sub total row -->
> <tr>
> 	<td colspan=2>Sports total</td><td>3</td><td>$39.00</td>
> </tr>
> <tr>
> 	<td>History</td><td>Planet Hong
> Kong</td><td>1</td><td>$31.00</td>
> </tr>
> <!-- sub total row -->
> <tr>
> 	<td colspan=2>History total</td><td>1</td><td>$31.00</td>
> </tr>
> <!-- grand total row -->
> <tr>
> 	<td colspan=2>Grand total</td><td>4</td><td>$70.00</td>
> </tr>
> </table>
>
> _______________________________________________
> Barracuda mailing list
> [email protected]
> http://www.enhydra.org/mailman/listinfo.cgi/barracuda
> FAQ - http://www.jguru.com/faq/Barracuda

_______________________________________________
Barracuda mailing list
[email protected]
http://www.enhydra.org/mailman/listinfo.cgi/barracuda
FAQ - http://www.jguru.com/faq/Barracuda
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.