使用展示示例,带有 p:columns 和分页的声明式 p:dataTable 可以工作。
我想让它以编程方式工作,并在页面加载网格上显示数据。当我点击程序化的分页时,所有数据都丢失了。它仍然显示正确的行,但我不确定为什么数据丢失。
项目:https ://github.com/ravihariharan/primefaces-test Primefaces 6,JSF 2.2.14,在 Tomcat 8.5.3 上
参考: 1. main/webapp/columns.xhtml 2. main/java/org/primefaces/showcase/view/data/datatable/ColumnsView.java
@PostConstruct
public void init() {
cars = service.createCars(50);
createDynamicColumns();
createProgramaticGrid();
}
private void createProgramaticGrid() {
DataTable dataTable = createDataTable();
dataTable.setId("progCars");
dataTable.setVar("progCar");
dataTable.setValueExpression("value", createValueExpression("#{dtColumnsView.cars}", List.class));
dataTable.setPaginator(true);
dataTable.setRows(10);
Columns columns = createColumn();
ValueExpression columnValues = createValueExpression("#{dtColumnsView.columns}", List.class);
columns.setValueExpression("value", columnValues);
columns.setVar("column");
OutputLabel headerValue = createOutputLabel();
headerValue.setValueExpression("value", createValueExpression("#{column.header}", String.class));
columns.getFacets().put("header", headerValue);
OutputLabel value = createOutputLabel();
value.setValueExpression("value", createValueExpression("#{progCar[column.property]}", String.class));
columns.getChildren().add(value);
dataTable.getChildren().add(columns);
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot viewRoot = context.getViewRoot();
viewRoot.findComponent("form:mainGridBody").getChildren().add(dataTable);
}
private OutputLabel createOutputLabel() {
return (OutputLabel) createComponent(OutputLabel.COMPONENT_TYPE);
// return new OutputLabel();
}
private Columns createColumn() {
return (Columns) createComponent(Columns.COMPONENT_TYPE);
// return new Columns();
}
private DataTable createDataTable() {
return (DataTable) createComponent(DataTable.COMPONENT_TYPE);
// return new DataTable();
}
private UIComponent createComponent(String componentType) {
return FacesContext.getCurrentInstance().getApplication().createComponent(componentType);
}
列.xhtml
<h3>programmatic</h3>
<p:panel id="mainGridBody">
</p:panel>