How to show JSF components if list is not null and has size() > 0 - jsf

How do I show JSF components if a list is not null and it has a size() > 0?

EL offers the empty operator which checks both the nullness and emptiness of an object.
Thus, this should do:
<h:dataTable value="#{bean.list}" var="item" rendered="#{not empty bean.list}">
No need for a clumsy double check on both null and size() as suggested by other answers.
See also:
How do I display a message if a jsf datatable is empty?
Conditionally displaying JSF components

use rendered attribute. most of the components have this attribute.This attribute;s main purpose is to render components conditionally.
<h:dataTable value="#{bean.list}" rendered="{bean.list !=null && bean.list.size()>0}" >
In the above piece of jsf code, datatable would only be rendered when list is not null and the size of list is greater than 0

<h:outputText value="No Data to Display!" rendered="#{empty list1.List2}" />
<a href="#">
<h:outputText value="Data is present" rendered="#{not empty list1.List2}" /></a>
Or
<h:outputText value="#{not empty list1.List2 ? 'Data is Present' : 'No Data to Display'}" style="color:blue"/>

Related

JSF datatable/panel display on condition

I have a datatable I display with some value from database etc.
I only want to render the datatable if there is values to display, if not maybe display a message saying there is no values.
I know you can add the datatable to a panelgroup, example:
<h:panelGroup rendered="#{myList > 0}">
Is there a better way of doing this? how would i display the message if the datatable is not rendered?
using jsf 2.2 and richfaces 4.
thanks
You can use opposite conditions for rendering message or result table. For example:
<h:outputText value="No payment receipt to display"
rendered="#{paymentReceiptList.size == 0}"/>
<rich:dataTable id="paymentReceiptTable" var="receipt"
value="#{paymentReceiptList}" rendered="#{paymentReceiptList.size > 0}" >
Or you can display "No result found" info inside rich:dataTable using:
<f:facet name="noData">
<h:outputText value="#{msg.noData}" />
</f:facet>
Or you can display your message inside rich:dataTable using noDataLabel attribute of rich:dataTable.

Evaluation with <c:when> in data table doesn't work

I have searched and tried but this time I'm really stuck.
I am trying to build a simple datatable (primefaces) where the cell content can be of different types. There is a holder class for each cell content that can hold different entities and I want to use < c:choose> to check and display the right entity in the entity specific way.
The code looks like:
<p:dataTable emptyMessage="" value="#{date.getThreadContent(column.propertyID)}" var="content">
<p:column>
#{content.type}
<c:choose>
<c:when test="#{content.type == 'activity'}">
#{content.activity.name}
</c:when>
<c:when test="#{content.type == 'todo'}">
#{content.activity.name}
</c:when>
<c:otherwise>
Neither activity or todo
</c:otherwise>
</c:choose>
</p:column>
</p:dataTable>
The EL evaluation doesn't work regardless if I try a boolean or String values in the bean (I'm using String value in the code above).
So each time the content is not empty, the output first displays the correct content type from #{content.type} but then together with 'Neither activity or todo'.
I can also say that this data table itself represents a cell content in a parent data table (list of entities per date) that is not visible in this code as I try to isolate the problem.
Could it be that this type of expression cannot be done in a ?
Any help is appreciated.
Use JSF rendered tags to do your conditional display logic, it removes it from the DOM completely and makes validations easier.
Refer: http://docs.oracle.com/javaee/6/tutorial/doc/bnaik.html for EL expressions
Also a good explanation is provided by BalusC here: JSTL c:if doesn't work inside a JSF h:dataTable
<p:dataTable emptyMessage="" value="#{date.getThreadContent(column.propertyID)}" var="content">
<p:column>
#{content.type}
<h:outputText value="#{content.activity.name}" rendered="#{content.type == 'activity' or content.type == 'todo'}" />
<h:outputText value="Neither activity or todo" rendered="#{content.type != 'activity' and content.type != 'todo'}" />
</p:column>
</p:dataTable>

Handling a ui:repeat that returns empy value

I have a ui:repeat that returns a list of entity. Is there any way to know that the returned list is empty?
<ui:repeat id="resulta" value="#{testController.testList}" var="list">
<div>list.name</div>
</ui:repeat>
something like if ui:repeat is empty then display a div saying "List is empty"
i've heard about varStatus -> Facelets repeat Tag Index
but i don't think there is something for empty list. or is there?
UPDATED
<ui:repeat id="resulta" value="#{testController.testList}" var="list">
<div>list.name</div>
<h:panelGroup rendered="#{empty list}">
list is empty!
</h:panelGroup>
</ui:repeat>
I tried to render the "list is empty!" when the list is empty but then it doesn't show.
<ui:repeat id="resulta"
value="#{testController.testList}"
var="list">
<div>
#{list.name}
</div>
</ui:repeat>
<h:panelGroup rendered="#{empty testController.testList}">
List is empty!
</h:panelGroup>
rendered is a conditional statement which only if it is true renders. In case you want to render the last h:panelGroup as a div instead of a span, consider adding layout='block' to the element.
You can display your empty list message outside your <ui:repeat> element as:
<ui:repeat id="resulta"
value="#{testController.testList}"
var="list"
rendered="#{not empty testController.testList}">
<div>
#{list.name}
</div>
</ui:repeat>
<h:panelGroup rendered="#{empty testController.testList}">
list is empty!
</h:panelGroup>

