10 Replies Latest reply on Sep 28, 2011 6:26 AM by henk de boer

    Dynamic columns in RF4

    Dmitry Shultz Novice

      Hi All,

       

      There are some articles talking about using rich:columns for dynamic column creation, but looks like it is not relevant anymore.

       

      So what is the proper way to create column's dynamically in RF 4?

       

      Thanks,

      Dmitry.

        • 1. Dynamic columns in RF4
          Ilya Shaikovsky Master

          http://is.gd/vRtm4T the same should be used for to create columns

          • 2. Dynamic columns in RF4
            Dmitry Shultz Novice

            Thanks a lot Ilya, but unfortunately I have some more complicated case, I'm not sure if it is a bug or it should not work, here is code to reproduce:

             

            complete page:

             

            <?xml version="1.0" encoding="UTF-8"?>

            <!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:c="http://java.sun.com/jsp/jstl/core">

             

             

            <h:head>

            </h:head>

             

            <h:body>

             

                <a4j:repeat value="#{testBean.testObjects}" var="testObj">

             

                    <c:forEach items="#{testObj.characters}" var="testChar">

                        <h:outputText value="#{testChar}"/>

                    </c:forEach>

             

                </a4j:repeat>

             

                <c:forEach items="#{testBean.getCharacters('test 5')}" var="testChar">

                    <h:outputText value="#{testChar}"/>

                </c:forEach>

             

            </h:body>

            </html>

             

            The TestBean.java :

             

            @ViewScoped

            @ManagedBean

            public class TestBean {

             

                private List<TestObject> testObjects;

             

                @PostConstruct

                public void init(){

                    testObjects = new ArrayList<TestObject>();

                    testObjects.add(new TestObject("test 1"));

                    testObjects.add(new TestObject("test 2"));

                    testObjects.add(new TestObject("test 3"));

                }

             

                public List<TestObject> getTestObjects() {

                    return testObjects;

                }

             

                public void setTestObjects(List<TestObject> testObjects) {

                    this.testObjects = testObjects;

                }

             

                public char[] getCharacters(String str){

                    return str.toCharArray();

                }

            }

             

            class TestObject {

             

                private String string1;

             

                public TestObject(String s) {

                    string1 = s;

                }

             

             

                public String getString1() {

                    return string1;

                }

             

                public void setString1(String string1) {

                    this.string1 = string1;

                }

             

                public char[] getCharacters(){

                    return string1.toCharArray();

                }

            }

             

             

            items="#{testObj.characters}" is not evaluated probably because testObj is defined in a4j:repeat, the second <c:if works fine (because it is using the top level object?).

             

            My problem is the same - I need to generate table columns dynamically inside of a4j:repeat. Is there any way to acomplish it?

             

            Thanks,

            Dmitry.

            • 3. Dynamic columns in RF4
              Nick Belaevski Master

              Dmitry,

               

              Please take a look at this article: http://www.ilikespam.com/jsf/c:foreach-vs-ui:repeat-in-facelets

              The proper way to build table is usage of nested a4j:repeat components.

              • 4. Dynamic columns in RF4
                Ilya Shaikovsky Master

                if you want to add columns components to the data table c:foreach should be used.

                 

                But in your case (just to iterate over nested lists and output the data) repeat is suitable solution as Nick said.

                 

                One important things which you should remember - those iteration tags should not be combined (mean using iteration var from one of them in the other). Because them are iterates over the collection at different phases so variables just not accerssible for nested one.

                • 5. Dynamic columns in RF4
                  Dmitry Shultz Novice

                  Thanks for your replies Guys, but I still have no idea how to solve the problem with having dynamic table columns generated for tables that also generated dynamically (using a4j:repeat). The sample code was provided to showcase these tags interoperability issue, not the real one.

                   

                  I understand that a4j:repeat can't have nested c:forEach, so I can't use c:forEach for column generation.... and a4j:repeat will not help me (because for dynamic columns only c:forEach shoukd be used).

                   

                  Is there any way to implement it?

                   

                  Thanks,

                  Dmitry.

                  • 6. Dynamic columns in RF4
                    raid3n Newbie

                    Hi Dmitry, I show you my solution in a pseudo-code:

                     

                     

                    public class DynamicBean {

                     

                              private List<String> columns;

                              private List<List<String>> rows;

                     

                    public List<String> getColumns () { return columns; }

                    public List<List<String>> getRows() { return rows; }

                    }

                     

                    <rich:dataTable                              value="#{dynamicBean.rows}"                              var="r">

                                                  <c:forEach items="#{dynamicBean.columns}" var="c" varStatus="i">

                                                                 <rich:column>

                                                                                <f:facet name="header">

                                                                                               <h:outputText value="#{c}" />

                                                                                </f:facet>

                                                                           <h:outputText value="#{r[i.index]}" />

                                                                 </rich:column>

                                                  </c:forEach>

                                        </rich:dataTable>

                     

                    I'm a beginner but it works.

                    • 7. Dynamic columns in RF4
                      Dmitry Shultz Novice

                      Hi raid3n,

                       

                      I uppreciate your help, but I can't see how it will solve my problem because you are not using a4j:repeat to generate tables dynamically, sorry i didn't specified it as requirement in the initial post (read comments).

                       

                      Thanks,

                      Dmitry.

                      • 8. Dynamic columns in RF4
                        Nick Belaevski Master

                        Why can't you use nested c:forEach then?

                        • 9. Re: Dynamic columns in RF4
                          Dmitry Shultz Novice

                          Tried it - doesn't work for me.

                          I have layout that is rendered using ajax request and can be simplified to something like this:

                           

                          <h:form>

                               <a4j:outputPanel>

                           

                                    <c:forEach value=#{bean.collectionOfPanels} var="panel1">

                           

                                          <rich:collapsiblePanel >

                           

                                                  <rich:dataTable value="#{panel1.rows}" var="rowItem">

                           

                                                        <c:forEach items="#{panel1.columns}" var="columnItem">

                           

                                                             <rich:column>

                                                                  <f:facet name="header">

                                                                         <h:outputText value="#{columnItem.name}"/>

                                                                   </f:facet>

                                                                    <h:selectOneMenu value ="#{rowItem.value}">

                                                                         <a4j:ajax .../>            

                                                                     </h:selectOneMenu>

                                                                </rich:column>

                           

                                                         </c:forEach>

                           

                                                  </rich:dataTable>

                           

                                          </rich:collapsiblePanel>

                           

                                    </c:for:Each>

                           

                               </a4j:outputPanel>

                          </h:form>

                           

                          I can see the table kind of rendered, but rich:collapsiblePanel is not properly rendered and <a4j:ajax> attached to the table cells are not working.

                          Menwhile I'm using nested a4j:repeat and 2 h:panelGrid's (one for header another for rows) as a workaround, but it will be nice be able to render a real table with dynamic columns.

                           

                          Thanks,

                          Dmitry.

                          • 10. Re: Dynamic columns in RF4
                            henk de boer Master

                            Just wondering, did you ever find a solution to your problem? I've got the same issue, as shown here: http://community.jboss.org/message/628994#628994