You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Jim Menard <ji...@io.com> on 2004/02/13 15:04:07 UTC

Checkboxes in a Foreach

I have two nested Foreach components that display a grid of photos. Now 
I want to put a checkbox next to each photo. How do I distinguish the 
different checkboxes?

I've read the recent thread "How to select all checkboxes, per group?", 
which ended up suggesting the use of an "id" binding for each checkbox. 
What I don't understand is how my Java code sees the id:

	<component id="photoCheckBox" type="Checkbox">
		<binding name="id" expression="generatePhotoCheckBoxId"/>
		<binding name="selected" expression="photoCheckBox"/>
	</component>

What should the Java code look like, in order to distinguish between 
different check boxes?

	public boolean getPhotoCheckBox() {
		// return WHAT?
	}
	public void setPhotoCheckBox(boolean val) {
		// How do I get the id that tells me which check box this is for?
	}

Jim
-- 
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
"Linux is like a wigwam. No windows, no gates and an apache inside."
     -- Unknown


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Checkboxes in a Foreach

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
On Feb 13, 2004, at 11:54 AM, Jim Menard wrote:
> I did not expect the row, or column to be valid when setChecked was 
> called. I thought the row and column values would be set during the 
> foreach rendering, and then be meaningless when the user submitted the 
> form later.

Render and rewind both do the same thing when it comes to forms in this 
respect.  Makes it confusing in some sense (as seen from recent 
threads), but makes stuff like a grid of checkboxes a real joy!

	Erik


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Checkboxes in a Foreach

Posted by Jim Menard <ji...@io.com>.
Erik,

[snip]
> See the rough example I just cobbled together below.  In my 
> setCheckbox method, I know exactly which checkbox is set.  Now my 
> example doesn't keep the state, all checkboxes are re-rendered as off, 
> but submitting the form tells me this:
[snip]

Thank you for taking the time to code up your example. That should do 
exactly what I want.

I did not expect the row, or column to be valid when setChecked was 
called. I thought the row and column values would be set during the 
foreach rendering, and then be meaningless when the user submitted the 
form later.

Jim
-- 
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
"HTML belongs in email the way vinyl siding belongs on gerbils.  Sure
its cute at first, but its just plain WRONG!!!"
     -- .sig of Carmiac


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


RE: Nested Components [Pethshop/Tapestry for Dummys]

Posted by Petter Måhlén <pe...@elevance.se>.
> Query A: 
> Where is loggedIn set within the Petshop example?
> Within visit.java I could only find isUserLoggedIn.
> I would have assumed to find it somewhere there.....

loggedIn is a page/component level property, meaning that it needs to be
defined in the page/component specification with a <property-specification>
tag. To access something in the Visit object, you need something like
"visit.myProperty", and for a component (as opposed to a page), you may in
fact need something like "page.visit.myProperty". This is translated to a
Java call similar to getPage().getVisit().getMyProperty().

> Query C: Who will implement the method setLoggedIn ?

That is implemented behind the scenes by Tapesty in an enhanced class, if
you have provided a corresponding <property-specification> tag, and if there
is no (non-abstract) method implementation in your Java class.

> 
> setLoggedIn is also not implemented in te Border.java.

Probably because there is no property-specification in Border.jwc?

/ Petter


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Nested Components [Pethshop/Tapestry for Dummys]

Posted by ford prefect <fo...@yahoo.de>.
Hi Folks!
~~~~~~~~

My goal is to learn tapestry this weekend!

It would really help me a lot to finally get started
if you could help me to answer the following queries
that refer to the Pethshop example. 

My first attempt is add a login window within the
Border component that will automatically display a
login window if the visitor is not logged in.

What I did so far:

Step A. Modifying Border.html
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I added the line:

<span jwcid="loginWindow" condition="ognl:!loggedIn"/>

Query A: 
Where is loggedIn set within the Petshop example?
Within visit.java I could only find isUserLoggedIn.
I would have assumed to find it somewhere there.....


Step B. Border.jwc
~~~~~~~~~~~~~~~~~~~

Making loginWindow known....

<component-type type="LoginWindow"
specification-path="LoginWindow.jwc"/>

<component id="loginWindow" type="LoginWindow"/>	

