td2142
td2142

Reputation: 77

JSF table with varying number of columns across rows

Does vanilla JSF 2.0 support datatable with varying number of columns across different rows?

I have a 2 dimensional array in my Java bean, in some rows only one column contains data. For those rows I want them appear as merged. ie without borders between columns, and data able to use the width of the adjacent columns for display.

Is this reasonable to do without additional JSF frameworks?

Upvotes: 1

Views: 636

Answers (1)

Aritz
Aritz

Reputation: 31649

You can do it with vanilla HTML table instead of the JSF one. Just implement a double iteration for your matrix. Supposing you're able to use EL 2.2 expressions in your environment:

@ManagedBean
@RequestScoped
public class ArrayBean {

    private Integer[][] array = { { 1, 2, 3 }, { 1 } };

    public Integer[][] getArray() {
        return array;
    }

}
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<h:head />
<h:body>
    <table border="1">
        <ui:repeat var="row" value="#{arrayBean.array}">
            <tr>
                <ui:repeat var="column" value="#{row}">
                    <td colspan="#{fn:length(arrayBean.array) / fn:length(row)}"
                        align="center">#{column}</td>
                </ui:repeat>
            </tr>
        </ui:repeat>
    </table>
</h:body>
</html>

This solution doesn't take into account you could have decimal colspans, so you should calculate the lowest common denominator for your column number. That's, however, out of the scope of your question.

Upvotes: 2

Related Questions