セルが編集されると、PrimeFaces Datatable を再レンダリングするのが困難です。1 つのセルの値を変更すると、他のセルのエントリが変更される可能性があるため、テーブル全体を更新する必要があります。
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");
}
}
update=":testForm:testContainer" を cellEdit AJAX イベント内に含めると、セルの値を変更すると、画面上のデータ テーブルが削除され、セルのコンテンツのみが (ボタンと共に) レンダリングされます。これがなぜなのかわかりません。update 属性が指定されていない場合、テーブルはアクティブ セルが更新された状態で画面に残りますが、他のセルは更新されません (予想どおり)。
AJAX cellEdit イベント内で update 属性を指定せず、セルの値を編集した後に [再表示] ボタンをクリックすることで、目的の動作を (自動化されていない方法で) 実現できます。自動化された方法でこれを達成するにはどうすればよいですか? update 属性が期待どおりに機能しないのはなぜですか?
PrimeFaces 4.0 を使用しています。