0

I'm investigating PrimeFaces recently. I try to create editable DataTable. I take some code from demos and created my own Facelets file and managed bean.

products.xhtml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://xmlns.jcp.org/jsf/html"
        xmlns:f="http://xmlns.jcp.org/jsf/core"
        xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
        xmlns:c="http://java.sun.com/jsp/jstl/core"
        xmlns:p="http://primefaces.org/ui">

    <h:head>
      <!-- some headers -->
    </h:head>

    <h:body>
        <h:form>
            <p:dataTable id="products" var="product" value="#{productsBean.products}" editable="true" style="margin-bottom: 20px; width: 1000px;">
                <f:facet name="header">Products</f:facet>

                <p:ajax event="rowEdit" listener="#{productsBean.onRowEdit}" />
                <p:ajax event="rowEditCancel" listener="#{productsBean.onRowCancel}" />

                <p:column headerText="Nazwa">
                    <p:cellEditor>
                        <f:facet name="output"><h:outputText value="#{product.name}" /></f:facet>
                        <f:facet name="input"><p:inputText value="#{product.name}" style="width:100%" label="Nazwa"/></f:facet>
                    </p:cellEditor>
                </p:column>

                <!-- more columns... -->            

                <p:column style="width:32px">
                    <p:rowEditor  />
                </p:column>

            </p:dataTable>
        </h:form>
    </h:body>
</html>

ProductsBean.java:

    import java.io.Serializable;
    import java.util.List;

    import javax.ejb.EJB;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;

    import org.primefaces.event.RowEditEvent;

    // more imports...

    @SessionScoped @ManagedBean
    public class ProductsBean implements Serializable {

    private static final long serialVersionUID = -501520863695260180L;

    @EJB
    private ProductDao productDao;

    @EJB
    private UnitDao unitDao;

    private List<Product> products;
    private List<Unit> units;

    @PostConstruct
    private void init() {
        products = productDao.findAll();
        units = unitDao.findAll();
    }

    public void onRowEdit(RowEditEvent event) {
        System.out.println("onRowEdit");
    }
    public void onRowCancel(RowEditEvent event) {
        System.out.println("onRowCancel");
    }

    public List<Product> getProducts() {
        return products;
    }

    public List<Unit> getUnits() {
        return units;
    }
}

Unfortunatelly, onRowEdit(RowEditEvent) is never invoked, only my table gets red. I don't get any other feedback. onRowCancel(RowEditEvent) is invoked correctly. What am I doing wrong or what I am missing? Thanks for your help.

Patryk Dobrowolski
  • 1,565
  • 2
  • 13
  • 29
  • Starters mistake #1: performing business logic in getter methods. Please stop doing that and retry. Related: http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times After your IDE generates the getters/setters, never touch them unless you really understand what all happens under the hoods. And put them away in the bottom of the class. – BalusC Jan 20 '15 at 21:06
  • Better? :) What is funny my first approach was to preload all data, but after that I thought JPA will handle this. – Patryk Dobrowolski Jan 20 '15 at 21:14
  • Ok, I'm getting angry. Now init() is not invoked. – Patryk Dobrowolski Jan 20 '15 at 21:17
  • It helps to pay more close attention to the examples. Is the method visible? – BalusC Jan 20 '15 at 21:20
  • Thanks. Very valuable answer. init() works now, there was a problem with publishing to wtp server in Eclipse. Sorry, but I'm looking for an answer for more than hour now. – Patryk Dobrowolski Jan 20 '15 at 21:22
  • And now, do the row events work? That's where this question was all about. – BalusC Jan 20 '15 at 21:23
  • Still nothing. onCancel works onEdit nope – Patryk Dobrowolski Jan 20 '15 at 21:27
  • OK. Model design mistake can now at least be excluded from being the cause. You said that your table gets "red". This suggests a validation error. Do you see any clues in server log, such as missing (validation) faces messages? What do you see if you add `` to the page? – BalusC Jan 20 '15 at 21:32
  • Thanks for `` hint. Now I know what happens. Thanks. – Patryk Dobrowolski Jan 20 '15 at 21:35
  • OK. Is this acceptable as dupe? http://stackoverflow.com/questions/2118656/commandlink-commandbutton-ajax-backing-bean-action-listener-method-not-invoked/ – BalusC Jan 20 '15 at 21:41

0 Answers0