0

I need your help in getting the column values by using the onclick event of the selected row through ajax listener.

The datatable code is:

    <p:dataTable id="availableCars" var="car" rowKey="#{car}" value="#{CarsView.carDetails}">
        <p:column style="width:20px">
            <h:outputText id="dragIcon" styleClass="ui-icon ui-icon-arrow-4"/>
            <p:draggable for="dragIcon" revert="true" helper="clone"/>
        </p:column>
        <p:column headerText="Id">
            <h:outputText value="#{car.employeeName}"/>
        </p:column>
        <p:column headerText="Id">
            <h:outputText value="#{car.employeeCode}"/>
        </p:column>
        <p:column style="width:32px">
            <p:commandButton icon="ui-icon-search" action="#{CarsView.getDetails}">
                <f:setPropertyActionListener target="#{CarsView.employee}" value="#{car.employeeName}"/>
                <f:setPropertyActionListener target="#{CarsView.code}" value="#{car.employeeCode}"/>
                <p:ajax event="click" listener="#{CarsView.onclickbutton}"/>
            </p:commandButton>
        </p:column>
    </p:dataTable>

And the code for retrieving the List:

@PostConstruct
   public void init() {
       cars= new ArrayList<Car>();
       droppedCars = new ArrayList<Car>();


       Car earn = new Car();

       earn.setEmployeeCode("1111");
       earn.setEmployeeName("James");
       cars.add(earn);

   }


public void onclickbutton(ClickEvent event)
        {
            System.out.println("Clicked");

 DataTable objDataTable = (DataTable) event.getSource();

   }

In my case how can I access the objects(employeeCode and the employeeName) in the availableCars datatable in the onclickbutton method and to get the column values and assign them to variables.

99maas
  • 1,239
  • 12
  • 34
  • 59

1 Answers1

0

You can achieve this by passing iterator variable "car" of p:datatable to method CarsView.onclickbutton

pass iterator variable "car" as following :

<p:commandButton icon="ui-icon-search" action="#{CarsView.getDetails}">
    <f:setPropertyActionListener target="#{CarsView.employee}" value="#{car.employeeName}"/>
    <f:setPropertyActionListener target="#{CarsView.code}" value="#{car.employeeCode}"/>
    <p:ajax event="click" listener="#{CarsView.onclickbutton(car)}"/>
</p:commandButton>

you can access the "car" ojbect in your method as following :

public void onclickbutton(Car car)
{
     System.out.println("Clicked");
     System.out.println("employeeeName ==> "+car.getEmployeeName());
}
Bhavin Panchani
  • 1,332
  • 11
  • 17
  • Thanks for the help. However, it showed me an error: listener="#{CarsView.onclickbutton(car)}" Error Parsing: #{CarsView.onclickbutton(car)} and in JDeveloper it showed me an error that I can't use the iterator variable (car) in the method – 99maas Apr 29 '15 at 21:04
  • I found that #{CarsView.onclickbutton(car)} is not supported. Is there any way that I can access the column values in the onclickbutton? – 99maas Apr 29 '15 at 21:32
  • which version of primefaces are you using ? – Bhavin Panchani Apr 30 '15 at 05:07