Render column depending on row rendering - jsf

I have a dataTable like this one:
<h:dataTable value="#{myList}" var="myVariable" >
<h:column>
<f:facet name="header"/>
<h:graphicImage style="display:block; margin:0px auto" value="imageURL" rendered="#{condition}"/>
</h:column>
<h:column>
<f:facet name="header">
Attribute1
</f:facet>
<h:outputText style="display:block; text-align:center" value="#{myVariable.attribute1}" />
</h:column>
<h:column>
<f:facet name="header">
Attribute2
</f:facet>
<h:outputText style="display:block; text-align:center" value="#{myVariable.attribute2}" />
</h:column>
<h:column>
<f:facet name="header">
Attribute3
</f:facet>
<h:outputText style="display:block; text-align:center" value="#{myVariable.attribute3}" />
</h:column>
</h:dataTable>
As you can see, I have multiple columns displaying all the attributes of a variable, and an image which is shown in a column if a condition is satisfied.
However, not fulfilling that condition give us a table with an empty column which I would like to not being shown.
Is there any way to hide that column when no image is shown in all the rows?

Related

Using inputRowSelect in a dynamic dataTable

Inside the dataTable dynamically creates a dataTable with the records, how can I get the number of rows for each dataTable that was created?
My Table:
<h:dataTable value="#{bean.tableId}"
var="tableIdInfo" rows="10">
<h:column>
<f:facet name="header">
<h:outputText value="id_date"></h:outputText>
</f:facet>
<h:outputText value="#{tableIdInfo.id_date}"
styleClass="outputText">
<hx:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>
</h:column>
<h:column>
<h:dataTable value="#{bean.tableList}" var="tInfo">
<h:column>
<f:facet name="header">
</f:facet>
<hx:inputRowSelect value="#{bean.rows}">
</hx:inputRowSelect>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText styleClass="outputText"
value="Date create"></h:outputText>
</f:facet>
<h:outputText value="#{tInfo.cDate}">
<hx:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>
</h:column>
</h:dataTable>
</h:column>
</h:dataTable>
I assume tableId is a list of data.
then add a column for getting row numbers.
<h:column>
<!-- display currently selected row number -->
<f:facet name="header">No</f:facet>
#{bean.tableId.rowIndex + 1}
</h:column>

Hide data table automatically if there is no data to show

I have this simple data table:
<h:dataTable id="table1" value="#{fournisseurbean.BC.listematpilotaccess1}" style="width : 900px; " var="item"
border="1">
<h:column>
<f:facet name="header">
<h:outputText value="idmatpilotaccess1n" />
</f:facet>
<h:outputText value="#{item.idmatpilotaccess1}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="serie" />
</f:facet>
<h:outputText value="#{item.serie}" />
</h:column>
</h:dataTable>
I would like to not show this table if there is no data to show, so when
#{fournisseurbean.BC.listematpilotaccess1} is empty. How can I achieve this?
Add a rendered attribute to the tag
<h:dataTable rendered="#{not empty fournisseurbean.BC.listematpilotaccess1}">
This will render the component if the expression evaluates to true.
render its ok but if you want to hide more than the datatble ,for example : a label or a div.
you can use :
<c:if test="#{beanMb.beanList.size() gt 0 }">
....code
</c:if>

Conditional column rendering

I'm working with jsf 2.0. I have this datatable
<h:dataTable value="#{agreement.licenseProducts}"
var="licenseProduct"
styleClass="pnx-table pnx-table-bdr pnx-table-unstyled mp-license-info">
<h:column>
<f:facet name="header">
<h:outputText value="Product" />
</f:facet>
<h:outputText value="#{licenseProduct.description}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Serial Number" />
</f:facet>
<h:outputText value="#{licenseProduct.serialNumber}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{agreement.labelForConcurrency}" />
</f:facet>
<h:outputText value="#{licenseProduct.concurrent}" />
</h:column>
<ui:fragment rendered="#{agreement.managementTool != 'NONE'}">
<h:column>
<f:facet name="header">
<h:outputText value="#{agreement.labelForLicenseType}" />
<span class="pnx-icon-right pnx-icon-info pnx-tooltip">
<div class="pnx-tooltip-content">
<h:outputText value="Tooltip content" />
</div>
</span>
</f:facet>
<h:outputText value="#{licenseProduct.licenseBase}" />
</h:column>
</ui:fragment>
<h:column>
<f:facet name="header">
<h:outputText value="#{agreement.labelForSeatCount}" />
</f:facet>
<h:outputText value="#{licenseProduct.seats}" />
</h:column>
</h:dataTable>
The problem is that the ui:fragment part is not working. No matter what the attribute's value is, it will NEVER show the column.
Any ideas?
--EDIT--
Just in case, I have other ui:fragments that depend on that same attribute, and they do render correctly depending on the attribute's value. I'm sure it has to do with the dataTable and the columns.
The rendered's attribute of <h:column> tag performs simply the job :
<h:column rendered="#{agreement.managementTool != 'NONE'}">
...
</h:column>

