3

I have an input field and a table on page. Whenever a number in a field changes (ajax listener to change event) - I want number of rows to change accordingly.

I pointed the table to an Arraylist in a Backing bean, and play with its size.

But then when I edit a value in the table - and press enter, the table disappears, and reappears only when I change field value again.

As a bonus, in debug mode, I see that backing arraylist always have empty values.

Here goes the Bean code:

@ManagedBean(name = "bean")
@ViewScoped
public class TestBean {

    private List<String> hostnames = new ArrayList<String>();

    private int copiesQuantityJustCopy;

    public int getCopiesQuantityJustCopy() {
        return copiesQuantityJustCopy;
    }

    public void setCopiesQuantityJustCopy(int copiesQuantityJustCopy) {
        this.copiesQuantityJustCopy = copiesQuantityJustCopy;
    }

    public List<String> getHostnames() {
        return hostnames;
    }

    public void setHostnames(List<String> hostnames) {
        this.hostnames = hostnames;
    }

    public void onUpdateCount() {
        int hostnamesCount = hostnames.size();
        System.out.println("delta = " + hostnamesCount + " - " + copiesQuantityJustCopy);
        int delta = hostnamesCount - copiesQuantityJustCopy;
        if (delta > 0)
            for (int i = 1; i < delta; i++)
                hostnames.remove(delta);
        if (delta < 0)
            for (int i = 0; i < -delta; i++)
                hostnames.add("");
    }
}

and the view code:

<h:form id="form1">
    <p:inputMask mask="9?99" maxlength="3" placeHolder=" " value="#{bean.copiesQuantityJustCopy}">
        <p:ajax event="change" listener="#{bean.onUpdateCount()}" update=":form1:hostnamesTable" />
    </p:inputMask>

    <p:outputPanel id="hostnamesTable" rendered="true">
        <p:dataTable value="#{bean.hostnames}" var="hostname" 
            id="hostnames" editable="true" editMode="cell">

            <p:ajax event="cellEdit" update=":form1:hostnamesTable" process="@this" />

            <p:column>
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{hostname}" />
                    </f:facet>

                    <f:facet name="input">
                        <p:inputText value="#{hostname}" style="width:96%" />
                    </f:facet>
                </p:cellEditor>
            </p:column>

        </p:dataTable>
    </p:outputPanel>
</h:form>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Anton
  • 1,409
  • 1
  • 19
  • 37
  • 1
    "*Disappear*" means what in this context? Does the table become empty or it vanishes itself from the HTML DOM tree? You also need to populate the list in a method annotated with `@PostConstruct`, for example to get the list to be initialized in the beginning, when the page is loaded and the bean is instantiated. (A view scoped bean also needs the `java.io.Serializable` interface to be implemented along with a private static final long field `serialVersionUid`). – Tiny Apr 12 '15 at 13:32
  • 1
    And check for the correct Viewscoped package in combination with ManagedBean – Kukeltje Apr 12 '15 at 14:02
  • @Tiny Hi! I've tried: 1) implementing the serializable: no effect on behavior; 2) looking into developer tools inspector: seeing only div by this name, nothing more:
    ,
    – Anton Apr 13 '15 at 07:20
  • @Kukeltje Hi! Both of them are javax.faces.bean.*. – Anton Apr 13 '15 at 07:21
  • This was solved in another thread. https://stackoverflow.com/questions/19548838/updating-entire-pdatatable-on-complete-of-pajax-event-celledit – Nathan Stanley Mar 13 '18 at 09:43
  • @NathanStanley I've seen this answer, one of it's other answers helped me than to make a workaround. Thanks for helping! – Anton Mar 13 '18 at 09:48

1 Answers1

3

I think my answer won't help @Anton, but it may be helpful to another "stackers" facing same problem.

My problem is similar (dataTable dissapears when cellEdit event is completed). In my case the cause was the table update performed by ajax. Since I realy don't need table update after listener firied I simply removed update attribute and now everything works good to me.

Bender
  • 617
  • 6
  • 17
  • Can you please add the 'non-working' code to the answer AND the working code... In [mcve] flavour (and only the xhtml in this case). And it **IS** btw exactely what the OP does as well, so it might help him. And can you post PrimeFaces version info in which this was encountered – Kukeltje May 05 '17 at 08:51
  • In my case (same problem) I've already guessed it must be some update problem. Complex css grid ui embedded in one form with p:autoupdate plus other update triggers for the form. The problem disappeared when I moved the component containing the celleditors to its own form without p:autoupdate and with just one form update from a dialog button (dialog also in its own form). I also tell other developers since years ago and regularly to don't use the big form but smaller forms :-) – Gunnar Mar 24 '22 at 12:28