0

I will be grateful, if you help me , so thank you in advance. I'm using PrimeFaces. I have a dynamic database table. I don't have problems with creating this table but I want to view this table using datatable and columns

Here is the part where I add database 's table content in an ArrayList:

elts = new ArrayList<String>();

try {
    Statement stmt = conn.createStatement();
    ResultSet RS = stmt.executeQuery("SELECT * FROM temporaire");
    ResultSetMetaData metadata = RS.getMetaData();
    nombreColonnes = metadata.getColumnCount();

    for (int j = 1; j <= nombreColonnes; j++) {
        cols.add("colonne" + j);
    }

    while (RS.next()) {
        for (int i = 1; i <= nombreColonnes; i++) {
            elts.add(RS.getArray(i).toString());
        }
    }

and the xhtml code of datatable is :

<p:dataTable value="#{templateBean.elts}" var="elts" rowIndexVar="indro"  rows="10"
             paginator="true"
             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
             columnsPerPageTemplate="5,10,15">
    <p:columns value="#{templateBean.cols}" var="cols" columnIndexVar="index">
        <f:facet name="header">
            <h:outputText value="#{cols}"/>
        </f:facet>
        <h:outputText value="#{elts}"/>
    </p:columns>
</p:dataTable>

and the problem is that the datatable i am getting is repeating the same element in each row like this:

col1Content | col1Content |col1Content | col1Content 
col2Content |col2Content |col2Content |col2Content 
...

Can you please help me where is the problem and what's wrong with my code? Thank you

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • This `#{templateBean.cols}` will be evaluated to the same value on every iteration of ``. You need to fetch a different value (an object) from the managed bean based on the row of `` being currently iterated over. You may consider a `java.util.Map` for this purpose in which you may pass a key value based on the data table item currently being iterated over to fetch an item associated with that key on every iteration of ``. – Tiny Apr 17 '15 at 22:58
  • You also do not need to use `` in this case to put a caption on each dynamically generated column. Instead, there is a `headerText` property associated with `` such as ``. Also, remove `columnIndexVar`, if it is not needed. – Tiny Apr 17 '15 at 22:59
  • Also there is no such an attribute of `columnsPerPageTemplate` associated with a ``. It is `rowsPerPageTemplate` instead. See also a [relevant question](http://stackoverflow.com/q/25658034/1391249). – Tiny Apr 17 '15 at 23:54
  • thank you , it worked for me by using an ArrayList to store the content of the database table. Thank you for the tips Tiny :) – faika bouthalja Apr 19 '15 at 12:58

0 Answers0