0
votes

i m using datatable using primefaces and i want to select a particular row and display date but i am facing problem that when i select any row it displays only first row data not that is selected ..here is the code

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 class :

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

1 Answers

0
votes

Should add 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>

Check http://www.primefaces.org/showcase/ui/datatableRowSelectionByColumn.jsf for more examples