You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by nazarhussain_s <na...@gmail.com> on 2011/06/09 12:37:36 UTC

Issue in implementing multiple select dropdown twice in a form

Hi,
      If I am implementing multiple select twice in a form, I am facing an
issue.I am currently following Tapestry in Action - pg 145 example for
multiple select. If I am trying to implement the example in the way given
below i am getting ambiguous output.Anybody help out regarding that.


Home.html
-----------
<html jwcid="@Shell" title="Toppings">
<body jwcid="@Body">

<form jwcid="myForm">

Select toppings:
<select jwcid="@Select" multiple="ognl:true">
  
    <option jwcid="@Option" selected="ognl:toppingSelected"
label="ognl:topping"/>
  
</select>
	
<br/>
Select breads:
<select jwcid="@Select" multiple="ognl:true">
  
    <option jwcid="@Option" selected="ognl:breadSelected"
label="ognl:bread"/>
  
</select>
<input type="submit" value="Select"/>

</form>

<hr/>
	
<p> # Return to Home page .</p>

</body>
</html>



Home.page
-----------
<page-specification class="com.tapestry.Home">
<component id="myForm" type="Form">
<binding name="listener" value="listener:selectToppings" />
<binding name="listener" value="listener:selectBreads" />
</component>
</page-specification>

Home.java
----------
package com.tapestry;

import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.html.BasePage;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.tapestry.IRequestCycle;

public abstract class Home extends BasePage implements
PageBeginRenderListener{
	
	private static String[] TOPPINGS
={"Lettuce","Tomato","Onions","Pickles","Relish","Mustard","Ketchup"};
	private static String[] BREADS 
={"Grains","Oat","Honey","Italian","Mexican","Whole Wheat"};
	
	
	
	public String[] getAllToppings(){
		return TOPPINGS;
	}

	public abstract String getTopping();
	public abstract void setSelectedToppings(ArrayList<String> toppings);
	public abstract ArrayList<String> getSelectedToppings();
	
	public boolean isToppingSelected(){
		return getSelectedToppings().contains(getTopping());
	}
		
	public void setToppingSelected(boolean toppingSelected){
		if(toppingSelected){
			System.out.println("\nbefore adding"+getTopping());
			getSelectedToppings().add(getTopping());
		}
			
		else{
			System.out.println("\nbefore removing"+getTopping());
			getSelectedToppings().remove(getTopping());
		}
			
	}
	
     public void selectToppings(IRequestCycle cycle){
    	 String toppings = getToppingsList();
    	 ToppingsResult page = (ToppingsResult)cycle.getPage("ToppingsResult");
    	 page.setToppings(toppings);
    	 cycle.activate(page);
     }

	private String getToppingsList() {
		if(getSelectedToppings().isEmpty())
			return "No Toppings";
		
		StringBuffer buffer = new StringBuffer();
		int count  = getSelectedToppings().size();
		
		int x=0;
		
		Iterator<String> i = getSelectedToppings().iterator();
		
		while(i.hasNext()){
			if(++x > 1){
				if(x == count)
					buffer.append(" and ");
				else
					buffer.append(", ");
				
			}
			
			String topping = (String)i.next();
			System.out.println("\n topping iterator has "+topping);
			buffer.append(topping);
		}
		 
		buffer.append(".");
		
		return buffer.toString();
		
	}
	
	
	public String[] getAllBreads(){
		return BREADS;
	}
	
	public abstract String getBread();
	public abstract void setSelectedBreads(ArrayList<String> breads);
	public abstract ArrayList<String> getSelectedBreads();
	
	public boolean isBreadSelected(){
		return getSelectedBreads().contains(getBread());
	}
		
	public void setBreadSelected(boolean breadSelected){
		if(breadSelected){
			getSelectedBreads().add(getBread());
		}
		else{
			getSelectedBreads().remove(getBread());
		}
			
	}
	
     public void selectBreads(IRequestCycle cycle){
    	 String breads = getBreadsList();
    	 ToppingsResult page = (ToppingsResult)cycle.getPage("ToppingsResult");
    	 page.setToppings(breads);
    	 cycle.activate(page);
     }

	private String getBreadsList() {
		if(getSelectedBreads().isEmpty())
			return "No Breads";
		
		StringBuffer buffer = new StringBuffer();
		int count  = getSelectedBreads().size();
		
		int x=0;
		
		Iterator<String> i = getSelectedBreads().iterator();
		
		while(i.hasNext()){
			if(++x > 1){
				if(x == count)
					buffer.append(" and ");
				else
					buffer.append(", ");
				
			}
			
			String bread = (String)i.next();
			System.out.println("\nbread iterator has "+bread);
			buffer.append(bread);
		}
		 
		buffer.append(".");
		System.out.println("\n"+buffer.toString());
		return buffer.toString();
		
	}
	
	public void pageBeginRender(PageEvent event){
		setSelectedToppings(new ArrayList<String>());
		setSelectedBreads(new ArrayList<String>());
	}

}


While trying to implement the above example i am getting only the second
time multi selected i.e, options from the bread drop down in the result
page. Can any body help me how to get both the drop downs at same time on
doing multi select?


Regards
Nazar


--
View this message in context: http://tapestry.1045711.n5.nabble.com/Issue-in-implementing-multiple-select-dropdown-twice-in-a-form-tp4472277p4472277.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Issue in implementing multiple select dropdown twice in a form - RESOLVED

Posted by nazarhussain_s <na...@gmail.com>.
Hi,
        I had declared two methods selectToppings , selectBreads with 
Request Cycle parameter passed twice. I combined those two into one method
and set the values in that  after which the app is running fine.seems as the
request was stateless it forgot about the previous values - values of
toppings in the form. some one can clarify better about it.

Regards
Nazar

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Issue-in-implementing-multiple-select-dropdown-twice-in-a-form-tp4472277p4475717.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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