DataTable 列分页问题
DataTable Columns Pagination Issue
使用展示示例,声明式 p:dataTable 与 p:columns 和分页一起工作。
我想让它以编程方式工作,并在页面加载网格中显示数据。当我点击程序化的分页时,所有数据都丢失了。它仍然显示正确的行,但我不确定为什么数据丢失了。
项目:https://github.com/ravihariharan/primefaces-test
Primefaces 6,JSF 2.2.14,在 Tomcat8.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);
}
columns.xhtml
<h3>programmatic</h3>
<p:panel id="mainGridBody">
</p:panel>
我使用 javax.faces.FULL_STATE_SAVING_VIEW_IDS 让这个工作正常。
javax.faces.FULL_STATE_SAVING_VIEW_IDS
<context-param>
<param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>
<param-value>/columnsprog.xhtml</param-value>
</context-param>
使用展示示例,声明式 p:dataTable 与 p:columns 和分页一起工作。
我想让它以编程方式工作,并在页面加载网格中显示数据。当我点击程序化的分页时,所有数据都丢失了。它仍然显示正确的行,但我不确定为什么数据丢失了。
项目:https://github.com/ravihariharan/primefaces-test Primefaces 6,JSF 2.2.14,在 Tomcat8.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);
}
columns.xhtml
<h3>programmatic</h3>
<p:panel id="mainGridBody">
</p:panel>
我使用 javax.faces.FULL_STATE_SAVING_VIEW_IDS 让这个工作正常。
javax.faces.FULL_STATE_SAVING_VIEW_IDS
<context-param>
<param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>
<param-value>/columnsprog.xhtml</param-value>
</context-param>