我有一个req,我有一个数据,有x列。一列包含3个单选按钮。我能够显示。但我的问题是我希望默认选择一个单选按钮。
我在做什么:在加载数据表我正在创建一个变量(我也尝试使用SlectIte列表),但无法获得检查值。
任何人都可以提供一个简单的工作示例吗?
答案 0 :(得分:0)
这是我演示如何在数据表
中默认无线电值的演示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());
}
}
}
域
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;
}
}