PrimeFaces DataTable RowEditor updating cell elements on RowEditInit, but not on RowEdit or RowEditCancel

允我心安 提交于 2020-01-06 15:06:31

问题


I have the following xhtml:

<h:form>
...
<p:dataTable id="table" value="#{BEAN.rows} var="row" >
    <p:ajax event="rowEdit" listener="#{BEAN.rowEdit}" />
    <p:ajax event="rowEditInit" listener="#{BEAN.rowEditInit}" />
    <p:ajax event="rowEditCancel" listener="#{BEAN.rowEditCancel}" />
    ...
    <p:columns id="columnsElement"
        value="#{BEAN.columns} var="column" >
        <f:facet name="header" >
            <h:outputText value="#{column.code}" />
            <p:selectBooleanCheckbox id="selectAll"
                disabled="#{BEAN.rowInEdit}" />
        </f:facet>

        <p:cellEditor>
            <f:facet name="output">
                <p:selectBooleanCheckbox id="cellCheck"
                    disabled="#{BEAN.rowInEdit} />
            </f:facet>
            <f:facet name="input">
                <p:selectBooleanCheckbox id="selectAll"
                disabled="true" />
            </f:facet>
        </p:cellEditor>
    </p:columns>
</p:dataTable> </h:form>

With the RowEdit listeners as follows:

public void rowEditInit(RowEditEvent event) {
rowInEdit = true;
UIData table = (UIData) event.getComponent();
String tableId = table.getClientId();
updateTableCheckboxes(tableId, table.getRowIndex());     }

public void rowEdit(RowEditEvent event) {
rowInEdit = false;
UIData table = (UIData) event.getComponent();
String tableId = table.getClientId();
updateTableCheckboxes(tableId, table.getRowIndex());    } // same for rowEditCancel

public void updateTableCheckboxes(String tableId, int index) {
int rowNumber = 0;
for (RowData row : rows) {
    int columnNumber = 0;
    for (ColumnData column : columns) {
        if (row != index) {
            String updateCheckboxId = tableId + ":" + rowNumber
                + ":columnsElement:" + columnNumber + ":cellCheck";
            FacesContext.getCurrentInstance()
                .getPartialViewContext().getRenderIds()
                .add(updateCheckboxId);
        }
        columnNumber++;
    }
    rowNumber++;
}

columnNumber = 0;
while (columnNumber < columns.size()) {
    String selectAllCheckboxId = tableId + ":columnsElement:"
        + columnNumber + ":selectAll";
    FacesContext.getCurrentInstance().getPartialViewContext()
        .getRenderIds().add(selectAllCheckboxId);
    columnNumber++;
}}

This is dynamically populating the Ids of the check boxes in columnsElement to be updated and is working fine for rowEditInit, disabling all checkboxes as expected. When I click the tick/cross for the rowEdit or rowEditCancel event the Ids to be updated are populated exactly the same, but only the check boxes of the row which had been edited are re-enabled.

If I click on a component elsewhere on the screen or one of the re-enabled check boxes then check boxes of other rows and the selectAll check boxes are all re-enabled as well strangely.

I have tried debugging the JSF lifecycle phases as in this blog, but all stages of the lifecycle are called exactly the same for Init, Edit and Cancel.

As far as I can see I'm doing the same thing, but getting different results in the different cases. Any suggestions why this could be happening?

Thanks


回答1:


I've managed to fix this using <p:remoteCommand/> outside of the datatable as follows...

<p:remoteCommand name="updateDataTable" update="table" />

and adding oncomplete="updateDataTable()" to the rowEdit and rowEditCancel ajax events.

This has given me the desired end result, but I am still curious why my initial solution didn't work or if there's a better solution.



来源:https://stackoverflow.com/questions/34724743/primefaces-datatable-roweditor-updating-cell-elements-on-roweditinit-but-not-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!