Forum OpenACS Development: Two levels multirow

Collapse
Posted by Iuri Sampaio on
Hi there,

Attempting to run a loop inside another (nested loops), I’ve written 2 template::multirow together with 2 foreach loops. The goal is to fill in an input type text as a series of form elements. (I’m not using ad_form / acs-templating)

I’ve read the documentation about —local and -ulevel, but there isn’t many examples, except for diagram package, which is very hard to visualize and understand its implementation.
https://openacs.org/forums/message-view?message_id=52050
https://openacs.org/api-doc/proc-view?proc=template::multirow&source_p=1
https://openacs.org/forums/message-view?message_id=103734

I noticed diagram pkg has a something similar, but I couldn’t run the package and evaluate it source code.

The chunk of code follows the example below:

  multiple name="rooms"
        div class="box-capacities-detail"
        form
            div class="left-capacities"
              input type="hidden" id=“room_id" name=“room_id" value=“@rooms.id@"
              input type="text" id=“name" name=“name" style="width:80%; float:right;" size="50" value="@rooms.name@" placeholder="Nome do Espaço"
            /div
            if @categories_p@
                multiple name="categories”
                  div class="col-5-capacities"
                    mailto:h3@categories.name@/h3
                    input type="text" id="mailto:cat_@categories.id@" name="mailto:cat_@categories.id@" value=“@categories.value@"
                  /div
                /multiple
            /div
            /if




The problem’s in second multiple block  (i.e. “categories”) to represent a two level data structure, using template::multirow . Perhaps, I shouldn’t even use it at all.

I’ve tried too the following line, but as expected it didn’t workout well.
<input type="text" id="mailto:cat_@rooms.categories.id@" name="mailto:cat_@rooms.categories.id@" value=“@rooms.categories.value@">

Such representation would never work! :) rsrsrs

multiple rooms
@elem.id@
@elem.name@

multiple categories
@elem.cats.id@
@elem.cats.name@
@elem.cats.value@

/multiple

/multiple

Then, simplifying and linearizing the implementation I tried the following line, but it displays only the very last element of the list.
<input type="text" id="mailto:cat_@categories.id@" name="mailto:cat_@categories.id@" value=“@categories.value@">

As a turn around I’ve tried reset the multirow, right in the end of the external foreach statement loop, but it unsets the entire multirow structure.

I’ve tried a third approach writing a unique multirow with all values in one single reach statement, but it did’t work either

elems
  elem1_id elem1_name elem1_cat1_id elem1_cat1_name elem1_cat1_value
  elem1_id elem1_name elem1_cat2_id elem1_cat2_name elem1_cat2_value
  …
  elem1_id elem1_name elem1_catM_id elem1_catM_name elem1_catM_value
  …
  elemN_id elemN_name elemN_cat1_id elemN_cat1_name elemN_cat1_value
  …
  elemN_id elemN_name elemN_catM_id elemN_catM_name elemN_catM_value

I’m pretty sure I’ve done this in the past, but I’ve lost my backups and now I’m stuck trying to figure out a way to implement a data structure as sort of:

How would I implement and represent a two-level multirow in ADP file? Is it even possible?

Best wishes,
Iuri

Collapse
2: Re: Two levels multirow (response to 1)
Posted by Brian Fenton on
Hi Iuri

it's not clear to me what you are trying to achieve exactly, but nested multiple tags is definitely fine to use in OpenACS. Here is a brief discussion about it https://openacs.org/forums/message-view?message_id=28810

If you look at the OpenACS there should be some existing examples of nested multiple tags. CVS.openacs.org is down right now, but I think there should be one in packages/acs-templating/resources/lists/table.adp

Brian

Collapse
3: Re: Two levels multirow (response to 2)
Posted by Iuri Sampaio on
Hi Brian,

I agree my explanation is confusing. :(

In attempt to clarify it, I've pasted the respective ADP and TCL chunks, at pastebin.com: pastebin.com/uKrGHXKR

The goal's to use categories dynamically, associated with input text fields, within a form.

Every loop of the first (i.e. outer) multirow creates a new form and a few fields. Then, every loop of the second (i.e. inner) multirow generates the rest of fields (input text), dynamically dependent on how many categories are in the category tree.

Categories are used to compound the labels, ids and values of the input text form fields.

So far, the source code runs fine, except for one issue: the second (inner) multirow repeats the very last value to all
I'm trying to figure out a way to empty the multirow, on every interaction, without unset the whole multirow datasource,.

Best wishes,
Iuri

Collapse
4: Re: Two levels multirow (response to 3)
Posted by Iuri Sampaio on
One more thing to mention,

The example your provided is quite different. I believe group tag is not necessary in this case. The key is to learn how do I reset the second (inner) multirow, without unset the whole datasource.

In a simple TCL variable it'd be something as in the example I've written bellow with +++

...
foreach id [pa_all_photos_in_album $album_id] {
set info_list [photo_album::photo::get -photo_id $photo_id]

...
multirow append photos $info_list
...

+++ set info_list ""
}

Best wishes,

Collapse
5: Re: Two levels multirow (response to 4)
Posted by Iuri Sampaio on
Brian and community,

I found a solution to the problem. I've added photo_id obtained in the first (outter) multirow, to the second (inner) multirow.

That why I'm able to validate and display the elements of the "inner" multirow depedding on the id matching conditional.

#TCL
...
template::multirow create photos id name thumb

template::multirow create categories_mapped photo_id category_id name value
...

#ADP
...
if categories_mapped.photo_id eq photos.id
...

See the entire example at pastebin.com:
pastebin.com/JxG4Wmhs

The question now is: is there a better approach?

Best wishes,

Collapse
6: Re: Two levels multirow (response to 5)
Posted by Gustaf Neumann on
wouldn't it be easier to extend the "photos" multirow from Tcl by an extra attribute for the categories, which is used in the adp for rendering the matching categories information?
Collapse
7: Re: Two levels multirow (response to 6)
Posted by Iuri Sampaio on
Yes, if I didn't have to add category names, ids and values dynamically, in several input text fields, such as the one below. Because of it, two nested loops are required still. Perhaps, I don't visualize your approach in order to implement it.

I'm going to play a bit more using your tip and burn more neurons out! :)

...
input type="text" id="mailto:cat_@categories_mapped.category_id@" name="mailto:cat_@categories_mapped.category_id@" value="@categories_mapped.value@"
...

Gustaf,
I've tried that one too, as in the sample bellow. categories element as a list of lists with 3 elements each. But then, looping over those elements in the same main & unique loop (foreach statement) would be tough.

template::multirow create photos id name thumb categories

...

foreach category_id [category_tree::get_categories -tree_id $tree_id] {
set value [db_string select_value {
SELECT value
FROM ee_photo_category_map
WHERE category_id = :category_id AND photo_id = :photo_id
} -default ""]
...

multirow append photos $photo_id $photo(caption) $photo(thumb_image_id) {$photo_id $category_id $name $value}

My code just doesn't seem simple and clean. But that's just about a "feeling"

Best wishes,

Collapse
8: Re: Two levels multirow (response to 7)
Posted by Iuri Sampaio on
"mailto" in the string above was a TYPO included automatically by OACS Forum source code

...
input type="text" id="mailto:cat_@categories_mapped.category_id@" name="mailto:cat_@categories_mapped.category_id@" value="@categories_mapped.value@"
...