第二个数据表中的p:commandButton不起作用

时间:2018-08-14 03:10:52

标签: jsf primefaces datatable

我在同一页面中有两个数据表。页面加载时,第一个加载。单击一行时将加载第二个。每行都有一个命令按钮。在第一个数据表中工作正常。但是,在第二个参数中,参数值为null。仅当我使用即时属性等于true时,它才有效。有人知道为什么吗?

我的页面:

<h:form id="form">

   <p:dataTable id="dtOwner" selectionMode="single"  var="owner" value="#{ownerBean.retornaOwners}"
   selection="#{ownerBean.ownerSelected}" rowKey="#{owner.id_owner}">
        <p:ajax update="form:pgCars" process="@form" event="rowSelect" listener="#{ownerBean.onRowSelect}"  />

        <p:column headerText="Actions">
            <p:commandButton value="Exclude" actionListener="#{ownerBean.excludeOwner(owner)}"></p:commandButton>
        </p:column>
        <p:column headerText="Name">
            <h:outputText value="#{owner.name}"></h:outputText>
        </p:column>
        <p:column headerText="Surname">
            <h:outputText value="#{owner.surname}"></h:outputText>
        </p:column>
    </p:dataTable>

    <p:panelGrid id="pgCars">
    <p:dataTable id="dtCars"  var="car" value="#{ownerBean.ownerSelected.listCars}"
   selection="#{ownerBean.ownerSelected}" rowKey="#{owner.id_owner}">

    <p:column headerText="Actions" >
    <p:commandButton value="Exclude" process="@all" update="form" actionListener="#{ownerBean.excludeCar(car)}"></p:commandButton>
    </p:column>

     <p:column headerText="id_Car" >
        <h:outputText value="#{car.id_car}"></h:outputText>
     </p:column>
     <p:column headerText="id_owner" >
        <h:outputText value="#{car.id_owner}"></h:outputText>
     </p:column>
     <p:column headerText="tipo_carro" >
        <h:outputText value="#{car.tipo_carro}"></h:outputText>
     </p:column>
     <p:column headerText="modelo_carro" >
        <h:outputText value="#{car.modelo_carro}"></h:outputText>
     </p:column>
   </p:dataTable>

    </p:panelGrid>

   </h:form>

我的豆子:

package com.tutorial;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;

@ManagedBean(name="ownerBean")
@SessionScoped
public class OwnerBean {
@ManagedProperty(value = "#{ownerDAO}")
private OwnerDAO ownerDAO;

public void setOwnerDAO(OwnerDAO ownerDAO) {
    this.ownerDAO = ownerDAO;
}

private Owner ownerSelected;

public void excludeOwner(Owner owner) {
    System.out.println("Excluding " + owner.getName());
}

public void excludeCar(Car car) {
    System.out.println("Excluding car " + car.getModelo_carro());
}

public void onRowSelect(SelectEvent event) {
    FacesMessage msg = new FacesMessage("Owner Selected", Integer.toString(   
(   (Owner) event.getObject()).getId_owner()));
    FacesContext.getCurrentInstance().addMessage(null, msg);
    ownerSelected = (Owner) event.getObject();
    ownerSelected.setListCars(new ArrayList<Car>());
    ownerSelected.setListCars(ownerDAO.getCars(ownerSelected));
}

public List<Owner> getRetornaOwners() {
    List<Owner> list = new ArrayList<>();
    try {
        list = ownerDAO.getOwners();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return list;
}


public Owner getOwnerSelected() {
    return ownerSelected;
}


public void setOwnerSelected(Owner ownerSelected) {
    this.ownerSelected = ownerSelected;
}
}

excludeOwner(Owner owner)函数可以正常工作,但excludeCars(Car car)则不能。我总是无法获得汽车价值。

谢谢

1 个答案:

答案 0 :(得分:0)

我在commandButton中使用process =“ @ this”并正常工作。另外,我从commandButton中删除了update属性,并从ajax中删除了process属性。非常感谢

Ajax:

<p:ajax update="form:dtCars" event="rowSelect" listener="#{ownerBean.onRowSelect}"  />  

命令按钮:

<p:commandButton value="Exclude" process="@this" actionListener="#{ownerBean.excludeCar(car)}"></p:commandButton>