Query B: Will it not find LoginWindow.jwc
automatically?


Step C.  LoginWindow.jwc
~~~~~~~~~~~~~~~~~~~~~~~~

public abstract class LoginWindow extends
BaseComponent implements PageRenderListener
{
	public void pageBeginRender(PageEvent event)
	{
		Visit visit = (Visit) getPage().getEngine()
getVisit();
		if (visit == null)
		{
			setLoggedIn(false);
			setShowBanner(false);
		}
		else
		{
			setLoggedIn(visit.isUserLoggedIn());
			setShowBanner(true);
		}
	}

	
	public abstract void setLoggedIn(boolean b);

}

Query C: Who will implement the method setLoggedIn ?

Step D: LoginWindow.html
~~~~~~~~~~~~~~~~~~~~~~~~

<html stuff/>


Now I do "ant run" and see what happends:

----------------------------------------------------
Method 'public abstract void
org.apache.tapestry.pets.presentation.components.LoginWindow.setLoggedIn(boolean)'
(declared in class
org.apache.tapestry.pets.presentation.components.LoginWindow)
has no implementation in class
org.apache.tapestry.pets.presentation.components.LoginWindow
(or enhanced subclass
org.apache.tapestry.pets.presentation.components.LoginWindow$Enhance_4).

----------------------------------------------------

setLoggedIn is also not implemented in te Border.java.

Strange ...


Thanks in advance for your help/comments....











	
		
Mit schönen Grüßen von Yahoo! Mail - http://mail.yahoo.de

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Checkboxes in a Foreach

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
On Feb 13, 2004, at 10:11 AM, Jim Menard wrote:
>>   The property can either be a pure <property-specification> or a 
>> setter.  Use the property in your getPhotoCheckBox to decide what to 
>> return.
>
> That works for getPhotoCheckBox, but how about setPhotoCheckBox? The 
> setter is called for each checkbox when the form is submitted, right? 
> The loop has already been executed and the page has been rendered. I 
> have no idea which checkboxes have been clicked.
>
> Say the user selects three photos out of ten. setPhotoCheckBox will be 
> called ten times, three times with true and seven with false. Since 
> that is called when the form is submitted, I can't tell which photo 
> goes with which check box.

See the rough example I just cobbled together below.  In my setCheckbox 
method, I know exactly which checkbox is set.  Now my example doesn't 
keep the state, all checkboxes are re-rendered as off, but submitting 
the form tells me this:

1a unchecked
1b unchecked
1c unchecked
2a unchecked
2b unchecked
2c checked
3a unchecked
3b unchecked
3c unchecked


Checkboxes.html:
<html>
   <body>
     <form jwcid="@Form">
     <table>
       <tr>
         <td>&nbsp;</td>
         <td jwcid="@Foreach" source="ognl:columns" element="td" 
value="ognl:column">
           <span jwcid="@Insert" value="ognl:column"/>
         </td>
       </tr>
       <tr jwcid="@Foreach" source="ognl:rows" element="tr" 
value="ognl:row">
         <td><span jwcid="@Insert" value="ognl:row"/></td>
         <td jwcid="@Foreach" source="ognl:columns" element="td" 
value="ognl:column">
           <input jwcid="@Checkbox" selected="ognl:checked"/>
         </td>
       </tr>
     </table>
     <input jwcid="@Submit"/>
     </form>
   </body>
</html>

Checkboxes.page:
<?xml version="1.0"?>
<!DOCTYPE page-specification PUBLIC
   "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
   "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
<page-specification class="playground.Checkboxes" >
</page-specification>


Checkboxes.java:
package playground;

import org.apache.tapestry.html.BasePage;

public class Checkboxes extends BasePage {
   private String row;
   private String column;

   public String[] getColumns() {
     String[] columns = new String[] {"a", "b", "c"};
     return columns;
   }

   public String[] getRows() {
     String[] rows = new String[] {"1", "2", "3"};
     return rows;
   }

   public void setRow(String row) {
     this.row = row;
   }

   public String getRow() {
     return row;
   }

   public void setColumn(String column) {
     this.column = column;
   }

   public String getColumn() {
     return column;
   }


   public boolean getChecked() {
     return false;
   }

