我有一个具有此属性的对象:Id,名称和状态,我有一个此对象的List。我想保存每个元素的状态(启用或禁用)。
答案 0 :(得分:2)
You can use SelectManyCheckbox Primefaces Component.
<p:selectManyCheckbox id="checkboxTest" value="#{myBean.selectedElements}">
<f:selectItems value="#{myBean.myElements}" var="elem" itemLabel="#{elem.value}" itemValue="#{elem.id}" />
</p:selectManyCheckbox>
You need to create in your backing bean a list that will be filled by the selected element (selectedElements in the example above) and use your list of object (myElements) to create the checkbox on the page. In this way on submit you will have the "selectedElements" list filled with the checked items.
See more:
答案 1 :(得分:2)
这是一个通用示例(使用<h:dataTable...>
):
<强> XHTML:强>
<h:form>
<h:dataTable var="row" value="#{categoryMan.items}">
<h:column>
<h:selectBooleanCheckbox value="#{row.enabled}">
</h:selectBooleanCheckbox>
</h:column>
<h:column>
<h:outputText value="#{row.id}"></h:outputText>
</h:column>
<h:column>
<h:outputText value="#{row.name}"></h:outputText>
<f:facet name="footer">
<h:commandButton action="#{categoryMan.save}" value="Save">
</h:commandButton>
</f:facet>
</h:column>
</h:dataTable>
</h:form>
<强> YourBean:强>
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="categoryMan")
@SessionScoped
public class CategoryManager implements Serializable {
private static final long serialVersionUID = -8453216983786165042L;
private List<Category> items;
public CategoryManager() {
}
@PostConstruct
private void init(){
try{
this.items = new ArrayList<Category>();
this.items.add(new Category("PS2001", "JSF", false));
this.items.add(new Category("PS2002", "ASP", true));
this.items.add(new Category("PS2002", "PHP", false));
}catch(Exception e){
e.printStackTrace();
}
}
public String save() {
for(Category cat: this.items){
System.out.println(cat.getName()+": "+cat.isEnabled());
}
return "yourNavigationRule";
}
public List<Category> getItems() {
return items;
}
public void setItems(List<Category> items) {
this.items = items;
}
}
您的对象:
import java.io.Serializable;
public class Category implements Serializable{
private static final long serialVersionUID = -8070175380194294502L;
private String id;
private String name;
private boolean enabled;
public Category(String id, String name, boolean enabled) {
super();
this.id = id;
this.name = name;
this.enabled = enabled;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
答案 2 :(得分:1)
这就是我如何显示具有特定问题的动态复选框列表。
<p:row styleClass="ui-panelgrid-cell" rowspan="3">
<p:column>
<div style="overflow: auto; width: 100%;">
<p:dataTable var="tQuestions"
value="#{userPreferences.availableQuestions}"
emptyMessage="No Questions Found." id="sTable">
<p:column>
<h:outputText id="sName" styleClass="cellLabelMand"
value="#{tQuestions.shortText}" />
<h:outputText value="<br/><br/>" escape="false" />
<ui:repeat var="tCategoryList" value="#{tQuestions.categoryList}">
<p:selectBooleanCheckbox value="checked" />
<h:outputText value="#{tCategoryList.categoryValue}" />
</ui:repeat>
</p:column>
</p:dataTable>
</div>
</p:column>
</p:row>