dev@jsftemplating.java.net

foreach component support

From: Ken Paulsen <Ken.Paulsen_at_Sun.COM>
Date: Tue, 07 Nov 2006 15:29:12 -0800

I have finished an initial implementation of a foreach component. There
are some things it doesn't do the way I feel it should, mostly around
events. The equivalent foreach LayoutElement used during component
creation provides events for "beforeLoop" / "afterLoop" and treats
"encode" / "beforeEncode" / "afterEncode" differently. Please do not
rely on these events for the "component version" of "foreach". I have
to finish implementing this behavior and your code will break if you use
these features.

Events inside components, inside a foreach are fine, though.

Another feature that does *not* exist is "decode behavior" for
components inside a foreach. In other words, you cannot handle actions
(or command events) for any component that is inside a foreach. If you
do, the results are not supported at this time. You may get around this
by using hyperlinks that pass information to the server via QUERY_STRING
parameters.

Attached is an example page that uses the loop inside a table.

Thanks!

Ken


<sun:page>
    <sun:html>
<!beforeCreate

    ######################
    ## CREATE DATA ##
    ######################

    // Row 1
    createMap(result=>$attribute{row1});
    mapPut(map="$attribute{row1}" key="a" value="Row1 Col A");
    mapPut(map="$attribute{row1}" key="b" value="Row1 Col B");
    mapPut(map="$attribute{row1}" key="c" value="Row1 Col C");
    // Row 2
    createMap(result=>$attribute{row2});
    mapPut(map="$attribute{row2}" key="a" value="Row2 Col A");
    mapPut(map="$attribute{row2}" key="b" value="Row2 Col B");
    mapPut(map="$attribute{row2}" key="c" value="Row2 Col C");
    // Row 3
    createMap(result=>$attribute{row3});
    mapPut(map="$attribute{row3}" key="a" value="Row3 Col A");
    mapPut(map="$attribute{row3}" key="b" value="Row3 Col B");
    mapPut(map="$attribute{row3}" key="c" value="Row3 Col C");
    // Row 4
    createMap(result=>$attribute{row4});
    mapPut(map="$attribute{row4}" key="a" value="Row4 Col A");
    mapPut(map="$attribute{row4}" key="b" value="Row4 Col B");
    mapPut(map="$attribute{row4}" key="c" value="Row4 Col C");
    // Row 5
    createMap(result=>$attribute{row5});
    mapPut(map="$attribute{row5}" key="a" value="Row5 Col A");
    mapPut(map="$attribute{row5}" key="b" value="Row5 Col B");
    mapPut(map="$attribute{row5}" key="c" value="Row5 Col C");

    // Create List of Map (List of the rows)
    setPageSessionAttribute(key='listOfRows' value={"$attribute{row1}" "${row2}" "${row3}" "${row4}" "${row5}"} );

    // Row 1 (again, different obj.)
    createMap(result=>$attribute{rowA});
    mapPut(map="$attribute{rowA}" key="selected" value="$boolean{false}");
    mapPut(map="$attribute{rowA}" key="foo" value="Other Obj: A - foo");
    mapPut(map="$attribute{rowA}" key="bar" value="Other Obj: A - bar");
    // Row 2 (again, different obj.)
    createMap(result=>$attribute{rowB});
    mapPut(map="$attribute{rowB}" key="selected" value="$boolean{false}");
    mapPut(map="$attribute{rowB}" key="foo" value="Other Obj: B - foo");
    mapPut(map="$attribute{rowB}" key="bar" value="Other Obj: B - bar");
    // Row 3 (again, different obj.)
    createMap(result=>$attribute{rowC});
    mapPut(map="$attribute{rowC}" key="selected" value="$boolean{false}");
    mapPut(map="$attribute{rowC}" key="foo" value="Other Obj: C - foo");
    mapPut(map="$attribute{rowC}" key="bar" value="Other Obj: C - bar");
    // Row 4 (again, different obj.)
    createMap(result=>$attribute{rowD});
    mapPut(map="$attribute{rowD}" key="selected" value="$boolean{false}");
    mapPut(map="$attribute{rowD}" key="foo" value="Other Obj: D - foo");
    mapPut(map="$attribute{rowD}" key="bar" value="Other Obj: D - bar");
    // Row 5 (again, different obj.)
    createMap(result=>$attribute{rowE});
    mapPut(map="$attribute{rowE}" key="selected" value="$boolean{false}");
    mapPut(map="$attribute{rowE}" key="foo" value="Other Obj: E - foo");
    mapPut(map="$attribute{rowE}" key="bar" value="Other Obj: E - bar");

    // Create List of Map (List of more rows)
    setPageSessionAttribute(key='moreRows' value={"$attribute{rowA}" "${rowB}" "${rowC}", "${rowD}", "${rowE}"});
/>

        <sun:head id="head" />
        <sun:body>
            <sun:form id="form">
                <sun:alert id="alert1" summary="" details="" />
                <sun:table id="basicTable" title="Test Table" deselectMultipleButton="#{true}" selectMultipleButton="#{true}" paginateButton="#{true}" paginationControls="#{true}">
                    <!facet actionsTop>
                        <sun:button text="Delete">
                            <!command
                                getUIComponent(clientId="form:basicTable:rowGroup1", component=>$attribute{trg});
                                getSelectedTableRowKeys(tableRowGroup="${trg}" rowKeys=>$attribute{rowKeys});
                                deleteTableRows(tableRowGroup="${trg}" rowKeys="${rowKeys}");
                                commitTableRowGroup(tableRowGroup="${trg}");
                            />
                        </sun:button>
                    </facet>

                    // NOTE: The {} on the outside of the ""'s create a List of the List(s) of rows
                    <sun:tableRowGroup id="rowGroup1" data={"$pageSession{listOfRows}", "$pageSession{moreRows}"} sourceVar="td" selected="#{td.value.selected}">
                        <sun:tableColumn headerText="Selected" selectId="selCb" id="selcol" sort="#{td.value.selected}">
                            <sun:checkbox id="selCb" value="#{td.value.selected}" />
                        </sun:tableColumn>
                        <sun:tableColumn headerText="Col 1" id="col1">
                            <staticText id="col1St" value="Val: #{td.value.a}" />
                            <event>
                                <!beforeEncode
                                    setAttribute(key="more" value="$attribute{more}A");
                                    setAttribute(key="coolList" value={"$attribute{more}", "b", "c"});
                                />
                            </event>
                            <foreach key="cool" list="#{coolList}">
                                "<br />
                                <sun:hyperlink url="http://www.google.com/search?q=#{cool}" text="Search google for '#{cool}'" />
                            </foreach>
                        </sun:tableColumn>
                        <sun:tableColumn headerText="Col 2" id="col2">
                            <staticText id="col2St" value="#{td.value.foo}" />
                        </sun:tableColumn>
                    </sun:tableRowGroup>
                </sun:table>
            </sun:form>
        </sun:body>
    </sun:html>
</sun:page>