Kelper
Kelper

Reputation: 91

Primefaces lazy data table is empty

I'm trying to implement a lazy data table on PrimeFaces (5.3) but it never renders the table. The problem is that load() method on the lazy model class never gets called. The table is build from an arrayList of Strings, where each String[] (length 5) is a row in the table.

View (relevant code)

<h:form id="formHome">
            <p:commandButton process="@this" partialSubmit="true" update="@form"
                value="obtener tabla"
                actionListener="#{mbParametros.init}"
                oncomplete="location.reload();">
            </p:commandButton>
    <p:dataTable id="parametros"
                            selection="#{mbParametros.lazy.seleccionados}"
                            rowKey="#{registro[0]}" value="#{mbParameros.lazy}" var="registro"
                            paginator="true" lazy="true" rows="15" selectionMode="single    "
                            paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                            rowsPerPageTemplate="15,30,45" resizableColumns="false"
                            widgetVar="tabla">

                            <p:ajax event="page" oncomplete="location.reload();" />

                            <f:facet name="header">
                                <h:outputText value="Parametros, Fechas y Etiquetas" />
                            </f:facet>

                            <!-- COLUMNA DE CHECKBOXES   -->
                            <p:column selectionMode="multiple" />

                            <!-- COLUMNA DEL ENTORNO GEOGRAFICO -->
                            <p:column id="col0" headerText="#{mbParametros.lazy.header}">

                                <h:outputText value="#{registro[0]}" />

                            </p:column>

                            <!-- COLUMNA DEL TIPO DE PARAMETRO -->
                            <p:column id="col1" headerText="Tipo">
                                <h:outputText value="#{registro[1]}" />
                            </p:column>

                            <!-- COLUMNA DEL NOMBRE DEL PARAMETRO -->
                            <p:column id="col2" headerText="Parametro">
                                <h:outputText value="#{registro[2]}" />
                            </p:column>


                            <!-- COLUMNA DEL VALOR DEL PARAMETRO -->
                            <p:column id="col3" headerText="Valor">
                                <h:outputText value="#{registro[3]}" />

                            </p:column>


                            <!-- COLUMNA DE LA DESCRIPCION DEL PARAMETRO -->
                            <p:column id="col4" headerText="Descripcion">
                                <h:outputText value="#{registro[4]}" />

                            </p:column>


                        </p:dataTable>

</h:form>

Controller (spring controller, have prime configured with spring 3.2.16)

@Scope("session")
@Controller("mbParametros")
public class MBParametros implements Serializable {

    private static final Logger LOG = LoggerFactory.getLogger(MBParametros.class);
    private static final long serialVersionUID = 1L;

    @Autowired
    private ResourceBundleMessageSource recursos;

    private LazyDataModel<String[]> lazy;

    public void init() {
        LOG.info("ENTRA AL INIT");
        this.setLazy(new ModeloLazy(recursos) {
            private static final long serialVersionUID = 1L;

        });
    }

    public LazyDataModel<String[]> getLazy() {
        return lazy;
    }

    public void setLazy(LazyDataModel<String[]> lazy) {
        this.lazy = lazy;
    }

Lazy Model class (relevant code only)

 public class ModeloLazy extends LazyDataModel<String[]> {
      // ArrayList of Strings that represent all table rows
    private List<String[]> tabla;    

    public List<String[]> getTabla() {
        return tabla;
    }

    public void setTabla(List<String[]> tabla) {
        this.tabla = tabla;
    }
    // all other fields, getters, setters, init, etc.....
        // constructor
 public ModeloLazy(ResourceBundleMessageSource recursos) {
            this.recursos = recursos;
            this.init();
 }

        // load never gets called by the framework, so its an error somewhere else
        @Override
public List<String[]> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
            this.setRowCount(15);
            return this.getTabla();
        }

    // init() calls this method for building the table
    // after execution, tabla is not null nor empty. 
    private void construir(DTOResponse result, DTORequest request) {
            List<DTORegistro> registros = bsd.join(result, request); //OK
            for (DTORegistro dtoRegistro : registros) {
                String tipo; 
                switch(dtoRegistro.getParametro().getTipo()) {
                case 'p':
                    tipo = "Parametro";
                    break;
                case 'e':
                    tipo = "Etiqueta";
                    break;
                default:
                    tipo = "Fecha";
                }
                String[] reg = {dtoRegistro.getDto().getNombre() + tipo
                        + dtoRegistro.getParametro().getValor() + dtoRegistro.getParametro().getDescripcion()};
                this.getTabla().add(reg);
            }

        }

    }

Upvotes: 1

Views: 1721

Answers (1)

J Slick
J Slick

Reputation: 939

My implementation of LazyDataModel also would not display rows until I called LazyDataModel.setRowCount(int rowCount) in the constructor of my implementation class.

My <p:dataTable> loads over 16K rows, so I use a paginator which calls load() upon page selection. load() fetches the specified rows according to arguments first and pageSize, then calls setRowCount(int rowCount) then the proper page of data is displayed in the DataTable.

Upvotes: 2

Related Questions