Search code examples
primefacesjsf-2.2primefaces-datatable

DataTable Columns Pagination Issue


Using the showcase example the Declarative p:dataTable with p:columns and pagination works.

I would like to get this working in a programmatic way and on page load grid is shown with data. when i click on the pagination for programmatic , all the data is lost. It still show the correct rows , but i am not sure why the data in missing.

enter image description here

Project : https://github.com/ravihariharan/primefaces-test Primefaces 6 , JSF 2.2.14 , On Tomcat 8.5.3

Refer : 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>

Solution

  • i got this working by using 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>