You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Marcos Chicote <mc...@newtechnologies.com.ar> on 2007/08/28 16:24:03 UTC

How-to update a selection model based on a prior selection

Hello,

I'm trying to update a selection model based on a prior selection, using
AJAX
and Tapestry 4.1.1.

I've built a small test case, connecting an EventListener to the onchange
event
of a PropertySelection. You can find it at the end of this mail.

In the test, the EventListener is properly called (when the value of the
PropertySelection is changed), but the selections are not updated. When
called
from Test.onChange(), Test.getA() returns 'null' instead of the selected
value.

Somebody knows what should I write in the EventListener to get the
selected value?.

Thank you very much, best regards.



Test.page:
----------
<?xml version="1.0"?>
<!DOCTYPE page-specification PUBLIC
       "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
       "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">

<page-specification
   class="net.nt.web.pages.Test"
>
</page-specification>


Test.html:
----------
<html jwcid="@Shell" title="Test"
       delegate="ognl:new
org.apache.tapestry.components.BlockRenderer(components.remainingHead)"
       renderBaseTag="ognl:false">
<head jwcid="remainingHead@Block">
</head>
<body jwcid="@Body">
       <form jwcid="form@Form">
           <span jwcid="A@PropertySelection" model="ognl:modelA"
value="ognl:a" />
               <span jwcid="B@PropertySelection" model="ognl:modelB"
value="ognl:b" />
       </form>
</body>
</html>


Test.java:
----------

package net.nt.web.pages;

import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.EventListener;
import org.apache.tapestry.event.BrowserEvent;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.form.IPropertySelectionModel;
import org.apache.tapestry.form.StringPropertySelectionModel;
import org.apache.tapestry.html.BasePage;


public abstract class Prueba extends BasePage
                                                       implements
 PageBeginRenderListener {
       public abstract String  getA();
       public abstract void    setA(String selected);

       public abstract String  getB();
       public abstract void    setB(String selected);

       public abstract void setModelB(IPropertySelectionModel modelB);
       public abstract IPropertySelectionModel getModelB();

       public IPropertySelectionModel getModelA() {
               return new StringPropertySelectionModel(new String[] {
"Letters",
                               "Numbers", "Colors" });
       }

       @SuppressWarnings("unchecked")
       public void pageBeginRender(PageEvent event) {
               if (getA() == null) {
                       //set it's inital value to the first value available
in the drop down.
                       System.out.println("Setting A to a default
value...");
                       setA("Letters");
                       setModelB(new StringPropertySelectionModel(new
String[] { "A", "B",
                                       "C", "D", "E" }));
               }
       }

       @EventListener(targets = "A", events = "onchange")
       public void onChange(IRequestCycle cycle, BrowserEvent event) {
               System.out.println("changeA()");

               String selectionA = getA();

               if (selectionA.equals("Letters")) {
                       setModelB(new StringPropertySelectionModel(new
String[] { "A", "B",
                                       "C", "D", "E" }));
               } else if (selectionA.equals("Numbers")) {
                       setModelB(new StringPropertySelectionModel(new
String[] { "1", "2",
                                       "3", "4", "5", "6" }));
               } else {
                       setModelB(new StringPropertySelectionModel(new
String[] { "Red",
                                       "Blue", "Green", "Cyan", "Magenta",
"Yellow" }));
               }

               cycle.getResponseBuilder().updateComponent("B");
       }

}

Re: How-to update a selection model based on a prior selection

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Tue, 28 Aug 2007 11:24:03 -0300, Marcos Chicote  
<mc...@newtechnologies.com.ar> wrote:

> In the test, the EventListener is properly called (when the value of the
> PropertySelection is changed), but the selections are not updated. When
> called from Test.onChange(), Test.getA() returns 'null' instead of the  
> selected
> value.

Try to make A persistent:

@Persist
public abstract String  getA();

Your getA() returns null because the request that renders the form is one  
and the EventListener call is another. ;)
Any value stored in Tapestry class properties, unless explicitly made  
persistent, is lost when the request finishes.

-- 
Thiago H. de Paula Figueiredo
Desenvolvedor, Instrutor e Consultor de Tecnologia
Eteg Tecnologia da Informação Ltda.
http://www.eteg.com.br

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


Re: How-to update a selection model based on a prior selection

Posted by Marcos Chicote <to...@gmail.com>.
Worked great!

Thanks Thiago.

On 8/28/07, Thiago H de Paula Figueiredo <th...@gmail.com> wrote:
>
> On Tue, 28 Aug 2007 11:24:03 -0300, Marcos Chicote
> <mc...@newtechnologies.com.ar> wrote:
>
> > @EventListener(targets = "A", events = "onchange")
> >        public void onChange(IRequestCycle cycle, BrowserEvent event) {
>
> Even better: you need to submit the form when the user selects a different
> value of A, so you know what value was chosen.
> The submitForm property of @EventListener does the trick:
>
> @EventListener(targets = "A", events = "onchange", submitForm="form")
> public void onChange(IRequestCycle cycle, BrowserEvent event) {
>
> Looking at the Javadocs of the latest snapshot, it looks like you won't
> need to specify submitForm anymore.
>
> http://tapestry.apache.org/tapestry4.1/apidocs/org/apache/tapestry/annotations/EventListener.html
>
> --
> Thiago H. de Paula Figueiredo
> Desenvolvedor, Instrutor e Consultor de Tecnologia
> Eteg Tecnologia da Informação Ltda.
> http://www.eteg.com.br
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: How-to update a selection model based on a prior selection

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Tue, 28 Aug 2007 11:24:03 -0300, Marcos Chicote  
<mc...@newtechnologies.com.ar> wrote:

> @EventListener(targets = "A", events = "onchange")
>        public void onChange(IRequestCycle cycle, BrowserEvent event) {

Even better: you need to submit the form when the user selects a different  
value of A, so you know what value was chosen.
The submitForm property of @EventListener does the trick:

@EventListener(targets = "A", events = "onchange", submitForm="form")
public void onChange(IRequestCycle cycle, BrowserEvent event) {

Looking at the Javadocs of the latest snapshot, it looks like you won't  
need to specify submitForm anymore.
http://tapestry.apache.org/tapestry4.1/apidocs/org/apache/tapestry/annotations/EventListener.html

-- 
Thiago H. de Paula Figueiredo
Desenvolvedor, Instrutor e Consultor de Tecnologia
Eteg Tecnologia da Informação Ltda.
http://www.eteg.com.br

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