   public void setChecked(boolean checked) {
     System.out.println(row + column + " " + (checked ? "checked" : 
"unchecked"));
   }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Checkboxes in a Foreach

Posted by Jim Menard <ji...@io.com>.
Erik,

> In your @Foreach loop, use a property to store the current iteration 
> item (value="ognl:item").

I'm already doing that. The inner loop stores the photo object.

>   The property can either be a pure <property-specification> or a 
> setter.  Use the property in your getPhotoCheckBox to decide what to 
> return.

That works for getPhotoCheckBox, but how about setPhotoCheckBox? The 
setter is called for each checkbox when the form is submitted, right? 
The loop has already been executed and the page has been rendered. I 
have no idea which checkboxes have been clicked.

Say the user selects three photos out of ten. setPhotoCheckBox will be 
called ten times, three times with true and seven with false. Since 
that is called when the form is submitted, I can't tell which photo 
goes with which check box.

Jim
-- 
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
"I invented the term Object-Oriented, and I can tell you I did not have 
C++
in mind." -- Alan Kay


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Checkboxes in a Foreach

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
In your @Foreach loop, use a property to store the current iteration 
item (value="ognl:item").  The property can either be a pure 
<property-specification> or a setter.  Use the property in your 
getPhotoCheckBox to decide what to return.

	Erik



On Feb 13, 2004, at 9:04 AM, Jim Menard wrote:

> I have two nested Foreach components that display a grid of photos. 
> Now I want to put a checkbox next to each photo. How do I distinguish 
> the different checkboxes?
>
> I've read the recent thread "How to select all checkboxes, per 
> group?", which ended up suggesting the use of an "id" binding for each 
> checkbox. What I don't understand is how my Java code sees the id:
>
> 	<component id="photoCheckBox" type="Checkbox">
> 		<binding name="id" expression="generatePhotoCheckBoxId"/>
> 		<binding name="selected" expression="photoCheckBox"/>
> 	</component>
>
> What should the Java code look like, in order to distinguish between 
> different check boxes?
>
> 	public boolean getPhotoCheckBox() {
> 		// return WHAT?
> 	}
> 	public void setPhotoCheckBox(boolean val) {
> 		// How do I get the id that tells me which check box this is for?
> 	}
>
> Jim
> -- 
> Jim Menard, jimm@io.com, http://www.io.com/~jimm/
> "Linux is like a wigwam. No windows, no gates and an apache inside."
>     -- Unknown
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Checkboxes in a Foreach

Posted by Jim Menard <ji...@io.com>.
Bob,

> I'm a newbie, but if I were going to tackle this, I would cobble up a 
> checkbox/picture component that had its own state.  Java would see the 
> ID as an instance variable.

Does it? How? What method do I implement or call to get the id 
attribute of the component?

How about this: each photo gets wrapped along with a boolean (for the 
checkbox). The checkbox's selected attribute calls a method in the 
wrapper object. The form submit can then walk the list of wrappers, 
doing something with the selected photos.

I think that'll do it. Thank you.

Jim
-- 
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
"I can picture in my mind a world without war, a world without hate. And
I can picture us attacking that world, because they'd never expect it."
     -- Jack Handey


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Checkboxes in a Foreach

Posted by Bob Stine <bo...@waltonstine.net>.
I'm a newbie, but if I were going to tackle this, I would cobble up a checkbox/picture component that had its own state.  Java would see the ID as an instance variable.

Jim Menard <ji...@io.com> wrote:I have two nested Foreach components that display a grid of photos. Now 
I want to put a checkbox next to each photo. How do I distinguish the 
different checkboxes?

I've read the recent thread "How to select all checkboxes, per group?", 
which ended up suggesting the use of an "id" binding for each checkbox. 
What I don't understand is how my Java code sees the id:






What should the Java code look like, in order to distinguish between 
different check boxes?

public boolean getPhotoCheckBox() {
// return WHAT?
}
public void setPhotoCheckBox(boolean val) {
// How do I get the id that tells me which check box this is for?
}

Jim
-- 
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
"Linux is like a wigwam. No windows, no gates and an apache inside."
-- Unknown


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org




___
Bob Stine
bob@waltonstine.net 
"Let's roll!"  Todd Beamer, UA flight 93