JSF dataTable: conditional filter facets - jsf

I've got a dynamic dataTable and would like to display different filter types depending on the column but it seems that I can only use one object within a filter facet at a time. If I try to use multiple input types I get a
java.lang.ClassCastException: javax.faces.component.UIPanel cannot be cast to javax.faces.component.ValueHolder
My current (not working) code looks like this:
<f:facet name="filter">
<p:inputText onkeyup="PF('myTable').filter()" rendered="#{column.filterType=='TEXT'}" />
<p:selectOneMenu onchange="PF('myTable').filter()" rendered="#{column.filterType=='LIST'}">
<f:selectItems value="#{column.filterMap}" />
</p:selectOneMenu>
</f:facet>
The inputText and selectOneMenus are displayed correctly, but I can't filter my table because of the ClassCast Exception.
If I only use one of the input types, the filtering works as expected.
Is it possible to conditionally show different filter facets or input types within the facet?

The only "solution" I found, is to always use selectOneMenus and make them editable and look like inputTexts if the filter type is "TEXT"
<p:selectOneMenu onchange="PF('myTable').filter()" editable="#{column.filterType=='TEXT'}" styleClass="#{column.filterType}">

following your 2nd comment I tested this code and it works; The idea is that the filter depends on the column, so:
<p:column filterBy="#{column.attribute}" filterMatchMode="contains" rendered="#{column.filterType=='TEXT'}" >
<f:facet name="filter">
<p:inputText onkeyup="PF('myTable').filter()" />
</f:facet>
</p:column>
<p:column filterBy="#{column.attribute}" filterMatchMode="equals" rendered="#{column.filterType=='LIST'}">
<f:facet name="filter">
<p:selectOneMenu onchange="PF('myTable').filter()" >
<f:selectItems value="#{column.filterMap}" />
</p:selectOneMenu>
</f:facet>
</p:column>
you show and hide the column, not the filter.
NB : The filter mode of an inputText is contains AND for a List is Equals.

Related

Primefaces datatable Identify if the current column is of date type

I am trying to make a common datatable using primefaces.
I want to check if the current column of datatable is of Date type. If it is a Date I want to add DatetimeConverter -
Here is my code -
<ui:component>
<p:dataTable id="#{tableId}" value="#{data}" var="row" styleClass="stdTable vertLines fndTable vertLinesRightBorder"
style="table-layout:fixed; border-color: #dddddd;">
<p:columns value="#{tableColumns}" var="column" sortBy="#{row[column.property]}">
<f:facet name="header">
#{column.header}
</f:facet>
#{row[column.property]}
</p:columns>
</p:dataTable>
</ui:component>
I am calling the above xhtml as follows-
<ui:include src="table.xhtml">
<mbcpos:param name="tableId" value="#{me.id}hTabel1" />
<mbcpos:param name="data" value="#{taskListBean.receivedOwnerTasks}" />
<mbcpos:param name="tableColumns"
value="#{me.columns}" />
</ui:include>
Can we identify the column datatype :
e.g., #{row[column.property]} equals Date
If the column is a date I want to format the date.
Your column model should be where you put the data type it holds and other data type presentation related things like masks, currency, alignment etc.
On the page, for each data type (according to your needs), inside the p:columns
you should do something like this:
<h:outputText rendered="#{column.dateType}" value="#{row[column.property]}">
<f:convertDateTime pattern="dd/MM/yyyy"/>
</h:outputText>

Primefaces dataTable filter selectOneMenu not working

I am using PrimeFaces 5.1, In my project dataTable to filter used.In text filter is work fine but dropdown filter is not working properly (i.e) In dropdown I show department,First time I choose any value from dropdown is work fine anothertime I choose dropdown It not return any value show in dataTable.I choose select one first value from dropdown also throw null pointer exception.
<p:dataTable id="datalist" widgetVar="datalist" var="user" value=#{beanList.userList}>
<p:column headerText="Department" filterBy="#{user.deptname}"
filterMatchMode="exact" >
<f:facet name="filter">
<p:selectOneMenu onchange="PF('datalist').filter()">
<f:selectItem itemLabel="ALL" itemValue="#{null}"
noSelectionOption="true" />
<f:selectItems value="#{datalist.deptList}" />
</p:selectOneMenu>
</f:facet>
<h:outputText value="#{user.depatname}" />
</p:column>
</p:dataTable>
My doubt is default value ALL click and second time select any value return null or no data show in dataTable.
Since I don't know the scope of your Managed Bean: Try a scope longer than request (see PrimeFaces 5.1 User Documentation), and provide a value "filteredValue" for your table, like this:
<p:dataTable id="datalist" widgetVar="datalist" var="user"
value="#{beanList.userList}" filteredValue="#{beanList.filteredUserList}">
With that, you make sure to keep your filtered table/list in a field in your managed bean and the contents won't get lost.
Also, make sure your Managed Bean class is serializable (See this stackoverflow post)

