JSF - What happens if value EL expression evaluates to null? - jsf

What will happen if the EL expression like #{bean.list} is null. I've a nested datatable and the "value" of dataTable evaluates to null. In below example if issuerList is null, outer table footer is displayed but not the inner table. How does #{Issuer.issue} is handled considering "Issuer" is null !!!
Ex:
<h:datatable value="#{bean.issuerList}" var="issuer" >
..
<h:datatable value="#{issuer.issueList}" var="issue" >
....
....
<f:facet name="footer">
This is inner footer
</f:facet>
</h:datatable>
<f:facet name="footer" >
This is outer footer
</f:facet>
</h:datatable>

If the #{bean.issuerList} is null then there are simply no rows to render. Since the nested datatable (and its footer) is part of a row, it's also never rendered. You would need at least one row to get the nested datatable (and its footer) to render. The footer of the outer datatable is not part of any row, so it's always rendered.

Related

jsf tabledata dynamic inputtext

I have a List in my backing bean. I want to present it in a table. I want table to have two rows - on left side, values from the list, and on the right side inputBoxes. It's the easy part. Here's my code:
<div class="table">
<h:dataTable id="korekty" styleClass="table table-hover"
value="#{searchBean.listX}" var="v">
<h:column>
<f:facet name="header">XXX</f:facet>
#{v.string}
</h:column>
<h:column>
<f:facet name="header">YYY</f:facet>
<h:inputText styleClass="form-control">
</h:inputText>
</h:column>
</h:dataTable>
</div>
The hard part of my problem is : in those inputtext user might put some numbers. Then after hitting SUBMIT button I want to create a list/map/whatever of inputed values. How can I do that? Thanks for help
First of all, prepare an array, or a list, containing placeholders for the given data alongside the list you've got:
List<String> initialData = ...;//initial data of n size
String[] submittedData = new String[initialData.size()];//array of the same size
Then, bind the <h:dataTable> component to the view to get access to the current row index of the iteration. This way you can finish your job by binding value of input elements to the corresponding objects in the array/list:
<h:dataTable binding="#{table}" ... >
<h:column>
<f:facet name="header">YYY</f:facet>
<h:inputText value="#{searchBean.submittedData[table.rowIndex]}" ... />
</h:column>
</h:dataTable>

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

Generate unique id for h:dataTable in the loop

I am using JSF 2.0. When I am trying to use a variable in the 'id' attribute of the h:dataTable its not taking that variable value.
I have h:dataTable inside ui:repeat with id values as index of ui:repeat as mentioned below.
<ui:repeat var="planMap" value="#{planAccountMap.entrySet().toArray()}" varStatus="planMapStatus">
<h:dataTable id="planTable#{planMapStatus.index}" value="#{planMap.value}" var="accountList">
Does any one know whether we can have dynamic id generated for h:dataTable in loop?
I have a Java script which is making use of table id, but as I am not able to have the unique id for each table in the loop, its breaking.
Any component inside of a repeatable component will always have a dynamic client id. You cannot do this.
Perhaps instead you can assign a unique style class to the dataTable component and use a jQuery selector to return an array of all the dataTable objects in the ui:repeat.
jQuery('.SomeClass');
I do some thing very similar to you. i have a UI:repeat inside a datatable column. tbh, if you check in fire bug, your datatable id would be unique as you put it inside a ui:repaet.
planMap:0:yourdatatableID
This problem can be solve by using datatable inside datatable. Second datatable is kept inside the column of the first datatable as below.
<h:dataTable id="outerTable" value="#{planAccountMap.entrySet().toArray()}" var="planMap" width="100%">
<h:column>
<h:dataTable id="planTable" value="#{planMap.value}" var="accountList"
headerClass="tablehead"
rowClasses="'',altrowcoloropt1"
first="0">
<h:column>
<f:facet name="header">
<h:outputText value="Date" />
</f:facet>
<h:outputText value="#{accountList.date}"/>
</h:column>
<h:column>
<f:facet name="header" >
<h:outputText value="Account Name" />
</f:facet>
<h:outputText value="#{accountList.accountName}"/>
</h:column>
</h:dataTable>
</h:column>
</h:dataTable>
By this you would get unique id as outerTable:0:planTable, outerTable:1:planTable.This even extend the unique id to each element in datatable as outerTable:1:planTable:1:columnid , outerTable:0:planTable:0:columnid

JSF datatable hide column border if column is not displayed(rendered)

I have a JSF data table
<h:dataTable id="memberTable" value="#{bean.pList}" border="0"
rowClasses="rowEven rowOdd" var="item">
<h:column rendered="#{item.isDisplay == Y}">
<h:outputText value="#{item.visitDate}" >
</h:outputText>
</h:column>
</datatable>
I have more such columns that are rendred based on a condition, when the condition is false and if I have the border=1; I see that blank cells are displayed. How can i display only the columns i want and have other ie. rendered=false not display blank cells?
Thanks,
Sai.
You can't render an entire column based on the condition of a particular row. This makes technically no sense. You need to evaluate the condition based on a property of the parent bean.
<h:dataTable value="#{bean.pList}" var="item">
<h:column rendered="#{bean.display == 'Y'}">
<h:outputText value="#{item.visitDate}" />
</h:column>
</datatable>
Move the public String getDisplay() method to the class behind #{bean}.
Note that I also fixed some other mistakes in your EL expression. You shouldn't prefix the property name with is or get and you should quote string values. You can also better make it a boolean property.
private boolean display;
public boolean isDisplay() {
return display;
}
with
rendered="#{bean.display}"

How to set row level rendering in JSF <h:datatable>

How can i set row level rendering in datatable JSF.
<h:dataTable styleClass="tablesub" border="0" value="#{historyQuestBean.answerMasterList[row].inputTextKeySet}" var="option">
<h:column>
<h:outputText value="#{option.sectionShortName}:"/>
</h:column>
<h:column>
<h:outputText value="#{option.type}:"/>
</h:column>
</h:dataTable>
I want to render only those rows who have status true.
How can I do this?
Easier and Simplest thing is to pass the list of values with status = true
In <h:datatable> you can give a style class within rowClasses say renderer and in that class specify a condition like display:#{option.status==true}?'block':'none'. This will evaluate the EL and accordingly place the style of that <tr\> to display or not.
Another option is to use <ui:repeat> instead of <h:datatable>, Here you can place rendered condition for the <tr>.

Resources