Dynamic table in ADF unlimited rows and columns

Original post is here :
http://thepeninsulasedge.com/blog/?p=108    by the author. I just paste this article here to avoid lost.

ADF Faces RC: Dynamic Table

June 17th, 2007

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.

adf faces rcf dynatable

JSPX File

XML:

  1. <?xml version=‘1.0’ encoding=‘windows-1252’?>
  2. <jsp:root xmlns:jsp=http://java.sun.com/JSP/Page&#8221; version=“2.0”
  3.           xmlns:
    f=
    http://java.sun.com/jsf/core&#8221;
  4.           xmlns:
    af=
    http://xmlns.oracle.com/adf/faces/rich&#8221;
  5.           xmlns:
    c=
    http://java.sun.com/jsp/jstl/core&#8221;
    >
  6. <jsp:directive.page contentType=“text/html;charset=windows-1252”/>
  7. <f:view>
  8. <af:document>
  9. <af:form>
  10. <af:panelHeader text=“” inlineStyle=“width:80%”>
  11. <af:panelHeader text=“Dynamic Table Example”>
  12. <af:table partialTriggers=“addRow addColumn”
  13. varStatus=
    “rowStat”
  14. value=
    “#{DynaTable.collectionModel}”
  15. rows=
    “#{DynaTable.collectionModel.rowCount}”
  16. rowSelection=
    “single”
  17. contentDelivery=
    “immediate”
  18. width=
    “100%”
  19. var=
    “row”
    >
  20. <af:column width=“20” rowHeader=“true”>
  21. <af:outputText value=“#{rowStat.index+1}”/>
  22. </af:column>
  23. <c:forEach items=“#{DynaTable.columnNames}” var=“name”>
  24. <af:column headerText=“#{name}” sortable=“true”
  25. sortProperty=
    “#{name}”
    >
  26. <af:inputText value=“#{row[name]}”/>
  27. </af:column>
  28. </c:forEach>
  29. </af:table>
  30. <f:facet name=“toolbar”>
  31. <af:toolbar>
  32. <af:commandToolbarButton id=“addColumn” text=“Add Column”
  33. partialSubmit=
    “true”
  34. actionListener=
    “#{DynaTable.addColumn}”
    />
  35. <af:commandToolbarButton id=“addRow” text=“Add Row”
  36. partialSubmit=
    “true”
  37. actionListener=
    “#{DynaTable.addRow}”
    />
  38. </af:toolbar>
  39. </f:facet>
  40. </af:panelHeader>
  41. </af:panelHeader>
  42. </af:form>
  43. </af:document>
  44. </f:view>
  45. </jsp:root>

Managed Bean: DynaTableBean.java

JAVA:

  1. package com.
    thepeninsulasedge.
    view.
    dynatable.
    managed;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.faces.event.ActionEvent;
  8. import org.apache.myfaces.trinidad.model.CollectionModel;
  9. import org.apache.myfaces.trinidad.model.SortableModel;
  10. public
    class DynaTableBean
    {
  11. private SortableModel model;
  12. private List<String> columnNames;
  13. public DynaTableBean
    (
    )
    {
  14.         columnNames =
    new ArrayList<String>
    (
    );
  15. }
  16. // Add a new row to the model
  17. public
    void addRow
    (
    ActionEvent evt
    )
    {
  18. if
    (!columnNames.
    isEmpty
    (
    )
    )
    {
  19. Map newRow = createNewRowMap
    (
    );
  20. (
    (
    List
    )model.
    getWrappedData
    (
    )
    ).
    add
    (newRow
    );
  21. }
  22. }
  23. // Get table model
  24. public CollectionModel getCollectionModel
    (
    )
    {
  25. if
    (model ==
    null
    )
    {
  26.             model =
    new SortableModel
    (
    new ArrayList<Map>
    (
    )
    );
  27. }
  28. return model;
  29. }
  30. // Get Column Names
  31. public
    Collection getColumnNames
    (
    )
    {
  32. return columnNames;
  33. }
  34. // Add column
  35. public
    void addColumn
    (
    ActionEvent evt
    )
    {
  36. // Create new column name
  37. String colName =
    (columnNames.
    size
    (
    )+
    1
    ) +
    “”;
  38. // Add column name to list of names
  39.         columnNames.
    add
    (colName
    );
  40. // Create new row
  41. Map newRow = createNewRowMap
    (
    );
  42. // Add new column name to new row
  43.         newRow.
    put
    (colName,
    null
    );
  44. // Update existing model
  45.         addColumnToCurrentModel
    (colName
    );
  46. }
  47. // Add column to existing rows
  48. private
    void addColumnToCurrentModel
    (
    String name
    )
    {
  49.         List<Map> list =
    (List<Map>
    )model.
    getWrappedData
    (
    );
  50. for
    (
    Map row:list
    )
    {
  51.             row.
    put
    (name,
    null
    );
  52. }
  53. }
  54. private
    Map createNewRowMap
    (
    )
    {
  55. Map newRow =
    new
    HashMap
    (
    );
  56. for
    (
    String colName:columnNames
    )
    {
  57.             newRow.
    put
    (colName,
    null
    );
  58. }
  59. return newRow;
  60. }
  61. }
  62.  

 

Advertisements