Primefaces <p:cellEditor> editing (more) cells

745 Views Asked by At

In primefaces I have a database with OneToMany relation. I have already made add(Create), search(Read) and Delete, now I want to also make edit(Update) operation.

For that I use primefaces example on primefaces

I pass parameter idPest(identifier) to next page with
<f:param name="idofpest" value="#{zuzek.idPest}" /> and on next page I have

<f:metadata>
    <f:viewParam name="idofpest" value="#{smPestsList.idPest}" />
    <f:viewAction action="#{smPestsList.init}" />
</f:metadata>

        <p:dataTable var="ime" value="#{smPestsList.imena}" editable="true" editMode="cell">
            <p:ajax event="cellEdit" listener="#{smPestsList.onCellEdit}" update=":pestsListEdit:msgs"/>
            <p:column headerText="ID PEST NAME">
                <p:cellEditor>
                    <f:facet name="output"><h:outputText value="#{ime.idPestName}" /></f:facet>
                    <f:facet name="input"><p:inputText id="modelInput" value="#{ime.idPestName}" style="width:100%"/></f:facet>
                </p:cellEditor>
            </p:column>
            <p:column headerText="LANGUAGE">
                <h:outputText value="Français"
                    rendered="#{ime.idLanguage eq 13}" />
                <h:outputText value="Italiano"
                    rendered="#{ime.idLanguage eq 8}" />
                <h:outputText value="English"
                    rendered="#{ime.idLanguage eq 4}" />
                <h:outputText value="Español"
                    rendered="#{ime.idLanguage eq 9}" />
                <h:outputText value="Deutsch"
                    rendered="#{ime.idLanguage eq 11}" />
                <h:outputText value="Magyar"
                    rendered="#{ime.idLanguage eq 10}" />
            </p:column>
            <p:column headerText="DISPLAY NAME">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{ime.name}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{ime.getName()}" style="width:100%"/>
                    </f:facet>
                </p:cellEditor>
            </p:column>
        </p:dataTable>

with bean onCellEdit

     public void onCellEdit(CellEditEvent event) {
        System.out.println(" idpest: " +idPest); // to see if passing parameter(id) is still in memory
        Object oldValue = event.getOldValue();
        Object newValue = event.getNewValue();
       
        if(newValue != null && !newValue.equals(oldValue)) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            System.out.println("Cellchange");

        }
    }

Now I have two problems:

  1. After editing cell backing bean doesn't print "Cellchange" and also after editing if the cell has changed it seems empty until I click on it again
  2. IF I want to edit more than one cell parameter isn't in memory anymore. Now I don't know if cellEditor can update parameters after each edit or should I add Save button with update bean like
    public SmPests update(SmPests pest) {
        em.getTransaction().begin();
        pest = em.merge(pest);
        em.getTransaction().commit();
        return pest;
    }

This is my first working with databases, so if I ask something stupid, please bear with me.

I am using Eclipse, postgresql, Wildfly17 and primefaces 7.0.

bean is in @RequestScoped I tried with other scopes, but I get the WELD-000072: Bean declaring a passivating scope must be passivation capable. error

0

There are 0 best solutions below