问题
I have a problem with my datatable.
I try to display selected rows of a table but I can't because my selection list is empty even if I check one or more rows.it seems that I have a selectedArticles empty I don't know what part of my code should I change .I m asking for your help please. My jsf page :
<h:body>
<h:form id="form">
<p:growl id="msgs" showDetail="true" />
<p:panel header="Order" style="margin-bottom:10px;">
<h:panelGrid columns="2" cellpadding="5">
<p:outputLabel for="category" value="Category: " />
<p:selectOneMenu id="category" value="#{catBean.category}" var="line" style="width:150px">
<p:ajax listener="#{catBean.onCategoryChange}" update="product" />
<f:selectItem itemLabel="Select Category" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{catBean.myItems}" />
</p:selectOneMenu>
<p:outputLabel for="product" value="" />
<p:dataTable id="product" var="line" value="#{catBean.myProducts}" selection="#{catBean.selectedItems}" style="margin-bottom:0" rowKey="#{line.cdProduct}">
<p:column selectionMode="multiple" style="width:16px;text-align:center"/>
<p:column headerText="Code">
<h:outputText value="#{line.cdProduct}" />
</p:column>
<p:column headerText="title" >
<h:outputText value="#{line.title}" />
</p:column>
<f:facet name="footer">
<p:commandButton process="product" update=":form:multiProductDetail" icon="ui-icon-search" value="View" oncomplete="PF('multiProductDialog').show()" />
</f:facet>
</p:dataTable>
<p:dialog header="Products" widgetVar="multiProductDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="false" width="auto">
<p:outputPanel id="multiProductDetail" style="text-align:center;">
<ui:repeat value="#{catBean.selectedItems}" var="line">
<h:outputText value="#{line.cdProduct} -- #{line.title}" style="display:block"/>
</ui:repeat>
</p:outputPanel>
</p:dialog>
</h:panelGrid>
</p:panel>
</h:form>
</h:body>
</html>
my managedBean contains this attributes
@ManagedBean(name="catBean")
@ViewScoped
public class catBean implements Serializable {
private String cdCategory;
private String category;
private List<SelectItem> myItems;
private List<Product> selectedItems=new ArrayList<Article>();
{geters and setters...}
回答1:
Given that the <p:dataTable value> is filled by a <p:selectOneMenu><p:ajax listener>, and thus the availability of the <p:dataTable value> for selection is dependent on the view scope, you need to make absolutely sure that the bean is also view scoped. Otherwise the <p:dataTable value> would default to null during postback and nothing would be available for selection.
Those annotations are good,
@ManagedBean(name="catBean")
@ViewScoped
public class catBean implements Serializable {
but you need to make sure that you import them both from javax.faces.bean package. If you import the @ViewScoped from javax.faces.view, then the bean will behave like @RequestScoped. The javax.faces.view is only when you use @Named instead of @ManagedBean.
来源:https://stackoverflow.com/questions/25427367/checkbox-in-datatble-using-primefaces5