简体   繁体   中英

p:commandButton in the second datatable doesn't work

I have two datatables in the same page. The first one is loaded when the page loads. The second one is loaded when a row is clicked. I have a command button in each row. In the first datatable works fine. But, in the second one, the parameter value is null. It only works if I use immediate attribute equals true. Does someone know why?

My page:

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

My bean:

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

The excludeOwner(Owner owner) function works fine, but the excludeCars(Car car) doesn't. I always get null in car value.

Thanks

I use process="@this" in the commandButton and works. Also, I remove update attribute from commandButton and process attribute from ajax. Thanks a lot

Ajax:

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

Command button:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM