0 Replies Latest reply on Feb 16, 2013 6:20 AM by solstice_pan

    Datatable dynamic columns

      Hi Guys,

       

      i know this Subject has been discussed already, but the Reason i want to discuss it again is that i don´t find that this is so simple like in the Example. I´m just using Richfaces 3.3.3 with JSF 1.2 and need a dynamic Datatable, so i started to try by the Richfaces showcase example. With this simple example it was very easy for me to reproduce it, so i give another try with my usecase...and it won´t be run correctly...so let me explain

       

      i have a Bean like this.

       

       

       

      public class TableBean {
           
           private String[] COLUMN_ONE_DATA_ARRAY = { "Column_ONE_Data1",
                                    "Column_ONE_Data2", "Column_ONE_Data3", "Column_ONE_Data4",
                                    "Column_ONE_Data5" };
      
           private String[] COLUMN_TWO_DATA_ARRAY = { "Column_TWO_Data1",
                                    "Column_TWO_Data2", "Column_TWO_Data3", "Column_TWO_Data4",
                                    "Column_TWO_Data5" };
      
           private List<Derivate> columnHeaderList ; 
      
           private List<Product[]> productListe ; 
      
           
          public List<Derivate> getColumnHeaderList() {
              return columnHeaderList == null ? Collections.EMPTY_LIST : columnHeaderList;
          }
      
          public void setColumnHeaderList(List<Derivate> derivateList) {
               this.columnHeaderList = derivateList;
          }
      
          
           public List<Product[]> getProductListe() {
                return productListe;
           }
      
           public void setProductListe(List<Product[]> productListe) {
                this.productListe = productListe;
           }
           
           public void performFillDerivateAndProductListActionEvent(ActionEvent event) {
                fillDerivateList();
                fillProductListe();
           }
           
           // THIS IS MY PROBLEM, AND CURRENTLY I HAVE NO GLUE HOW TO DO IT RIGHT
           private void fillProductListe() {
                if ( productListe == null) {
                     productListe = new ArrayList<Product[]>();
                }
                productListe.clear();
                int derivateSize = columnHeaderList.size();
                for ( int i = 0 ; i < derivateSize; i++) {
                     Derivate d = columnHeaderList.get(i);
                     for ( int j = 0 ; j < d.getProducts().size() ; j++ ) {
                      // HOW IT´s RIGHT..?????
                          Product[] p = new Product[derivateSize];
                          p[i] = new Product();
                          p[i].setProductName(d.getProducts().get(j).getProductName());
                         productListe.add(p);
                          }
                }
      
                     // THIS WORKS CORRECTLY
                     //for ( int i = 0 ; i < 9 ; i++ ) {
                     //           Product[] p = new  Product[2];
                     //     p[0] = new Product("COLUMN_DATA_ONE_" +i );
                     //     p[1] = new Product("COLUMN_DATA_TWO_"  +i);
                     //     productListe.add(p);
                     //}
      
                
                }
      
           
           private Derivate createDerivate(String marketName){
                Derivate d = new Derivate();
                d.setDerivateName(marketName);
                return d;
           }
           
           private void fillDerivateList() {
               if ( columnHeaderList == null ) {
                    columnHeaderList = new ArrayList<Derivate>();
               }
               columnHeaderList.clear();
               Derivate colOneDeriv =createDerivate("Column ONE");
                for ( int i = 0 ; i < COLUMN_ONE_DATA_ARRAY.length ; i++ ) {
                     Product p = new Product();
                     p.setProductName(COLUMN_ONE_DATA_ARRAY[i]);
                     colOneDeriv.addProduct(p);
                }
                Derivate colTwoDeriv = createDerivate("Column TWO");
                     for ( int i = 0 ; i < COLUMN_TWO_DATA_ARRAY.length ; i++ ) {
                               Product p = new Product();
                               p.setProductName(COLUMN_TWO_DATA_ARRAY[i]);
                               colTwoDeriv.addProduct(p);
                     }
                     columnHeaderList.add(colOneDeriv);
                     columnHeaderList.add(colTwoDeriv);
           }
      
      }
      
      
           public class Product {
        
                private String productName ;
      
      
                public Product() {
                }
        
                public Product(String productName) {
                          this.productName = productName;
                }
      
      
                public String getProductName() {
                          return productName;
                }
      
      
                public void setProductName(String productName) {
                          this.productName = productName;
                } 
           }
      
           public class Derivate {
        
                private String derivateName ;
                private List<Product> products = new ArrayList<Product>() ;
        
                public Derivate() {
                }
        
                public Derivate(String derivateName) {
                          this.derivateName = derivateName;
                }
      
      
                public String getDerivateName() {
                          return derivateName;
                }
      
      
                public void setDerivateName(String derivateName) {
                          this.derivateName = derivateName;
                }
      
      
                public List<Product> getProducts() {
                          return products;
                }
      
      
                public void setProducts(List<Product> products) {
                          this.products = products;
                } 
        
                public void addProduct(Product p) {
                          products.add(p);
                }
        
           }
      
      
      IN MY XHTML TEST PAGE
      
      
      <h:form>
         <a4j:commandButton
               value="FILL LIST"
               actionListener="#{tablePojo.performFillDerivateAndProductListActionEvent}"
               reRender="ajaxpnl"
               immediate="true"
         />
      </h:form>
      
      
      <rich:spacer/>
        
      <a4j:outputPanel
        ajaxRendered="false"
        id="ajaxpnl
      >
           
           <rich:panel
                header="TEST"
           >
                <h:form>
                     <rich:dataTable
                          width="700px"
                          value="#{tablePojo.productListe}"
                          var="product"
                     >
                          <c:forEach
                               items="#{tablePojo.columnHeaderList}"
                               var="column"
                               varStatus="status"
                           >
                           <rich:column
                                sortBy="#{column.derivateName}"
                           >
                                <f:facet name="header">
                                     <h:outputText value="#{column.derivateName}" />
                                </f:facet>
                                     <h:outputText value="#{product[status.index].productName}" /> 
                           </rich:column>
                          </c:forEach> 
                       </rich:dataTable>
                </h:form>
           </rich:panel>
      </a4j:outputPanel>
      

      The Working Example looks like how i want it

       

      work.png

      and the not working example....

      notwork.png

      any help would be very appreciate !!!