2

My DataTable's editMode is "row". So, the user clicks on a pencil icon to start editing a row and then clicks a check-mark icon to confirm and end editing. They do the same for some rows and then click on save button to save all edited rows in one transaction. But, they may click save button before ending editing; in such case I need to stop saving and tell them to end their row editing before save.

The question is how can I find out whether the table is in editing state or not?

My DataTable is something like this:

        <p:dataTable id="myTable"
                     value="#{myBean.dataModel}"
                     editMode="row"
                     editable="true"
                     var="row"
                     rowKey="#{row.id}"
                     rowIndexVar="rowIndex">
            <p:column>
                <p:rowEditor/>
            </p:column>
            <p:column headerText="label">
                <p:cellEditor>
                    <f:facet name="input">
                        <p:inputText value="#{row.label}" />
                    </f:facet>
                    <f:facet name="output">
                        <h:outputText value="#{row.label}"/>
                    </f:facet>
                </p:cellEditor>
            </p:column>
            <p:column headerText="name" >
                <p:cellEditor>
                    <f:facet name="input">
                        <p:inputText value="#{row.name}"/>
                    </f:facet>
                    <f:facet name="output">
                        <h:outputText value="#{row.name}"/>
                    </f:facet>
                </p:cellEditor>
            </p:column>
        </p:dataTable>

By the way, there is a property named editingRow with its accessors isEditingRow and setEditingRow that I expected to be my answer; but, it isn't. It seems that setEditingRow is never called.

I'm using PrimeFaces 6.1 and JSF 2.2.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Mehdi Javan
  • 1,081
  • 6
  • 25

1 Answers1

2

Use the row editing Ajax events rowEdit (when a row is edited), rowEditInit (when a row switches to edit mode) and rowEditCancel (when row edit is cancelled) to keep track of the editing state of your table.

In the simplest form, bean:

private int edits = 0;

public void startEdit(RowEditEvent event) {
  edits++;
}

public void stopEdit(RowEditEvent event) {
  edits--;
}

public boolean isEditing() {
  return edits > 0;
}

Data table:

<p:dataTable ...>
  <p:ajax event="rowEditInit" listener="#{myBean.startEdit}"/>
  <p:ajax event="rowEdit" listener="#{myBean.stopEdit}"/>
  <p:ajax event="rowEditCancel" listener="#{myBean.stopEdit}"/>
</p:dataTable>

If simply counting is not enough for you, you could keep track of the objects that are being edited:

private Set<Object> editObjects = new HashSet<>();

public void startEdit(RowEditEvent event) {
  editObjects.add(event.getObject());
}

public void stopEdit(RowEditEvent event) {
  editObjects.remove(event.getObject());
}

public boolean isEditing() {
  return !editObjects.isEmpty();
}

Please note that RowEditEvent#getObject() returns the row data.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • This idea can't be a solution, because when we have some editing rows in progress and the dataTable is updated, all rows become read-only while no `rowEditCancel` or `rowEdit` event is fired. So, we have not rows being edited and a positive `edits`. – Mehdi Javan Mar 10 '18 at 15:45
  • For example consider we have a dataTable for employees including their net income as a column and there is an inputText for tax rate. When the tax rate changes the data in the dataTable must be updated. – Mehdi Javan Mar 11 '18 at 06:05
  • I've added an object variant to the answer. You could determine the editing state by comparing it with a read only list you need to keep track of. – Jasper de Vries Mar 11 '18 at 10:07
  • Thank you. But, It's the same. When the dataTable is updated `editObjects` may not be empty, while the table in no longer in edit mode. I figured out that this is the root of my problem and created an issue for it in github: https://github.com/primefaces/primefaces/issues/3465 . And of course your effort deserves an upvote. Thank you again. – Mehdi Javan Mar 11 '18 at 12:00