Updating row on complete of <p:ajax event="cellEdit">

5.4k views Asked by At

I want to update cellValue in same row. I followed the sugestions of BalusC Updating entire <p:dataTable> on complete of <p:ajax event="cellEdit">

After all my code perform 2 Requests. The secound one makes a full page Reload and all data will reseted.

I also tried <p:remoteCommand name="updateTable" process="@this" update="kAbnrTbl" /> following this suggestion. Using a p:remoteCommand to update a p:dataTable

Here is JSF Page:

<h:form id="kalkEditForm">
        <p:outputPanel id="dropArea">
            <p:remoteCommand name="updateTable" update="kAbnrTbl" />
            <p:dataTable id="kAbnrTbl" value="#{tableBean.data}" var="data" editable="true" editMode="cell">

                <p:ajax event="cellEdit"  listener="#{tableBean.onCellEdit}" oncomplete="updateTable()"/> 

                <p:column headerText="Col1">  
                    <p:cellEditor>
                        <f:facet name="output"><h:outputText value="#{data.col1}" /></f:facet>
                        <f:facet name="input"><p:inputText value="#{data.col1}" /></f:facet>
                    </p:cellEditor> 
                    </p:column>

                <p:column headerText="Col2">
                    <h:outputText value="#{data.col2}" />
                </p:column>
           </p:dataTable>   
        </p:outputPanel>
</h:form>

And here Bean:

    @ManagedBean(name="tableBean")
    @ViewScoped
    public class TableBean {

    public TableBean() {
        RowData entry = new RowData("a1", "b1");
        entries.add(entry);
        entry = new RowData("a2", "b2");
        entries.add(entry);
        entry = new RowData("a3", "b3");
        entries.add(entry);
    }
public class RowData {

    private String col1;
    private String col2;

    public RowData(String col1, String col2) {
        this.col1 = col1;
        this.col2 = col2;
    }

    public String getCol1() {
        return col1;
    }

    public void setCol1(String col1) {
        this.col1 = col1;
    }

    public String getCol2() {
        return col2;
    }

    public void setCol2(String col2) {
        this.col2 = col2;
    }
}

private final ArrayList<RowData> entries = new ArrayList<RowData>();

public List<RowData> getData() {
    return entries;
}

public void onCellEdit(CellEditEvent event) {
    final DataTable dataTable = (DataTable)event.getComponent();
    RowData data = (RowData) dataTable.getRowData();
    data.setCol2("changed");
  }

}

I have no idea what is wrong with the code. Why perform <p:remoteCommand ... the second Request.

Using:Primface 5.3

Back to the beginning point. If I don't use <p:remoteCommand name="updateTable" update="kAbnrTbl" /> hack, it works ok but I have to press the refreshButton. If I use the hack I have 2 Requests and a full page reload. There must be a tiny typo or something that I overlook.

Here the code without the hack.

<h:form id="kalkEditForm">
        <p:outputPanel id="dropArea">
            <!-- <p:remoteCommand name="updateTable" update="kAbnrTbl" /> -->
            <p:dataTable id="kAbnrTbl" value="#{tableBean.data}" var="data" editable="true" editMode="cell">
                <p:ajax event="cellEdit"  listener="#{tableBean.onCellEdit}" update="kAbnrTbl"/>
                <!-- <p:ajax event="cellEdit"  listener="#{tableBean.onCellEdit}" oncomplete="updateTable()"/> -->
                <p:column headerText="Col1">
                    <p:cellEditor>
                        <f:facet name="output"><h:outputText value="#{data.col1}" /></f:facet>
                        <f:facet name="input"><p:inputText value="#{data.col1}" /></f:facet>
                    </p:cellEditor>  
                </p:column>
                <p:column headerText="Col2">
                    <h:outputText value="#{data.col2}" />
                </p:column>
           </p:dataTable>   
        </p:outputPanel>
         <p:commandButton id="refreshButton" value="Redisplay" update="kAbnrTbl" />
    </h:form>
2

There are 2 answers

0
inog On BEST ANSWER

Following the note from user3821232, the workaround is to be used instead of "celleditor" "inplace" in conjunction with a standard <p:ajax.. call.

Here you can see the workaround.

xthml:

<h:form id="kalkEditForm">
        <p:outputPanel id="dropArea">
            <p:dataTable id="kAbnrTbl" value="#{tableBean.data}" var="data" editable="true" editMode="cell" rowIndexVar="row">
                <p:column headerText="Col1" id="col1" sortBy="#{data.col1}" sortable="true">
                    <p:inplace>
                        <f:facet name="output"><h:outputText value="#{data.col1}" /></f:facet>
                        <f:facet name="input">
                            <p:inputText value="#{data.col1}" > 
                                <p:ajax event="blur" process="@this" listener="#{tableBean.updateData(row, 'col1')}" update="kAbnrTbl"/>
                            </p:inputText>  
                        </f:facet>
                    </p:inplace>
                </p:column>
                <p:column headerText="Col2">
                    <h:outputText id="col2" value="#{data.col2}" />
                </p:column>
           </p:dataTable>   
        </p:outputPanel>
    </h:form>

Bean:

@ManagedBean(name="tableBean")
@ViewScoped
public class TableBean {

private static final Logger logger = Logger.getLogger(TableBean.class);

public TableBean() {
    RowData entry = new RowData("a1", "b1");
    entries.add(entry);
    entry = new RowData("a2", "b2");
    entries.add(entry);
    entry = new RowData("a3", "b3");
    entries.add(entry);
}

public class RowData {
    private String col1;
    private String col2;

    public RowData(String col1, String col2) {
        this.col1 = col1;
        this.col2 = col2;
    }

    public String getCol1() {
        return col1;
    }

    public void setCol1(String col1) {
        this.col1 = col1;
    }

    public String getCol2() {
        return col2;
    }

    public void setCol2(String col2) {
        this.col2 = col2;
    }
}

private final ArrayList<RowData> entries = new ArrayList<RowData>();

public List<RowData> getData() {
    return entries;
}

public void updateData(Integer row, String colName){
    logger.debug(String.format("updateData called row:%d colName %s", row, colName));
    RowData data = getData().get(row);
    data.setCol2("changed");
}

}

6
BrunoLima On

I used event="rowEdit", this worked for me.

<p:ajax event="rowEdit" listener="#{tableBean.onCellEdit}" oncomplete="updateTable()" />