Sunday, April 5, 2009

ADF Faces RC: Dynamic Table

Example

I have seen a number of posts in the forums concerning “How to create dynamic tables with RCF?”. As such, here is code example that creates an af:table with commandButtons that allow a user to dynamically add both rows and columns to a table, creating the oh so desired Ajax goodness.
Now, there a couple of things to note about this example. Namely, this example does not dynamically create components in a backing bean. Instead, a “best practices” approach is taken that enforces better separation between data and UI manipulation. Thus, the dynamic nature of the table is driven by updates to the model rather directly manipulating UI components.
JSPX File
Managed Bean: DynaTableBean.java
JAVA:
1.package com.thepeninsulasedge.view.dynatable.managed;
2.
3.import java.util.ArrayList;
4.import java.util.Collection;
5.import java.util.HashMap;
6.import java.util.List;
7.
8.import java.util.Map;
9.
10.import javax.faces.event.ActionEvent;
11.
12.import org.apache.myfaces.trinidad.model.CollectionModel;
13.import org.apache.myfaces.trinidad.model.SortableModel;
14.
15.public class DynaTableBean {
16. private SortableModel model;
17. private List columnNames;
18.
19.public DynaTableBean() {
20. columnNames = new ArrayList();
21. }
22.
23.// Add a new row to the model
24. public void addRow(ActionEvent evt){
25.if(!columnNames.isEmpty()){
26. Map newRow = createNewRowMap();
27.((List)model.getWrappedData()).add(newRow);
28.}
29.}
30.
31.// Get table model
32. public CollectionModel getCollectionModel(){
33. if(model == null){
34. model = new SortableModel(new ArrayList());
35. }
36. return model;
37. }
38.
39.// Get Column Names
40.public Collection getColumnNames(){
41. return columnNames;
42. }
43.
44.// Add column
45.public void addColumn(ActionEvent evt){
46. // Create new column name
47.String colName = (columnNames.size()+1) + “”;
48.// Add column name to list of names
49. columnNames.add(colName);
50. // Create new row
51. Map newRow = createNewRowMap();
52. // Add new column name to new row
53. newRow.put(colName,null);
54.// Update existing model
55.addColumnToCurrentModel(colName);
56.}
57.
58.// Add column to existing rows
59. private void addColumnToCurrentModel(String name){
60. List list = (List)model.getWrappedData();
61. for(Map row:list){
62. row.put(name,null);
63. }
64. }
65.
66. private Map createNewRowMap(){
67.Map newRow = new HashMap();
68. for(String colName:columnNames){
69. newRow.put(colName,null);
70. }
71.return newRow;
72.}
73.
74.}
IN BACKING BEAN
In backing I have set ‘DynamicTable’ to instantiate it manually, if the session bean might not be get right.
public void setDocument1(RichDocument document1) {
this.document1 = document1;
FacesContext ctx = FacesContext.getCurrentInstance();
Application app = ctx.getApplication();
ValueBinding bind = app.createValueBinding(”#{DynamicTable}”);//session bean name which has given in adf-config
Object o = bind.getValue(ctx);
DynaTableBeantemp = (DynaTableBean) o;
// temp.setUserName(”Test “);
// temp.setDateAndTime(Calendar.getInstance().getTime().toString());

}

0 comments: