Mihnea Mihai
Mihnea Mihai

Reputation: 181

Create h:column components inside h:dataTable based on a list of column names

I'm trying to create a <h:dataTable> using two lists. One list contains the column names and the other one contains the data that will populate the table. I need the list containing the column names because the data in the second one has more attributes than I'd like to display. How can I achieve this?

Upvotes: 1

Views: 779

Answers (1)

BalusC
BalusC

Reputation: 1108722

Yes, you can if you use <c:forEach> to iterate over the column names and generate the <h:column> components necessary for <h:dataTable>. You can use the brace notation [] in EL to use the column name as a "dynamic" bean property name (or even as a Map key).

<h:dataTable value="#{bean.rows}" var="row">
    <c:forEach items="#{bean.columnNames}" var="columnName">
        <h:column>#{row[columnName]}</h:column>
    </c:forEach>
</h:dataTable>

Upvotes: 5

Related Questions