Column header not visible when column is conditionally rendered

I have a h:dataTable. I would like to render a row conditionally as follows:
<h:dataTable value="#{myController.entities}" var="entity">
<h:column rendered="#{entity.selected}">
<f:facet name="header">
<h:outputText value="header" />
</f:facet>
<h:outputText value="#{entity.name}" />
</h:column>
</h:dataTable>
In my controller there is variable List <Entity> entities which has getters and setters.
Entity class contains two variables : boolean selected and String name with its getters and setters.
When #{entity.selected} is false, then the row is hidden. When #{entity.selected} is true, then the row is shown, but without header. When I do not use the rendered attribute on the column my header becomes visible.
This question is similar to p:column header facet is not shown when rendered attribute is used on p:column
But none of the solution given works for me and Iam not using primefaces.
Can somebody help me with this? Iam new to JSF and the mistake might be something silly and fix fairly easy, but please help me figure it out. Thanks.
Iam posting the outputs that I get when I tried the above code.
**Header**
name 0
name 1
name 2
name 3
name 4
This output shows the datatable using simply <h:column> without trying to render it conditionally. The header can be seen.
Now when I use conditional rendering:
name 1
name 3
In this output Iam trying to use conditional rendering <h:column rendered="#{entity.selected}">. As you can see that for "name 0", "name 3" and "name 5" the #{entity.selected} is false and hence its not rendering.
This is exactly what I need but only if the header shows up.
I want to render my row invisible where the #{entity.selected} value is false. Also show the column header. Can this be possible?
You need to put the rendered condition on the cell, not on the column.
<h:dataTable value="#{myController.entities}" var="entity">
<h:column>
<f:facet name="header">
<h:outputText value="header" />
</f:facet>
<h:outputText value="#{entity.name}" rendered="#{entity.selected}" />
</h:column>
</h:dataTable>
Or, if you've multiple components or plain text representing cell content, wrap it in a <h:panelGroup> or <ui:fragment>.
<h:dataTable value="#{myController.entities}" var="entity">
<h:column>
<f:facet name="header">
header
</f:facet>
<h:panelGroup rendered="#{entity.selected}">
Blah blah #{entity.name} blah blah
</h:panelGroup>
</h:column>
</h:dataTable>
(note: you don't necessarily need h:outputText over all place, see also Is it suggested to use h:outputText for everything?)
Apply if necessary the following CSS on the table to hide empty cells in case you've applied e.g. borders or bgcolors on table cells:
table {
empty-cells: hide;
}
A completely different alternative is to provide a new model which contains only the desired entities:
<h:dataTable value="#{myController.selectedEntities}" var="entity">
<h:column>
<f:facet name="header">
header
</f:facet>
#{entity.name}
</h:column>
</h:dataTable>
Instead of
...
<f:facet name="header">
<h:outputText value="header" />
</f:facet>
...
try simply
...
<f:facet name="header">Header</f:facet>
...

p:selectOneMenu, custom content and editable=true

I have the following usage of the p:selectOneMenu:
<p:selectOneMenu id="selectField"
value="#{someBean.someField}"
converter="#{selectItemConverter}" var="x" editable="true">
<f:selectItems
value="#{selectItemsBean.getSelectItems(tab, field)}" var="si"
itemLabel="#{si.label}" itemValue="#{si}" />
<p:column>
<h:outputText value="#{si.label}" />
</p:column>
<p:column>
<h:graphicImage library="images" name="noway_16x16.png"
title="#{si.disabledReason}" rendered="#{si.disabled}" />
</p:column>
<p:ajax event="change" update="#form" partialSubmit="true" process="selectField" />
</p:selectOneMenu>
As you can see, I use custom content in combination with editable=true. When I submit the form, the Converter gets the label of a selected item as the value, not the actual value. In the HTML page, the values are correct, e.g. <option value="C">C-style mounting</option>. With editable=false, the correct value (e.g. C is sent to the converter, with editable=true the converter retrieves C-style mounting.
What I want is that the user can either select one of the pre-defined items in the list and the server submits that value of the item OR the user enters something and that is submitted as value. But the current behavior is a bit strange - or am I just wanting too much?

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.

Resources