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));


                  String tableName = getSessionBean().getSlectedTable();
                  if (tableName != null) {
                      ArrayList<String> headersFromDB = new ArrayList<String>(
                      ArrayList<String> dynamicHeaders = new ArrayList<String>();


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


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


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


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


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


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


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