2 Replies Latest reply on Dec 21, 2009 2:38 PM by Shravan Kumar Mali

    Building a datatable from a DB Table

    Shravan Kumar Mali Newbie
      I am trying to populate data from an database table, where I cannot freeze the design of the table, it is completely dynamic in nature, I need to dynamically generate columns and there respective data.

      Any help is greatly appreciated.           
        • 2. Re: Building a datatable from a DB Table
          Shravan Kumar Mali Newbie

          Thank you Ilya Shaikovsky for your reply.

           

          I am looking for something like below...

           

          I am trying to build a datatable in the backing bean, I need sorting, filtering and pagination features to be added to the datatable. Can anyone please help on this?

           

          Below is my code:

           

          private void populateDynamicDataTable() {
                  HtmlDataTable dynamicDataTable = new HtmlDataTable();
                  dynamicDataTable.setValueExpression("value", createValueExpression(
                          "#{tableDataBean.dynamicList}", List.class));
                  dynamicDataTable.setVar("dynamicItem");

           

                  String tableName = getSessionBean().getSlectedTable();
                  if (tableName != null) {
                      ArrayList<String> headersFromDB = new ArrayList<String>(
                              tableService.getColumnNames(tableName));
                      ArrayList<String> dynamicHeaders = new ArrayList<String>();
                      dynamicHeaders.add("Select");

           

                      for (String header : headersFromDB) {
                          dynamicHeaders.add(header);
                      }
                      dynamicList = tableService.getTableData(tableName);

           

                      // Iterate over columns.
                      for (int i = 0; i < dynamicList.get(0).size(); i++) {

           

                          // Create <h:column>.
                          HtmlColumn column = new HtmlColumn();
                          dynamicDataTable.getChildren().add(column);

           

                          // Create <h:outputText value="dynamicHeaders[i]"> for <f:facet
                          // name="header"> of column.
                          HtmlOutputText header = new HtmlOutputText();
                          header.setValue(dynamicHeaders.get(i));
                          column.setHeader(header);

           

                          if (i == 0) {
                              HtmlSelectBooleanCheckbox checkbox = new HtmlSelectBooleanCheckbox();
                              checkbox.setValueExpression("value", createValueExpression(
                                      "#{dynamicItem[" + 0 + "]}", Boolean.class));
                              column.getChildren().add(checkbox);
                          } else {

           

                              // Create <h:outputText value="#{dynamicItem[" + i + "]}">
                              // for the body of column.
                              HtmlOutputText output = new HtmlOutputText();
                              output.setValueExpression("value", createValueExpression(
                                      "#{dynamicItem[" + i + "]}", String.class));                   
                             column.getChildren().add(output);
                          }
                      }

           

                      // Add the datatable to <h:panelGroup
                      // binding="#{myBean.dynamicDataTableGroup}">.
                      dynamicDataTableGroup = new HtmlPanelGroup();
                      dynamicDataTableGroup.getChildren().add(dynamicDataTable);
                  }
              }