完成时更新整个 " /> 在 <p:ajax event="cellEdit"> 完成时更新整个 <p:dataTable>

完成时更新整个

2024-03-25

编辑单元格后,我很难重新渲染 PrimeFaces 数据表。更改一个单元格中的值可能会更改其他单元格中的条目,因此需要刷新整个表格。

这是 JSF 页面:

<h:form id="testForm">
    <p:outputPanel id="testContainer">

        <p:dataTable id="testTable" value="#{tableBean.data}" var="entry" editable="true" editMode="cell">

            <p:ajax event="cellEdit" listener="#{tableBean.onCellEdit}" update=":testForm:testContainer" />

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

            <p:column headerText="Col2">
                <p:cellEditor>
                    <f:facet name="output"><h:outputText value="#{entry.col2}" /></f:facet>
                    <f:facet name="input"><p:inputText value="#{entry.col2}" /></f:facet>
                </p:cellEditor>  
            </p:column>

        </p:dataTable>

        <p:commandButton id="refreshButton" value="Redisplay" update="testContainer" />

    </p:outputPanel>                                    
</h:form>

这是支持 bean:

@ManagedBean(name = "tableBean", eager = false)
@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 ArrayList<RowData> entries = new ArrayList<RowData>();

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

    public void onCellEdit(CellEditEvent event) {
        entries.get(event.getRowIndex()).setCol1("Dummy Col 1");
        entries.get(event.getRowIndex()).setCol2("Dummy Col 2");        
    }   
}

当在 cellEdit AJAX 事件中包含 update=":testForm:testContainer" 时,更改单元格值会删除屏幕上的数据表,并且仅呈现单元格内容(以及按钮)——我不明白这是为什么。当未指定更新属性时,表格将保留在屏幕上,并且活动单元格已更新,但其他单元格均不会更新(如预期)。

通过不在 AJAX cellEdit 事件中指定更新属性并在编辑单元格的值后单击“重新显示”按钮,可以实现所需的行为(以非自动方式)。我怎样才能以自动化的方式实现这一点,为什么更新属性不能按我的预期工作?

我正在使用 PrimeFaces 4.0。


The rowEdit and cellEdit根据设计,表内的事件不会更新/重新渲染当前行以外的任何内容,即使在中明确指定时也是如此update属性。这是 PrimeFaces 过于热心地尝试最小化响应大小的结果。这在大多数情况下都是有意义的,但在您的具体情况下则不然。值得一份问题报告。

与此同时,在他们解决此行为之前,您最好的选择是使用<p:remoteCommand>调用所需的侦听器方法并执行表的完整更新。

Rewrite

<p:dataTable ...>
    <p:ajax event="cellEdit" listener="#{tableBean.onCellEdit}" update=":testForm:testContainer" />
    ...
</p:dataTable>

to

<p:remoteCommand name="onCellEdit" action="#{tableBean.onCellEdit}" update="testContainer" />
<p:dataTable ...>
    <p:ajax event="cellEdit" oncomplete="onCellEdit()" />
    ...
</p:dataTable>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

完成时更新整个 的相关文章