PageLink Index Radio

PropertySelection
org.apache.tapestry.form.PropertySelection
Color
 

Description
Creates form elements that allow a property of an object to be set from a drop-down list.

Uses a model to map between Java values that will be assigned, and textual labels that will appear in the HTML response.

There are a range property selection models you can use, including StringPropertySelectionModel and EnumPropertySelectionModel. You can also create your own model, as illustrated in the Examples below.

Note THAT complex value Objects need to properly implement the Object.equals() method if the correct item intial item is to be displayed.

Informal parameters are applied to the <select> tag.  To have greater control over the <option> tags, you must use a Select and Option components.

See Also
Select, Option, Radio, RadioGroup, Form, Script
Parameters
Name Type Direction Required Default Description
value Object in-out yes   The property to set. During rendering, this property is read, and sets the default value of the selection (if it is null, no element is selected). When the form is submitted, this property is updated based on the new selection.
model IPropertySelectionModel in yes   The model provides a list of possible labels, and matches those labels against possible values that can be assigned back to the property.
disabled boolean in no false Controls whether the <select> is active or not. A disabled PropertySelection does not update its value parameter. Corresponds to the "disabled" HTML attribute.
submitOnChange boolean in no false If true, then additional JavaScript is added to submit the containing form when select is changed. Equivalent to specifying a JavaScript event handler of this.form.submit().

Body: removed
Informal parameters: allowed
Reserved parameters: "name"

Examples

The PropertySelection component provides Gender selection drop down list using a StringPropertySelectionModel

Gender:

<form jwcid="detailsForm">
 Gender: <span jwcid="selectGender"/>
</form>


<component id="detailsForm" type="Form">
    <binding name="listener" expression="listeners.formSubmit"/>
</component>

<component id="selectGender" type="PropertySelection">
    <binding name="model" expression="@com.mycorp.DetailsPage@GENDER_MODEL"/>
    <binding name="value" expression="gender"/>
</component>


package com.mycorp;

public class DetailsPage extends BasePage {
    public static final IPropertySelectionModel GENDER_MODEL = 
        new StringPropertySelectionModel(new String[] { "Unspecified", "Female", "Male" });

    private String gender;
   
    public String getGender() { return gender;  }

    public void setGender(String value) { 
        gender = value;
    }
   
    public void detach() {
        gender = null;
        super.detach();
    }   

    public void formSubmit(IRequestCycle cycle) {   
        // Process form submission
        String genderSelection = getGender();
        ..
    }
}

Provides list of clothing items for the user to select. When the user selects a cloting item from the list the description the label and price is automatically updated. The list of clothing items would typically be loaded from a database.

This example uses the component's submitOnChange property to automatically submit the form when a HTML <select> onchange event occurs. The page's clothing item property is then updated and the new cloting item information is displayed by the description, label and price Insert components.

Item:

Description: Cotton full length Summer dress

Label: CountryClub

Price: $89.95

<form jwcid="purchageForm">
 Item: <span jwcid="selectItem"/>
 <p>
 Description: <span jwcid="insertDescription"/>
 <p>
 Label: <span jwcid="insertLabel"/>
 <p>
 Price: $<span jwcid="insertPrice"/>
</form>


<component id="purchaseForm" type="Form">
    <binding name="listener" expression="listeners.formSubmit"/>
</component>

<component id="selectItem" type="PropertySelection">
    <binding name="model" expression="itemSelectionModel"/>
    <binding name="value" expression="clothingItem"/>
    <binding name="submitOnChange" expression="true"/>
</component>

<component id="insertDescription" type="Insert">
    <binding name="value" expression="clothingItem.description"/>
</component>

<component id="insertLabel" type="Insert">
    <binding name="value" expression="clothingItem.label"/>
</component>

<component id="insertPrice" type="Insert">
    <binding name="value" expression="clothingItem.price"/>
</component>


public class PurchagePage extends BasePage {
    private String clothingItem;
    private ItemSelectionModel itemSelectionModel;

    public Item getClothingItem() { return clothingItem;  }

    public void setClothingItem(Item value) { 
        clothingItem = value;  
        fireObservedChange("clothingItem", value);
    }

    public ItemSelectionModel getItemSelectionModel() { 
        return itemSelectionModel;
    } 

    public ItemSelectionModel setItemSelectionModel(ItemSelectionModel value) { 
        itemSelectionModel = value;
        fireObservedChange("itemSelectionModel", value);
    }

    public void detach() {
        clothingItem = null;
        itemSelectionModel = null;
        super.detach();
    }

    public void formSubmit(IRequestCycle cycle) {
        // Process form submission 
    }   
}

public class Item implements Serializable {
    private Integer id;	 
    private String name;
    private String description;
    private String label;
    private String price;

    public Item(Integer id, String name, String desc, String label, String price) { 
        this.id = id; 
        this.name = name;
        this.description = desc;
        this.label = label;
        this.price = price;
    }

    public int getId() { return id;  }

    public String getName() { return name;  }

    public int getDescription() { return description;  }

    public int getLabel() { return label;  }   
   
    public int getPrice() { return price;  }   
}

public class ItemSelectionModel implements IPropertySelectionModel, Serializable {
    private List itemList;

    public ItemPropertySelectionModel(List itemList) { 
        this.itemList = itemList; 
    }  

    public int getOptionCount() { return itemList.size(); }

    public Object getOption(int index) {
        return itemList.get(index)
    }

    public String getLabel(int index) {
        return ((Item) itemList.get(index)).getName();
    }

    public String getValue(int index) { Integer.toString(index); }

    public Object translateValue(String value) {
        return getOption(Integer.parseInt(value));
    }
}

PageLink Index Radio