Peter Penzov
Peter Penzov

Reputation: 1626

Create edit form for Primefaces table

I want to create edit form for Primefaces table. I tested this code:

<h:form id="form">
    <p:dataTable id="tbl" var="systemuser" value="#{systemusers.systemUsers}"                                 
                 selectionMode="single" selection="#{systemusers.selectedSystemUser}" rowKey="#{systemuser.username}" lazy="true"
                 resizableColumns="true"
                 >

        <p:ajax event="rowSelect" listener="#{systemusers.onRowSelect}" update=":form:carDetail" oncomplete="PF('carDialog').show()" />
        ....................
    </p:dataTable>

    <p:dialog id="wd" header="System User Details" widgetVar="carDialog" modal="true" showEffect="fade" hideEffect="fade" resizable="true">
        <p:outputPanel id="carDetail" style="text-align:center;">
            <p:panelGrid  columns="2"  columnClasses="label,value">

                <h:outputLabel for="id" value="ID" />
                <p:outputLabel id="id" value="#{systemusers.selectedSystemUser.id}" rendered="#{not systemusers.editable}"/>
                <h:inputText value="#{systemusers.selectedSystemUser.id}" rendered="#{systemusers.editable}" />
                ........
            </p:panelGrid>
        </p:outputPanel>

        <f:facet name="footer">
            <p:commandButton value="Edit System User" rendered="#{not systemusers.editable}"
                             actionListener="#{systemusers.editRecord(false)}">
                <f:ajax render=":form:wd" execute=":form:wd"></f:ajax>
            </p:commandButton>&nbsp;

            <p:commandButton value="Cancel" rendered="#{systemusers.editable}" actionListener="#{systemusers.editRecord(true)}">
                <f:ajax render=":form" execute=":form"></f:ajax>
            </p:commandButton>
        </f:facet>
    </p:dialog>

    <p:contextMenu for="tbl">
        <p:menuitem value="View" update="carDetail" icon="fa fa-search" oncomplete="PF('carDialog').show()"/>
        <p:menuitem value="Delete" update="tbl" icon="fa fa-remove" actionListener="#{systemusers.deleteSystemUser}">
            <p:confirm header="Confirmation" message="Are you sure?" icon="fa fa-warning" />
        </p:menuitem>
    </p:contextMenu>

    <p:confirmDialog global="true" showEffect="fade" hideEffect="fade">
        <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="fa fa-check" />
        <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="fa fa-remove" />
    </p:confirmDialog>
</h:form>

Managed bean:

@Named
@RequestScoped
public class Systemusers implements Serializable
{
.......
public void editRecord(boolean editable)
    {
        SessionFactory factory = HibernateUtils.getSessionFactory();
        Session session = factory.getCurrentSession();

        try
        {
            session.getTransaction().begin();

            SystemUsersModel obj = new SystemUsersModel();
            obj.setId(selectedSystemUser.getId());
            ..........
            session.update(obj);

            session.getTransaction().commit();

            this.editable = editable;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
    }
......
private boolean editable = false;

    public boolean isEditable()
    {
        return editable;
    }

    public void setEditable(boolean editable)
    {
        this.editable = editable;
    }
}

When I open the Edit form and I click button Edit System User the form disappears. Probably because I render and execute it. How I can execute the form and under it without closing it?

Upvotes: 4

Views: 1216

Answers (1)

Kukeltje
Kukeltje

Reputation: 12337

The example code is bad in multiple ways. Two things stand out:

  • It is not an [mcve]
  • <f:ajax render=":form" execute=":form"> is superfluous or might even cause the weird behaviour

OP most likely does not know that (quote from the PrimeFaces showcase , emphasis mine)

CommandButton

CommandButton extends the standard h:commandButton with ajax, partial processing and skinning features.

So the commandBotton is already ajax anabled and does not need to hava a

<f:ajax render=":form" execute=":form">

inside it. This (most likely) makes an the update run twice. One @none (Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes) and one updating the form. The second one most likely does not have any content. But trying (and debugging) will make things clear. If this is not the answer, the reason cannot be seen in the code and hence it should have been a [mcve]

Upvotes: 1

Related Questions