XHTML
<h:form> <p:dataTable var="catalog" value="#{radioView.catalogs}"> <p:column headerText="City"> <p:selectOneRadio id="city" value="#{catalog.city}" columns="3"> <f:selectItems value="#{radioView.cities}" var="c" itemLabel="#{city}" itemValue="#{city}"/> </p:selectOneRadio> </p:column> </p:dataTable> <p:commandButton value="changeSelection" process="@form" update="@form" actionListener="#{radioView.changeSelection}"/> <p:commandButton value="submit" process="@form" update="@form" actionListener="#{radioView.submit}"/> </h:form>
ManagedBean
@ManagedBean
public class RadioView {
private List<Catalog> catalogs;
private List<String> cities;
@PostConstruct
public void init() {
cities = new ArrayList<String>();
cities.add("San Francisco");
cities.add("London");
cities.add("Paris");
//default radio value
Catalog c1 = new Catalog("San Francisco");
Catalog c2 = new Catalog("London");
Catalog c3 = new Catalog("Paris");
Catalog c4 = new Catalog("London");
catalogs = new ArrayList<Catalog>();
catalogs.add(c1);
catalogs.add(c2);
catalogs.add(c3);
catalogs.add(c4);
}
public List<Catalog> getCatalogs() {
return catalogs;
}
public void setCatalogs(List<Catalog> catalogs) {
this.catalogs = catalogs;
}
public List<String> getCities() {
return cities;
}
public void changeSelection(ActionEvent event){
for (Catalog catalog : catalogs) {
catalog.setCity("San Francisco");
}
}
public void submit(ActionEvent event) {
for (Catalog catalog : catalogs) {
System.out.println(catalog.getCity());
}
}
}
Domain
public class Catalog implements Serializable{
private String city;
public Catalog(String city){
this.city = city;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
No comments:
Post a Comment