[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:
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