tags:

views:

1031

answers:

2

Using JSF1.2, if my datatable binding returns no rows I want to display a message saying so.

How do I do that?

And for extra points - how do I hide the table completly if it's empty?

Thanks.

+8  A: 

Make use of the rendered attribute. It accepts a boolean expression. You can evaluate the datatable's value inside the expression with help of the EL's empty keyword. If it returns false, the whole component (and its children) won't be rendered.

<h:outputText value="Table is empty!" rendered="#{empty bean.list}" />

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

For the case you're interested, here are other basic examples how to make use of the EL powers inside the rendered attribute:

<h:someComponent rendered="#{bean.booleanValue}" />
<h:someComponent rendered="#{bean.intValue > 10}" />
<h:someComponent rendered="#{bean.objectValue == null}" />
<h:someComponent rendered="#{bean.stringValue != 'someValue'}" />
<h:someComponent rendered="#{!empty bean.collectionValue}" />
<h:someComponent rendered="#{!bean.booleanValue && bean.intValue != 0}" />
<h:someComponent rendered="#{bean.stringValue == 'oneValue' || bean.stringValue == 'anotherValue'}" />
BalusC
+2  A: 

You can test this in several ways, for example by having a function in a bean that tests the list size:

function boolean isEmpty() {
    return myList.isEmpty();
}

then in the JSF pages :

<h:outputText value="List is empty" rendered="#{myBean.empty}"/>

<h:datatable ... rendered="#{!myBean.empty}">
...
</h:datatable>
romaintaz
A bit overwhelming if you can just do `#{bean.list.empty}` instead. Besides, this doesn't cover a list which is `null`. The EL `empty` keyword does.
BalusC