Why does <h:column><f:facet name="header"><h:outputText> render as th instead of td

I have below code:
<h:dataTable class="pretty" value="#{ftManagedBean.ftDataModel}" >
<h:column>
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Outstanding"/>
</f:facet>
</h:column>
</h:dataTable>
But it prints Name and Outstanding between <th></th> tags. Why? I need them in <td></td> tags. How can I do that?
Name and Outstanding end up in <th> elements because you placed them inside <f:facet name="header"> tags. Remove those and they'll be output to <td> tags.
<h:dataTable class="pretty" value="#{ftManagedBean.ftDataModel}" >
<h:column>
<h:outputText value="Name"/>
</h:column>
<h:column>
<h:outputText value="Outstanding"/>
</h:column>
</h:dataTable>
See the JSF HTML Tag Reference for an example on creating a table.
You have to change your code into this:
<h:dataTable class="pretty" value="#{ftManagedBean.ftDataModel}" >
<h:column>
<h:outputText value="Name"/>
</h:column>
<h:column>
<h:outputText value="Outstanding"/>
</h:column>
</h:dataTable>
If you wrap your output text within the <f:facet name="header"> this corresponds to <th> (table header), otherwise they will be printed in <td>.
I try this solution and ok.
(http://www.coderanch.com/t/431222/JSF/java/dynamically-set-panel-header-condition)
<rich:dataGrid value="#{myMB.student.list}" rendered="#{!empty myMB.student and !empty myMB.student.list}" var="st" rowKeyVar="row">
<rich:panel>
<f:facet name="header">
<h:panelGroup id="panelHeader">
<h:outputText value="Just one student" id="header1" required="true" rendered="#{!myMB.manyStudents}"/>
<h:outputText value="#{row+1}ยบ Student" id="header2" required="true" rendered="#{myMB.manyStudents}"/>
</h:panelGroup>
</f:facet>
<rich:panel>

Where to keep h:messages in a h:datatable while validating two h:columns

I am using one h:selectbooleancheckbox in each row to make 2 columns editable.
Have a look at my JSF page
<h:dataTable id="editTable" styleClass = "listtable" value="#{bean.GroupList}" var="group" border="1" first="0" rows="8" width="75%" frame="hsides" rules="all" cellpadding="5" headerClass="tableheading" rowClasses="firstrow, secondrow">
<f:facet name="header">
<h:outputText value="Groups"></h:outputText>
</f:facet>
<h:column>
<f:facet name="header">
<h:outputText value="GroupId"></h:outputText>
</f:facet>
<h:outputText value="#{group.Id}" rendered="#{not bean.checked[group.Id]}"></h:outputText>
<h:inputText value="#{group.Id}" rendered="#{bean.checked[group.Id]}" required="true"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="GroupName"></h:outputText>
</f:facet>
<h:outputText value="#{group.Name}" rendered="#{not bean.checked[group.Id]}"></h:outputText>
<h:inputText value="#{group.Name}" rendered="#{bean.checked[group.Id]}" required="true"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Check to Enable/Disable"></h:outputText>
</f:facet>
<h:selectBooleanCheckbox value="#{bean.checked[group.Id]}" />
</h:column>
</h:dataTable>
I have required="true" for GroupId and GroupName columns.
I am not getting where to keep h:messages for each column to display requiredmessage
Please help.
You need to use <h:message> instead to display errors specific to an input element. The <h:messages> will display all messages which are not covered by any <h:message>.
<h:column>
<f:facet name="header">
<h:outputText value="GroupId"></h:outputText>
</f:facet>
<h:outputText value="#{group.Id}" rendered="#{not bean.checked[group.Id]}"></h:outputText>
<h:inputText id="groupId" value="#{group.Id}" rendered="#{bean.checked[group.Id]}" required="true"/>
<h:message for="groupId" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="GroupName"></h:outputText>
</f:facet>
<h:outputText value="#{group.Name}" rendered="#{not bean.checked[group.Id]}"></h:outputText>
<h:inputText id="groupName" value="#{group.Name}" rendered="#{bean.checked[group.Id]}" required="true"/>
<h:message for="groupName" />
</h:column>

Resources