0 Replies Latest reply on May 6, 2011 2:25 PM by Carlos Rosas

    Create richfaces DataTable dinamically

    Carlos Rosas Newbie

      I have a problem to fill a dataTable dynamically, hope you can help me.

       

      First I had this code to create the table since jsp, where I indicate the list which I want to Iterate.

       

      <rich:dataTable value="#{DivisionBean.divisionAgentsList}" var="agent" rowKeyVar="row">

           <f:facet name="header" >

                <rich:columnGroup>

                     <rich:column colspan="4" styleClass="peqInfo triple gralBlack">

                          <h:outputText value="#{msgs.divInfoPersonal}" styleClass="peqInfo"/>

                     </rich:column>

                     <rich:column breakBefore="true" styleClass="peqInfo triple infoAgents">

                          <h:outputText value="#{msgs.divNo}" styleClass="peqInfo"/>

                     </rich:column>

                     <rich:column styleClass="peqInfo infoAgents">

                          <h:outputText value="#{msgs.divNombreAgente}" styleClass="peqInfo"/>

                     </rich:column>

                     <rich:column styleClass="peqInfo infoAgents">

                          <h:outputText value="#{msgs.divIdAgente}" styleClass="peqInfo" escape="false"/>

                     </rich:column>

                     <rich:column styleClass="peqInfo infoAgents">

                          <h:outputText value="#{msgs.divAntiqui}" styleClass="peqInfo"/>

                     </rich:column>

                </rich:columnGroup>

           </f:facet>

       

       

                <rich:column styleClass="peqInfo">

                     <h:outputText value="#{row+1}" styleClass="peqInfo"/>

                </rich:column>

                <rich:column styleClass="peqInfo">

                     <h:outputText value="#{agent.nombreAgente}" styleClass="peqInfo"/>

                </rich:column>

                <rich:column styleClass="peqInfo">

                     <h:outputText value="#{agent.claveAgente}" styleClass="peqInfo"/>

                </rich:column>

                <rich:column styleClass="peqInfo">

                     <h:outputText value="#{agent.antiguedad}" styleClass="peqInfo"/>

                </rich:column>

      </rich:dataTable>

       

      And this is the result, get all the items for the table.

      agents1.jpg

      ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

      For a requeriment I took the descicion to build it dynamically, this is the code

       

      private UIComponent buildTableAgents(String ids[]){

       

           String id=ids[0]+"_"+ids[1];

       

           //Create de dataTable

           HtmlDataTable agents=new HtmlDataTable();

           agents.setId(AGENTS+id);

       

           //Create the ColumGroup which contain the columns of the header

           HtmlColumnGroup headerAgents=new HtmlColumnGroup();

       

           //Create a Column and OutputText for each header

           HtmlColumn columsHeader[]=new HtmlColumn[5];

           HtmlOutputText agentsInfo[]=new HtmlOutputText[5];

       

           for(int i=0;i<columsHeader.length;i++){

                columsHeader[i]=new HtmlColumn();

                agentsInfo[i]=new HtmlOutputText();

       

                String msg=null;

       

                columsHeader[i].setStyleClass(PEQ_INFO+" "+INFO_AGENTS);

       

                agentsInfo[i].setStyleClass(PEQ_INFO);

                agentsInfo[i].setEscape(false);

       

                switch ( i ) {

                case 0:

                     columsHeader[i].setColspan(4);

                     columsHeader[i].setStyleClass(PEQ_INFO+" "+TRIPLE+" "+GRAL_BLACK);

                     msg=INFOPERSONAL;

                     break;

                case 1:

                     columsHeader[i].setBreakBefore(true);

                     columsHeader[i].setStyleClass(PEQ_INFO+" "+TRIPLE+" "+INFO_AGENTS);

                     msg=NO;

                     break;

                case 2:

                     msg=NOMBREAGENTE;

                     break;

                case 3:

                     msg=IDAGENTE;

                     break;

                case 4:

                     msg=ANTIQUI;

                     break;

                }

                agentsInfo[i].setValue(getMessage(msg));

       

                //Add each header to ColumnGruop

                columsHeader[i].getChildren().add(agentsInfo[i]);

                headerAgents.getChildren().add(columsHeader[i]);

           }

       

           //Add ColumnGroup as header

           agents.setHeader(headerAgents);

       

           /*

                I create two arrays, Column and Output to build just one row for visualize it

           */

           HtmlColumn agent[]=new HtmlColumn[4];

           HtmlOutputText agentTxt[]=new HtmlOutputText[4];

       

           for(int i=0;i<agent.length;i++){

                agent[i]=new HtmlColumn();

                agentTxt[i]=new HtmlOutputText();

       

                agentTxt[i].setStyleClass(PEQ_INFO);

                agentTxt[i].setValue("ALGO");

       

                agent[i].setStyleClass(PEQ_INFO);

       

                //Add the Column to DataTable agents

                agent[i].getChildren().add(agentTxt[i]); 

                agents.getChildren().add(agent[i]);

           }

       

           return agents;

      }

       

       

      And this is the result, when I just want to get the first row, I dont get it

      agents2.jpg