0
votes

I'm a bit stuck in PrimeFaces dataTable part. I created a table with rowEditor and sortBy built-in methods, but when I modify a row in the table order of entries are not updated.

In theory, it should work: update=":form:fruits". But it doesn't update the modified row. So I tried to update the whole form: update=":form". But in this case I loose all data from the table except the updated one without any desing.

ScreenShot01 ScreenShot02

Here is a very short sample code to reproduce the problem.

Fruit.java:

public class Fruit {

    private Integer id;
    private String name;

    public Fruit(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Fruit)) {
            return false;
        }
        return id == ((Fruit) o).getId();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Fruits.java:

@ManagedBean(name = "fruits")
@ViewScoped
public class Fruits implements Serializable {

    private static final long serialVersionUID = 1L;

    private ArrayList<Fruit> list = new ArrayList<Fruit>() {
        {
            add(new Fruit(1, "apple"));
            add(new Fruit(2, "orange"));
            add(new Fruit(3, "banana"));
            add(new Fruit(4, "pineapple"));
            add(new Fruit(5, "cocoa"));
        }
    };

    public void onRowEdit(RowEditEvent event) {
        Fruit fruit = (Fruit) event.getObject();
        list.set(list.indexOf(fruit), fruit);
    }

    public ArrayList<Fruit> getList() {
        return list;
    }

    public void setList(ArrayList<Fruit> list) {
        this.list = list;
    }
}

fruits.xhtml:

<h:head />

<h:body>
    <h:form id="form">
        <p:dataTable id="fruits" value="#{fruits.list}" var="fruit"
            editable="true" sortBy="#{fruit.name}">
            <p:ajax event="rowEdit" listener="#{fruits.onRowEdit}" update=":form"></p:ajax>
            <p:column headerText="Name" sortBy="#{fruit.name}">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{fruit.name}" />
                    </f:facet>
                    <f:facet name="input">
                        <h:inputText value="#{fruit.name}" />
                    </f:facet>
                </p:cellEditor>
            </p:column>
            <p:column>
                <p:rowEditor />
            </p:column>
        </p:dataTable>
    </h:form>
</h:body>
</html>

Any ideas how I could fix the problem?

4
I have the same problem, does anybody found a solution?gregor

4 Answers

2
votes

Today I had the same problem and found a workaround: Create a remoteCommand which updates the table and call this remoteCommand as oncomplete action after the cellEdit ajax event.

Snippet:

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

    <p:dataTable id="table" ...>

        <p:ajax event="cellEdit" listener="#{anyClass.onCellEdit}"
            oncomplete="remoteCmd();" />

        <p:column headerText="header">
            values
        </p:column>
    </p:dataTable>
</h:form>
0
votes

try to use @form instead of :form using :form:fruits is the correct form I don't know why it's not working with you.

0
votes

I already tried all of possible keywords (@this, @form, @all), but none of these helped.
Today I added a Refresh button to the end of form. Using this button I can refresh the table as expected. But I don't really undrestand why update=":form"doesn't works with event "rowEdit".

<p:commandButton value="Refresh">
    <p:ajax update=":form"></p:ajax>
</p:commandButton>
0
votes

I have the same or a similar problem in PF 6.1. If I don't sort the datatable by clicking a sortable header, I can edit a cell just fine (I'm using cell edit, not row edit) and all is good. If I sort the table on any of the sortable headers and then edit a cell, the model data is not corrupted but the edited cell displays the value of a seemingly random cell above or below it in the table, as if it is displaying the value the cell would have had if the table not sorted. The other cells in the row are fine. If I click in the cell to re-edit it, the displayed value in the input element is correct; if I then move focus out of the cell, the incorrect value reappears. If I then refresh the page, or do another sort, the values displayed in the table are correct.

I have encountered a scenario where the model data is corrupted but I cannot reproduce it now.

I tried Gregor's workaround with RemoteCommand, no change.

I also have a "Refresh" button which does clear the problem but it's not intuitive to the user do use it for this purpose.

I know this isn't an answer but it may help someone troubleshooting the same problem. Just adding to the knowledgebase.