How to include multiple columns from extra file in <p:dataTable>? - jsf-2

I use objects of different classes inside a <p:dataTable> and want to conditionally render multiple <p:column> for the different classes. I do not want to use <p:columns> as I would have to provide the data on what columns should be rendered mostly from a bean and I just want to do it in Facelets.
Actually I could do it just like shown below but for many different classes with different properties this would grow messy. I want all columns that are rendered on a certain condition included from another file.
<p:dataTable value="#{myBean.object} var="object">
<p:column rendered="#{myBean.classOfObject == 'Car'}" />
#{object.yearOfConstruction}
</p:column>
<p:column rendered="#{myBean.classOfObject == 'Person'}">
#{object.dateOfBirth}
</p:column>
</p:dataTable>

I am still open to suggestions to the following approach as well as new ones:
datatable.xhtml
<c:set var="isCar" value="#{param.type == 'Car'}" />
<p:dataTable value="#{myBean.object} var="object">
<ui:include src="car.xhtml"/>
</p:dataTable>
car.xhtml
<p:column rendered="#{isCar}" />
#{object.yearOfConstruction}
</p:column>
<p:column rendered="#{isCar}" />
#{object.color}
</p:column>
I am still not happy with the repeating rendered-condition.

Related

Remove sort indicators Primefaces DataTable

I have a Primefaces DataTable with sortable columns. Even though it implements single sort (with and without sortMode="single"), the sort indicators on other columns do not disappear when I sort by a new column. It's purely for aesthetics, but I would like to remove the indicator once another column is sorted. I'm new to jsf and inherited this project, so there may be something happening behind the scenes that I don't understand. Here is a snippet from the xhtml:
<p:dataTable id="searchResults"
value="#{searchController.searchResults}"
var="emr"
selection="#{searchController.selectedEmr}"
rowKey="#{emr.emrid}"
selectionMode="single" widgetVar="theTable"
scrollable="true"
resizableColumns="true"
stickyHeader="true"
rowIndexVar="rowIndex"
sortBy="#{emr.emrid}"
styleClass="stdSearchResult">
<f:facet name="header">
Search Results
</f:facet>
<p:column style="width:16px">
<p:rowToggler />
</p:column>
<p:column headerText="Emr ID" sortBy="#{emr.emrid}" styleClass="wrap">
<h:outputText value="#{emr.emrid}" title="emrId"/>
</p:column>
Table:
Seems like an obvious name now, but removing stickyHeader="true" accomplished the desired functionality. Thank you for the assistance.

show data in ascending or descending manner

We are using JSF and primefaces to develop application, i have a scenario where it will show data in the table and on click on any column header the data should get sorted either ascending/descending. BUt problem is the sort functionality is not working nor throwing any kind of errors to debug. Below is the code i have written:
Please let me know do i need to include any other attributes to make sorting work.
Thanks.
Change your columns like this
<p:column id="NumberCol" sortBy="#{OrderInfo.orderNumber}" headerText="Order Number" >
<h:outputText value="#{OrderInfo.orderNumber}" />
</p:column>
<p:column id="NumberCol" sortBy="#{OrderInfo.Number}" headerText="Number" >
<h:outputText value="#{OrderInfo.Number}" />
</p:column>
This should work.
Hope this helps

filterBy (primefaces 3.2) doesn't work with date

am usign jsf 2.0 + primefaces 3.2. i have a problem with filterBy when the data is a date but it works with other types of data.
<p:column sortBy="#{item.dateNaissance}" filterBy="#{item.dateNaissance}">
<f:facet name="header">
<h:outputText value="#{bundle.ListEtudiantTitle_dateNaissance}"/>
</f:facet>
<h:outputText value="#{item.dateNaissance}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:outputText>
</p:column>
default filtering doesn't work for date field, though you can use advanced filtering option and provide a filter server side method which does the filtering from the data set for you.
Basically you will specify your metho on key up event and filter record as per your need, default assumes everything to be String.

How to update particular table column of particular row on checkBox click

I have a dataTable in which i have five columns. First , fourth and fifth columns consists of checkboxes. I want that if user select or deselect checkbox of a particular row then the all checkboxes of that row also effected. I am using viewScope. Is this possible with view scope ?
Thanks
Just specify the relative IDs of those checkboxes in the render/update.
<p:column>
<h:selectBooleanCheckbox id="column1" value="#{item.column1}">
<f:ajax render="column4 column5" />
</h:selectBooleanCheckbox>
</p:column>
...
<p:column>
<h:selectBooleanCheckbox id="column4" value="#{item.column4}" />
</p:column>
<p:column>
<h:selectBooleanCheckbox id="column5" value="#{item.column5}" />
</p:column>
I would invoke a bean method when the tick changes in the first column. There you can set the calues for the other selects. After setting, refresh.

JSF 2, Primesfaces: Update cell on selectOneMenu selection in Incell Editing

I'm using Primesfaces' Incell editing in a p:dataTable.
When choosing a new id in the selectOneMenu, I'd like to update the 'name' field in the same row without having to wait until the user presses the ok check mark for the name field to be updated. Objects' name attribute is updated in bean.idEdited(object).
I was hoping I could just use f:ajax render="name" to update the other field like this:
<h:form>
<p:dataTable var="object" value="#{bean.objects}"
<p:column>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{bean.objectId}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{bean.objectId}">
<f:selectItems value="#{bean.objectIds}" />
<f:ajax listener="#{bean.idEdited(object)}"
render="name" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column>
<h:outputText id="name" value="#{object.name}" />
</p:column>
</p:dataTable>
</h:form>
When I try render="name" I get this:
SEVERE: Error Rendering View[/logicalAddress.xhtml]
java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed
at org.apache.catalina.connector.Request.doGetSession(Request.java:2867)
...
Using
Primesfaces 2.2.1
Netbeans 7.0.1
JSF Bundled with Netbeans
Ideas, other ways or salvation?
edit: p:ajax renders the same result.
This is happening because when you are calling ajax to render the page, that page is already rendered.
TIP: If you use primefaces you could also use primefaces components where this is possible. Also upgrade to Primefaces 3.0.M4 for a better implementation. You also have a primefaces selectOneMenu component, and you can use primefaces ajax (p:ajax event="onchange") with update attribute (update the entire form)
updating the entire form with p:ajax certainly updates the name field but the row will no longer be in edit mode after this. If the user wants to edit the other cell in the same row, he has to explicitly go into edit mode

Resources