My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Se încarcă...

Petition by Java EE Guardians

Twitter

luni, 26 ianuarie 2015

[OmniFaces utilities (2.0)] Update the row/column of the given UIData component at the given zero-based row/column index


[OmniFaces utilities] The updateRow() method updates the row of the given UIData component at the given zero-based row index. This will basically update all direct children of all UIColumn components at the given row index. The updateColumn() method updates the column of the given UIData component at the given zero-based column index. This will basically update all direct children of the UIColumn component at the given column index in all rows.

Note that the to-be-updated direct child of UIColumn must be a fullworthy JSF UI component which renders a concrete HTML element to the output, so that JS/ajax can update it. So if you have due to design restrictions for example a <h:panelGroup rendered="..."> without an ID, then you should give it an ID. This way it will render a <span id="..."> which is updateable by JS/ajax.

Method (for row) - just a minor change in OmniFaces 2.1; a part of code was moved in a private method:


Omni-Util method (for column) - just a minor change in OmniFaces 2.1; a part of code was moved in a private method:

Usage:

We create a table with 10 rows and 4 columns. The rows are numbered from 0-9 and the columns from 0-3. We refer to the row/column to update by this index (0 - first row/column, 1- second row/column...):

JSF Page: 
<h:form id="playerFormId">
 <h:dataTable id="tablePlayerId" binding="#{playersTable}"
              value="#{playersBean.players}" var="t" border="1">
  <h:column>
   <f:facet name="header">Ranking</f:facet>
   <h:panelGroup id="rankingId">#{t.ranking}</h:panelGroup>
  </h:column>
  <h:column>
   <f:facet name="header">Name</f:facet>
   <h:panelGroup id="playerId">#{t.player}</h:panelGroup>
  </h:column>
  <h:column>
   <f:facet name="header">Age</f:facet>
   <h:panelGroup id="ageId">#{t.age}</h:panelGroup>
  </h:column>
  <h:column>
   <f:facet name="header">Birthplace</f:facet>
   <h:panelGroup id="birthplaceId">#{t.birthplace}</h:panelGroup>
  </h:column>                    
 </h:dataTable>         

 <f:ajax>
  <h:commandButton value="Update First Row"              
    action="#{playersBean.updateRow(playersTable, 0)}" />
  <h:commandButton value="Update Last Row"
    action="#{playersBean.updateRow(playersTable, 9)}" />
  <h:commandButton value="Update Random Row"
    action="#{playersBean.updateRow(playersTable, playersBean.randomRow(10))}" />
               
  <h:commandButton value="Update First Column"
    action="#{playersBean.updateColumn(playersTable, 0)}" />
  <h:commandButton value="Update Last Column"
    action="#{playersBean.updateColumn(playersTable, 3)}" />
  <h:commandButton value="Update Random Column" \
    action="#{playersBean.updateColumn(playersTable, playersBean.randomColumn(4))}" />
 </f:ajax>
</h:form> 

JSF Bean:
import org.omnifaces.util.Ajax;
...
@ManagedBean
public class PlayersBean ... {

 List<Players> players = new ArrayList<>();

 public PlayersBean() {
  players.add(new Players(...)); //add 10 players
  ...
 }

 public void updateRow(UIData table, int index) {
  long seed = System.nanoTime();
   Collections.shuffle(players, new Random(seed));
   Ajax.updateRow(table, index);
 }
   
 public void updateColumn(UIData table, int index) {
  long seed = System.nanoTime();
  Collections.shuffle(players, new Random(seed));
  Ajax.updateColumn(table, index);
 }
   
 public int randomRow(int max){
  return new Random().nextInt(max);
 }
   
 public int randomColumn(int max){
  return new Random().nextInt(max);
 }

 //getters and setters
 ...
}

Output (notice that even if we shuffle the whole list that populate the table, only the specified rows/columns are actually updated):
  • update first row test
  • update last column

If you need to update rows of a table that supports pagination then you need to install OmniFaces 2.2 and follow the post , Update the row of the given UIData component at the given zero-based row index in tables with pagination.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors