生成数据表的代码
public DataTable getDataTable(DataTable myDataTable,List<? extends ExcelTableModel> value,List<String> attributes){
FacesContext fc = FacesContext.getCurrentInstance();
Application application = fc.getApplication();
ExpressionFactory ef = application.getExpressionFactory();
ELContext elc = fc.getELContext();
int i=0;
for(String attribute:attributes){
ValueExpression valueExp = ef.createValueExpression(elc, "#{mydata." + attribute + "}", Object.class);
HtmlOutputText output = (HtmlOutputText)application.createComponent(HtmlOutputText.COMPONENT_TYPE );
output.setValueExpression("value", valueExp);
output.setId(attribute+"output"+i);
InputText input = (InputText)application.createComponent(InputText.COMPONENT_TYPE );
input.setValueExpression("value", valueExp);
input.setStyle("color:black");
input.setId(attribute+"input"+i);
CellEditor editor = (CellEditor)application.createComponent(CellEditor.COMPONENT_TYPE );
editor.getFacets().put("output", output);
editor.getFacets().put("input", input);
Column dataColumn = new Column();
dataColumn.setHeaderText(attribute);
dataColumn.getChildren().add(editor);
myDataTable.getChildren().add(dataColumn);
i++;
}
return myDataTable;
}
数据表绑定代码
<h:form id="form">
<p:dataTable
id="table"
var="mydata"
value="#{dynamicColumnBean.linkedList}"
binding="#{dynamicColumnBean.myDataTable}"
editable="true"
editMode="cell"
widgetVar="aList"
tableStyle="width:auto"
rowIndexVar="rowIndex">
<p:ajax event="cellEdit" listener="#{dynamicColumnBean.onCellEdit}" update=":form:table"/>
</p:dataTable>
</h:form>
onCellEdit事件监听器
public void onCellEdit(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
}
每一件事都有效,但当我检查onCellEdit事件方法时,它为getOldValue和getNewValue返回相同的值,即使我为它们获得相同的id。我想我在为数据表生成组件时遗漏了一些东西。在此提前感谢我在这里错过了什么。