4 Replies Latest reply on Mar 22, 2011 4:06 AM by ilya_shaikovsky

    How to create dynamic rich:datatable with richfaces 4.x?

    kittop

      I try to do like here:

       

      rich.xhtml:

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:a4j="http://richfaces.org/a4j"
      xmlns:rich="http://richfaces.org/rich"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">

      <h:head></h:head>
      <body>
      <rich:panel>
      <f:facet name="header">
              Write your own custom rich components with built-in AJAX support
              </f:facet>
      <h:form>
        <rich:dataTable binding="#{richBean.htmlDataTable}">

        </rich:dataTable>
      </h:form>
      </rich:panel>
      </body>
      </html>

       

      Backing Bean: RichBean:

      package demo;

      import java.util.ArrayList;
      import java.util.List;

      import javax.el.ExpressionFactory;
      import javax.el.ValueExpression;
      import javax.faces.application.Application;
      import javax.faces.bean.ManagedBean;
      import javax.faces.bean.SessionScoped;
      import javax.faces.component.html.HtmlColumn;
      import javax.faces.component.html.HtmlOutputText;
      import javax.faces.context.FacesContext;
      import javax.faces.model.DataModel;
      import javax.faces.model.ListDataModel;

      import org.richfaces.component.UIDataTable;

      @ManagedBean
      @SessionScoped
      public class RichBean {
      private DataModel dataModel;

      private UIDataTable htmlDataTable;

       

      public void setDataModel(DataModel dataModel) {
        this.dataModel = dataModel;
      }

       

      @SuppressWarnings({ "rawtypes" })
      public DataModel getDataModel() {
        dataModel = new ListDataModel();
        List list = new ArrayList();
        list.add(new Affix("1", "kyo"));
        list.add(new Affix("2", "kitto"));
        list.add(new Affix("3", "ptianfeng"));
        list.add(new Affix("4", "ptfeng"));
        dataModel.setWrappedData(list);
        return dataModel;
      }

       

      public UIDataTable getHtmlDataTable() {
        FacesContext fc = FacesContext.getCurrentInstance();
        Application app = fc.getApplication();
        ExpressionFactory exp = app.getExpressionFactory();
        htmlDataTable = new UIDataTable();
        htmlDataTable.setVar("var");

       

        HtmlOutputText headerText = new HtmlOutputText();

        headerText.setValue("Sqlid");
        HtmlColumn column = new HtmlColumn();
        HtmlOutputText out = new HtmlOutputText();
        ValueExpression value = exp.createValueExpression(fc.getELContext(),
         "#{var.sqlid}", String.class);
        out.setValueExpression("value", value);
        column.getChildren().add(out);
        column.setHeader(headerText);
        htmlDataTable.getChildren().add(column);
        htmlDataTable.setValue(getDataModel());
        return htmlDataTable;
      }

       

      public void setHtmlDataTable(UIDataTable htmlDataTable) {
        this.htmlDataTable = htmlDataTable;
      }

      }

       

      and the Affix:

      package demo;

      import java.io.Serializable;


      public class Affix implements Serializable{
      private String sqlid;/*  */
      private String name; /*  */


      public Affix(String sqlid, String name){
        this.sqlid = sqlid;
        this.name = name;
      }


      public String getSqlid() {
        return sqlid;
      }
      public void setSqlid(String sqlid) {
        this.sqlid = sqlid;
      }
      public String getName() {
        return name;
      }
      public void setName(String name) {
        this.name = name;
      }
      }

       

      and it just show:

      Sqlid
      1
      2
      3
      4

      Is any error in the code ?

      how to set the skin of richfaces?

      Thank!

        • 1. Re: How to create dynamic rich:datatable with richfaces 4.x?
          ilya_shaikovsky

          at first you used not rich column but jsf h:column

          import javax.faces.component.html.HtmlColumn;

           

          And one more minor suggestion use Application.createComponent(componentType) instead of new UIClass(). There could be problems with classloading in some cases so usage of JSF api is more proper way.

          • 2. Re: How to create dynamic rich:datatable with richfaces 4.x?
            kittop

            now, I use the UIColumn:

            public UIDataTable getHtmlDataTable() {
              FacesContext fc = FacesContext.getCurrentInstance();
              Application app = fc.getApplication();
              ExpressionFactory exp = app.getExpressionFactory();
            htmlDataTable = (UIDataTable) app.createComponent(UIDataTable.COMPONENT_TYPE);
              htmlDataTable.setId("tableid");
              htmlDataTable.setVar("var");

              HtmlOutputText headerText = new HtmlOutputText();

              headerText.setValue("Sqlid");
            UIColumn column = (UIColumn) app.createComponent(UIColumn.COMPONENT_TYPE);
              column.setId("columnid");
              HtmlOutputText out = new HtmlOutputText();
              ValueExpression value = exp.createValueExpression(fc.getELContext(),
                "#{var.sqlid}", String.class);
              out.setValueExpression("value", value);
              column.getChildren().add(out);
              column.setHeader(headerText);
              htmlDataTable.getChildren().add(column);
              htmlDataTable.setValue(getDataModel());
              return htmlDataTable;
            }

             

             

            and it also show like above,nothing change.

            now that the problem isn't out of the way.

            1 of 1 people found this helpful
            • 3. Re: How to create dynamic rich:datatable with richfaces 4.x?
              sloidl

              I have the same problem. Richfaces 4 M5.

               

              I think that the style Information for the UIColumn is not added to the page.

              Here is why:

               

              {code}

                      <!--  Dummy to make styling work -->

                      <rich:extendedDataTable rendered = "false">

                          <rich:column>

                              <f:facet name="header">

                              </f:facet>

                          </rich:column>

                      </rich:extendedDataTable>

               

                     <!-- Table to be shown -->

                     <rich:extendedDataTable id="list" binding="#{testbean.table}" value="#{testbean.values}"/>

              {code}

              So if I add unrendered dummy table, everything works fine.