Search code examples
jsfjsf-2datatablemyfaces

Apache Myfaces prints blank td tag when using h:dataTable and an empty dataset


Is there a way to have MyFaces not print the following when the List or Array backing an h:dataTable is empty?

<table>
    <tbody id="itemsForm:itemsDataTable:tbody_element">
        <tr>
            <td></td>
        </tr>
    </tbody>
</table>

I suspect it would be more correct to print an empty tbody. Can this be overridden somehow?


Solution

  • Those elements are required as per XHTML spec. The <table> requires at least one <tr>. The <tr> requires in turn at least one <td>.

    <!ELEMENT table
         (caption?, (col*|colgroup*), thead?, tfoot?, (tbody+|tr+))>
    <!ELEMENT caption  %Inline;>
    <!ELEMENT thead    (tr)+>
    <!ELEMENT tfoot    (tr)+>
    <!ELEMENT tbody    (tr)+>
    <!ELEMENT colgroup (col)*>
    <!ELEMENT col      EMPTY>
    <!ELEMENT tr       (th|td)+>
    <!ELEMENT th       %Flow;>
    <!ELEMENT td       %Flow;>
    

    (the + stands for one or more, the * stands for zero or more, the ? stands for zero or one)

    Your best bet is to hide the table altogether when the data model is empty.

    <h:dataTable ... value="#{bean.items}" rendered="#{not empty bean.items}">
    

    Otherwise, you can't go around a custom renderer.