Use ui:repeat variable in c:if statement [duplicate] - jsf

This question already has an answer here:
Specify conditional rendering of element inside <ui:repeat>? The <c:if> does not seem to work
(1 answer)
Closed 7 years ago.
I am trying to use a c:if statement inside a ui:repeat tag.
However U cannot get the value of course.days while inside the if tag. It just always evaluates to empty.
<div id="schedule_wrapper">
<ui:repeat value="#{schedule.courses}" var="course">
<div class="course">
<b>#{course.shortTitle}</b> : #{course.subjectCode} #{course.courseNumber}-#{course.section}<br />
#{course.roomCode} #{course.buildingName} <br />
<c:if test="${not empty course.days}">
#{course.days} : #{course.startTime} - #{course.endTime}
</c:if>
</div>
</ui:repeat>
</div>
Any ideas how I can use the course.days variable inside the if statement?
Or an alternate way to achieve the same thing...
All of the other course.xxxxx value as pulling their values in.

Try to use inside your using a rendered to check your condition.
rendered="#{not empty course.days}" or #{course.days > 0} if days is a Integer
<div id="schedule_wrapper">
<ui:repeat value="#{schedule.courses}" var="course">
<div class="course">
<b>#{course.shortTitle}</b> : #{course.subjectCode} #{course.courseNumber}-#{course.section}<br />
#{course.roomCode} #{course.buildingName} <br />
**<ui:fragment rendered="#{not empty course.days}">
#{course.days} : #{course.startTime} - #{course.endTime}
</ui:fragment>**
</div>
</ui:repeat>
</div>

I found an alternate way to achieve this.
Instead of:
<c:if test="${not empty course.days}">
#{course.days} : #{course.startTime} - #{course.endTime}
</c:if>
I used:
<h:panelGroup rendered="#{not empty course.days}">
#{course.days} : #{course.startTime} - #{course.endTime}
</h:panelGroup>
I am still interested if this is possible with an if statement.

Related

How to iterate over a list in a textarea using JSF? [duplicate]

This question already has answers here:
How iterate over List<T> and render each item in JSF Facelets
(2 answers)
Closed 6 years ago.
I am new to JSF2 and having very less idea about front end developement.Currently i am trying to iterate over a list in a text area using JSF2? we have a list of datas like aList[bean1,bean2,bean3,....] and we have to print datas
<h:inputTextarea var ="list" value="#{Bean.value}">
row1-> datas of bean1
row2-> datas of bean2
like all the datas in the list
</h:inputTextarea>
I vahe searched a lot but did not get proper information, please help me.
If you want to display different textArea for different rows you can write as:
<h:form id="test">
<h:panelGrid value="#{testController.myRows}" var="myVar">
<h:inputTextarea value="#{myVar}"/>
</h:panelGrid>
</h:form>
Although instead of panelGrid you can use datalist also
<ace:panel id="jPanel5" style="text-align:left;border:0;width:100%;">
<h:outputLabel id="message" value="#{msgs['css.Label.message']}" styleClass="css-lebel">
</h:outputLabel>
<ice:panelSeries var="item" value="#{Bean.searchResultArea}" style="border:1px solid black;width:1150px;height:400px;overflow-y:scroll;overflow-x:scroll;">
<h:outputText value="#{item}<br/><br/>" style="white-space: nowrap;" escape="false" />
</ice:panelSeries>
</ace:panel>
I did like this and get proper output.

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>

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

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"/>

Concatenating strings within EL expression defined in an attribute of a facelets tag

I need to write an EL expression for an attribute which goes something like this:
#{cc.attrs.appreciatedByCurrentUser ? (cc.attrs.count +'<br/>'+ (cc.attrs.count-1)) : ((cc.attrs.count+1) +'<br/>'+ cc.attrs.count)}
Now the problem is that this gives an error as strings cannot be concatenated, the way I am doing it. So how can I rectify this?
I'm using JSF 2.0 with facelets.
EDIT :
I'm resolving the issue using the following inline javascript
<script type="text/javascript">
var count=#{cc.attrs.count};
document.write(#{cc.attrs.appreciatedByCurrentUser} ? (count-1) +'<br/>'+count : count+'<br/>'+ (count+1));
</script>
Can you think of any issue with this?
It is possible to concatenate Strings in EL using the java.lang.String.concat(String) method. Thus your code could look like:
<h:outputText value="#{cc.attrs.appreciatedByCurrentUser ? (''.concat(cc.attrs.count).concat('<br/>').concat(cc.attrs.count-1)) : (''.concat((cc.attrs.count+1)).concat('<br/>').concat(cc.attrs.count))}" escape="false" />
In this particular case however I would go with one of the options that Mr BalusC had suggested because the code above doesn't look quite elegant. In some cases knowing this technique could be useful, though.
I would hardly recommend using javascript as a workaround here.
String concatenation in EL is only possible by just inlining in the expression. The + operator is in EL exclusively a sum operator. Further, < and > are invalid characters in XML attributes, so you have to escape them (and instruct <h:outputText> to not escape them once again by escape="false"):
<h:outputText value="#{cc.attrs.count}<br/>#{cc.attrs.count-1}" escape="false" rendered="#{cc.attrs.appreciatedByCurrentUser}" />
<h:outputText value="#{cc.attrs.count+1}<br/>#{cc.attrs.count}" escape="false" rendered="#{!cc.attrs.appreciatedByCurrentUser}" />
Alternatively, you can also use <c:set> to alias the expression:
<c:set var="appreciated" value="#{cc.attrs.count}<br/>#{cc.attrs.count-1}" />
<c:set var="notAppreciated" value="#{cc.attrs.count+1}<br/>#{cc.attrs.count}" />
<h:outputText value="#{cc.attrs.appreciatedByCurrentUser ? appreciated : notAppreciated}" escape="false" />
This is the only thing i can come up with.
<h:panelGroup rendered="#{cc.attrs.appreciatedByCurrentUser}">
<h:outputText value="#{(cc.attrs.count)}" style="display:block;" />
<h:outputText value="#{(cc.attrs.count-1)}" />
</h:panelGroup>
<h:panelGroup rendered="#{not cc.attrs.appreciatedByCurrentUser}">
<h:outputText value="#{(cc.attrs.count+1)}" style="display:block;" />
<h:outputText value="#{(cc.attrs.count)}" />
</h:panelGroup>
Putting <br> in a value attribute will always throw errors in JSF, so you'll have to use display:block.

JSF Compound EL expression

I am trying to validate a inputText box based on the selection of a CheckBox as shown below.
< <h:inputText required="#{param[facesContext.externalContext.response.namespace'form:checkBoxId']}"> >.
The issue is as you see, the component Ids are dynamic, I should be able to use facesContext.externalContext.response.namespace inside the EL expression. Is there a solution to it, appreciate any suggestions.
Thanks.
Just bind the UIComponent to a page scoped property and access its getValue() method.
<h:selectBooleanCheckbox binding="#{checkbox}" />
<h:inputText required="#{not empty checkbox.value and checkbox.value}" />
As to the dynamicness, you can also go around by just giving it a fixed id.
<h:form id="form">
<h:selectBooleanCheckbox id="checkbox" />
<h:inputText required="#{not empty param['form:checkbox'] and param['form:checkbox']}" />
</h:form>
It's however only ugly when it gets lengthy.

Resources