如何使用 ajax 访问数据表中的列值?

How to access column values in datatable by using ajax?

我需要你的帮助,通过 ajax 侦听器使用所选行的 onclick 事件来获取列值。

数据表代码为:

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

以及检索列表的代码:

@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();

   }

在我的例子中,我如何访问 onclickbutton 方法中 availableCars datatable 中的对象(employeeCode 和 employeeName)并获取列值并将它们分配给变量。

您可以通过将 p:datatable 的迭代器变量“car”传递给方法 CarsView.onclickbutton

来实现此目的

传递迭代器变量“car”如下:

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

您可以在您的方法中访问 "car" 对象,如下所示:

public void onclickbutton(Car car)
{
     System.out.println("Clicked");
     System.out.println("employeeeName ==> "+car.getEmployeeName());
}