h:selectOneMenu in p:dataTable doesn't submit its value

I have a question about selectOneMenu and settting the values. I have an Object SampleDesc that has and ID, Text, and a List<SampleDescValues>. For each datatable row the Text is the output label and the select one menu values are the List<SampleDescValues>.
XHTML:
<h:panelGroup id="tables">
<p:dataTable resizableColumns="true"
var="sampleDesc" id="SampleDescTable" rowIndexVar="rowIndex"
value="#{sampleBean.sampleDescList.list}"
rendered="#{sampleBean.sampleDescList.list.size() gt 0}">
<p:column>
<h:outputLabel value="#{sampleDesc.sampleDescText}"/>
</p:column>
<p:column>
<h:selectOneMenu required="#{sampleBean.sampleDescList.list.size() gt 0}" converter="#{sampleDescValueConverter}"
id="SampleDescValue" value="#{sampleBean.selectedSampleDescList.get(rowIndex)}">
<f:selectItem itemLabel="Select One" itemValue="#{null}"/>
<f:selectItems value="#{sampleDesc.sampleDescValues}" var="sdv"
itemLabel="#{sdv.sampleDescValuesText}" itemValue="#{sdv}" />
</h:selectOneMenu>
</p:column>
</p:dataTable>
</h:panelGroup>
I have the converter setup and it works because ive set it to a single SampleDescValue and it set the value.
The problem is when i try and populate the form with a Sample from the database it can only set one of the dropdowns when there could be an infinite number of selectonemenu's
I set the value selected to private List<SampleDescValue> selectedSampleDescList;
When i try and submit it does nothing, it works when the datatable is not rendered.
Your menu value is wrong:
<h:selectOneMenu value="#{sampleBean.selectedSampleDescList.get(rowIndex)}">
It's not possible to perform a set operation on this EL expression.
Use the brace notation instead:
<h:selectOneMenu value="#{sampleBean.selectedSampleDescList[rowIndex]}">
Note that this expects a non-null selectedSampleDescList. So make sure that you've already properly initialized it with a new ArrayList<>() beforehand. EL won't do that for you. It will only set the list items using List#add(index, object) method.
See also:
Our EL wiki page
Unrelated to the concrete problem, this expression
#{sampleBean.sampleDescList.list.size() gt 0}
can be simplified as follows
#{not empty sampleBean.sampleDescList.list}
And this is unnecessary in the required attribute of the <h:selectOneMenu> as it would always evaluate true at that point. Just use required="true" directly instead.

Best approach to render UI elements based on dataType

I am trying to render richfaces and jsf UI elements dynamically based on the dataType value.
Ex : I have a enum as below
public enum DataType {
DT_LONGLONG(1), DT_STRING(2), DT_LONG(3), DT_DATE(4), DS_EXTERNALREFERENCE(5),
DT_BOOLEAN(6), DT_FLOAT(7), DT_SHORT(8);
}
Then in xhtml page while iterating through the list of my custom objects, I check for the dataType and render the UI elements accordingly as below :
<c:if test="#{meaCompPartAttr.dataType.dataType == 2}">
<h:inputText />
</c:if>
<c:if test="#{(meaCompPartAttr.dataType.dataType == 1) or
(meaCompPartAttr.dataType.dataType == 3) or
(meaCompPartAttr.dataType.dataType == 8)}">
<h:inputText onkeyup="javascript:validateField(this, '#{tpMsgs.longRegularExpression}');">
<f:validateLongRange/>
</h:inputText>
</c:if>
<c:if test="#{meaCompPartAttr.dataType.dataType == 7}">
<h:inputText onkeyup="javascript:validateField(this, '#{tpMsgs.doubleRegularExpression}');">
<f:validateDoubleRange/>
</h:inputText>
</c:if>
<c:if test="#{meaCompPartAttr.dataType.dataType == 6}">
<h:selectBooleanCheckbox />
</c:if>
<c:if test="#{meaCompPartAttr.dataType.dataType == 4}">
<rich:calendar />
</c:if>
Because of this I usually get class cast exceptions like String to Boolean or Long to String etc. I assume this is happening coz jstl and jsf code do not run in sync.
Is there any other approach to render UI elements dynamically as proposed in the above sample?
So you're iterating using <ui:repeat> or <h:dataTable> or any other JSF iterating component instead of the JSTL <c:forEach>? Either use <c:forEach> instead, or use the rendered attribute instead of <c:if>.
See also:
JSTL in JSF2 Facelets... makes sense?
How to create dynamic JSF form fields

Resources