Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking. I'm 24, born in Romania, studied at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff.

Monday, April 16, 2012

How to programmatically access PrimeFaces tags

This is FYI kind tip. Recently, I tried to programmatically acess PrimeFaces tags from Java beans. I found two solutions that can be extrapolated to almost all PrimeFaces tags.

1. Use the findComponent() method

Per example, the below code contains a DataTable, and I want to programmatically to set the zero-relative row number of the first row to be displayed.

<p:layoutUnit position="center" styleClass="welcome">
<p:tabView id="centerTabIds" activeIndex="2">
<p:tab title="LBD">
<p:accordionPanel id="dataId">
<p:tab title="Data Table Viewer">
<h:panelGrid columns="1" cellpadding="5"
style="margin-left: auto; margin-right: auto;">
<h:form id="tableViewFormId" prependId="false">
<p:dataTable id="tableViewId" var="t" value="#{tableViewBean.lazyModel}"
paginator="true" rows="5"
currentPageReportTemplate="{currentPage} of {totalPages}"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink}
{PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
rowsPerPageTemplate="5,10" emptyMessage="No records found"
paginatorPosition="top" first="0">
<f:facet name="header">
Data Table
<c:forEach items="#{tableViewBean.simpleColumns}" var="column">
<p:column headerText="#{column.header}">
<h:outputText value="#{t[]}"/>
<f:facet name="footer">
<c:when test="#{not empty tableViewBean.selectedTable}">
<h:outputText value="#{tableViewBean.selectedTable}" />

And here it is the programmatically solution using the findComponent() method:


This is a tip in a tip – using multiple dynamic data tables and switching between them will get you to an unpleasant situation. PrimeFaces does not reset the data table to the first page every time you switch to another table, but this code fixes that problem.

Even if it is not a good idea to hardcode components ids, this can save a lot of time when no solution is available.

2. Use ActionEvent

Another common scenario is like this: I have a PrimeFaces menu, with several menu items. Each menu item has a value attribute, and all of them indicates the same action listener. I want to get the value of the menu item that was selected. The code looks like this:

<c:forEach items="#{iccAnalysisBean.wellIds}" var="row">
<p:menuitem value="#{row.toString()}"
actionListener="#{iccAnalysisBean.selectedWellAction}" ajax="true"/>

And I’ve accommplish my task like this:

public void selectedWellAction(ActionEvent e) {
String valueOfWell = String.valueOf(e.getComponent().getAttributes().get("value"));

I hope that this will help you when you are stack in such issues!

1 comment:

Anonymous said...

Thank you very much!