JSF Primefaces数据表行选择错误

时间:2013-02-21 17:50:20

标签: jsf primefaces

我正在使用使用primefaces的数据表,我想选择一个特定的行和显示日期,但我面临的问题是,当我选择任何行时,它只显示第一行数据而不是选择..这是代码

view.xhtml:

 <h:form prependId="false" id="form">  

<p:dataTable id="dataTable" var="car" value="#{tableBean.getList()}"  
             paginator="true" rows="10"  selectionMode="single" selection="#{tableBean.selectedCar}" rowKey="#{car.model}" 
             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
             rowsPerPageTemplate="5,10,15">  
    <f:facet name="header">  
        Posted Jobs  
    </f:facet>  

    <p:column>  
        <f:facet name="header">  
            <h:outputText value="Model" />  
        </f:facet>  
        <h:outputText value="#{car.model}" />  
    </p:column>  

    <p:column>  
        <f:facet name="header">  
            <h:outputText value="Year" />  
        </f:facet>  
        <h:outputText value="#{car.year}" />  
    </p:column>  

    <p:column>  
        <f:facet name="header">  
            <h:outputText value="Manufacturer" />  
        </f:facet>  
        <h:outputText value="#{car.manufacturer}" />  
    </p:column>  

    <p:column>  
        <f:facet name="header">  
            <h:outputText value="Color" />  
        </f:facet>  
        <h:outputText value="#{car.color}" />  
    </p:column>  
      <f:facet name="footer">  
        <p:commandButton id="viewButton" value="View" icon="ui-icon-search"  
               update=":form:display" oncomplete="carDialog.show()"    actionListener="#{tableBean.add}"
                />  
    </f:facet>

</p:dataTable>  
 <p:dialog id="dialog" header="Car Detail" widgetVar="carDialog" resizable="false"  
          width="200" showEffect="clip" hideEffect="fold">  

    <h:panelGrid id="display" columns="2" cellpadding="4">  



        <h:outputText value="Model:" />  
        <h:outputText value="#{tableBean.selectedCar.model}" />  

        <h:outputText value="Year:" />  
        <h:outputText value="#{tableBean.selectedCar.year}" />  

        <h:outputText value="Manufacturer:" />  
        <h:outputText value="#{tableBean.selectedCar.manufacturer}" />  

        <h:outputText value="Color:" />  
        <h:outputText value="#{tableBean.selectedCar.color}" />  
    </h:panelGrid>  
</p:dialog>  

  </h:form>  

Bean类:

package Bean;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
   @ManagedBean

    public class TableBean implements Serializable {


private static final long serialVersionUID = 1L;
private Cars selectedCar; 
public Cars getSelectedCar() {
    return selectedCar;
}
public void setSelectedCar(Cars selectedCar) {
    this.selectedCar = selectedCar;
}
public static long getSerialversionuid() {
    return serialVersionUID;
}
public List<Cars> getList() {
    List<Cars> Carss = new ArrayList<Cars>();

    Cars c = new Cars();
    c.setColor("sd");
    c.setManufacturer("sd");
    c.setModel("sd");
    c.setYear("1212");
    Cars c1 = new Cars();
    c1.setColor("sd");
    c1.setManufacturer("sd");
    c1.setModel("sd");
    c1.setYear("1000");
    Carss.add(c1);
    Carss.add(c);


    return Carss;

}
public void add(){

    System.out.print(getSelectedCar().getYear());

    }
      }

1 个答案:

答案 0 :(得分:0)

应该添加f:setPropertyActionListener

<p:commandButton id="viewButton" value="View" icon="ui-icon-search"  
               update=":form:display" oncomplete="carDialog.show()"    actionListener="#{tableBean.add}"
                >  
         <f:setPropertyActionListener value="#{car}" target="#{tableBean.selectedCar}"/> 
</p:commandButton>

查看http://www.primefaces.org/showcase/ui/datatableRowSelectionByColumn.jsf了解更多示例