Search code examples
jsfprimefacesdatatableupdatemodel

Primefaces update table from another form


I want refresh my lazytable from another from. I have the following xhtml:

<h:form id="tableform">
    <p:remoteCommand name="updateTable" update="table" />

    <p:dataTable widgetVar="tableWidget" id="table"
        selection="#{personBean.selectedPerson}" selectionMode="single"
        lazy="true" paginator="true" var="person" rowKey="#{person.id}"
        value="#{personBean.personModel}" paginatorPosition="bottom" rows="5"
        paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}">            

        <p:column filterBy="#{person.name}" headerText="Names:"
            sortBy="#{person.name}">
            <h:outputText value="#{person.name}" />
        </p:column>


    </p:dataTable>

    <p:commandButton value="buttonInForm2" update=":tableform" />
    <p:commandButton value="buttonInForm" update="@form" />
</h:form>

<h:form>
    <p:commandButton value="directUpdate" update=":tableform" />
    <p:commandButton value="directUpdate" update=":tableform:table" />
    <p:commandButton value="remoteCommand" oncomplete="updateTable()" />
</h:form>

The remoteCommand (my current solution) and the buttons in the from refresh the table right. But when i use the "directUpdate"- buttons the filtertext of the table disappear. But i dont understand why? (The filter value stays at the background but the text is empty)

I know that i can use widgetVar.filter() on table but than the paginator will be reseted. My solution is the remoteCommand with it all works fine and the table reloads with current page and filter. (Better solutions are welcome)

The question why reset directUpdate the text and an update in the form not?

Thanks for time.


Solution

  • The <p:dataTable> has its own hidden inputs for among others pagination, selection and filtering so that the server side knows the client side state. This way it can prepare and return the same state as it is in the client side.

    If you submit a certain form, only the inputs contained in the particular form will be sent, not from other forms. This is not a JSF specific problem. HTML has always worked that way. The world wide web would otherwise have looked very different.

    With invoking the <p:remoteCommand> from outside the form, you're basically submitting the "right" form, namely the one it is contained in which also covers the data table.

    Do note that you don't need a <p:commandButton> for it per se, just a simple <p:button ... onclick="updateTable(); return false;"> is already sufficient. Even the whole second form is unnecessary for the job.