14 Replies Latest reply on Jul 13, 2011 9:15 PM by shadowcreeper

    Dynamic datatable in RichFaces

    seb seb Newbie

      Hello,

      I'm brand new here and doing my best to find an answer to my question regarding the dynamic datatable usage in Rich Faces.

      I'm used to developping with Tomahawk and IceFaces and both use DataModel structures (headers and datatable).

      In RichFaces the only way I've found to dynamically display a datatable was using a generic list and iterate through the list with an index (on the columns).

      Here is an example:

      - I assigned a List < List > to the rich:dataTable value component and I declared a variable.
      - I used a List < Object > to the rich:columns value component and I declared an index.
      - Then I display the value by using: variable[index].

      It works fine but it's completely different from the way I was used to working with Tomahawk and IceFaces.

      Let's go back to the previous example, with IceFaces (or Tomahawk) :

      - I assigned a DataModel to the DataTable
      - I assigned another DataModel to the headers
      - I display the value by calling a method in the bean that provides me with the corresponding piece of data (by using getRowData() and getRowIndex() from the DataModel)

      It seems that I can only use DataModel with other components such as ScrollableDataModel.

      Am I missing something here?!

      Thank you for your help and support.

      PS : Sorry for my English I'm French ^^

        • 1. Re: Dynamic datatable in RichFaces
          seb seb Newbie

          Any ideas why I'm not able to use generic dataModels with RichFaces please?

          Thanks

          • 2. Re: Dynamic datatable in RichFaces
            shadowcreeper Apprentice

            Because 2-dimensional arrays are easier???
            Because maps are easier???

            Why would you need anything more complicated for a 2-dimensional table?

            • 3. Re: Dynamic datatable in RichFaces
              seb seb Newbie

              Hi,

              Because using DataModels is easy and seems much cleaner, isn't it?

              I guess it's a different approach of the Model-View-Control pattern in RichFaces.

              By using the DataModel and its associated mothods (e.g. getRowIndex(), etc...), the datatable can be dynamically built by calling a method in the bean (business logic).

              RichFaces expects a List<List> and directly iterates through this list with an index in the JSP page (i.e. in the View part of MVC).

              I just wondered why it was implemented in this way...

              Thank you.

              • 4. Re: Dynamic datatable in RichFaces
                seb seb Newbie

                oups it's : List < List < Object > > :)
                (Obviously the forum's editor only displays it correctly if you put spaces before and after '<' and '>')
                ^^

                • 5. Re: Dynamic datatable in RichFaces
                  shadowcreeper Apprentice

                  I switched mine over from DataModels to an array of lists when switching from tomahawk to richfaces and I personally feel that my code is now much simpler/cleaner.

                  In fact, the whole time I was mucking with the data models in tomahawk I was thinking that it would be much cleaner to just use an array or list or map (all of which work fine when using indexes if setup correctly).

                  This is only personal opinion of course, and your mileage may vary... ;)

                  • 6. Re: Dynamic datatable in RichFaces
                    Alessandro Dionisi Newbie

                    Please sebs, can you show the example code you are speaking about (JAVA+JSP)? Please, I'm new to RichFaces and I can't handle dynamic columns.

                    • 7. Re: Dynamic datatable in RichFaces
                      seb seb Newbie

                      Hi,

                      Sure here is the JSP code:

                      <rich:dataTable id="myDataTable"
                       value="#{dataTableBean.dataList}" var="tableItem">
                       <rich:columns
                       value="#{dataTableBean.columnHeaders}"
                       var="columnHeader" index="index">
                       <f:facet name="header">
                       <h:outputText value="#{columnHeader.label}" />
                       </f:facet>
                       <h:outputText value="#{tableItem[index]}" />
                       </rich:columns>
                       </rich:dataTable>


                      I cannot easily paste the Java code since I've built a framework for the end users.
                      But you need to declare the following in your bean:

                      - a "dataList" which is a List < List < Object > >
                      - a "columnHeaders" which is a List < Object >
                      (+ getters/setters of course)

                      In my example above, columnHeader is a List < MyHeader > and MyHeader contains a String (i.e. "label" variable).
                      You can make it simple and use a List < String > then you can write:

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


                      Cheers

                      • 8. Re: Dynamic datatable in RichFaces
                        Alessandro Dionisi Newbie

                        Sorry sebs.
                        I don't understand why it's necessary a List<List>...I have a list of objects with variable number of columns...isn't it the same problem as me?
                        Please explain me why you need a List<List>.
                        Thanks

                        • 9. Re: Dynamic datatable in RichFaces
                          Alessandro Dionisi Newbie

                          Ah ok I understood now...I must transform my objects list in a matrix.
                          My problem is that I want to generate dynamic tables, depending on Object used. For example, in my application, I can have a List or a List; what I desire is to have (at runtime) a table that can display both type of objects, without create a specific JSP for Car or Motorbike.

                          Is there another way to create a dynamic table, just using the List?

                          • 10. Re: Dynamic datatable in RichFaces
                            seb seb Newbie

                            I'm not sure to really understand what's your problem.

                            In your datatable bean you just need to declare your generic dataList as a List < Object >.
                            Then you create (probably in some other beans) your List < Car > or List < Motorbike > or whatever... and you assign it to the generic dataList.

                            Have you tried it yet? :-)

                            • 11. Re: Dynamic datatable in RichFaces
                              seb seb Newbie

                               

                              "shadowcreeper" wrote:
                              I switched mine over from DataModels to an array of lists when switching from tomahawk to richfaces and I personally feel that my code is now much simpler/cleaner.

                              In fact, the whole time I was mucking with the data models in tomahawk I was thinking that it would be much cleaner to just use an array or list or map (all of which work fine when using indexes if setup correctly).

                              This is only personal opinion of course, and your mileage may vary... ;)


                              Much easier does not mean better nor cleaner ;-)

                              Let's take a very simple example: if you want to retrieve the values for the current selected row, you'll most probably use the method getRowData() from your DataModel or you HTMLDataTable in your managed bean.

                              In RichFaces you'll get a List < String > for this selected row instead of an Object. You'll have to iterate this list until you find the piece of information you're looking for instead of getting an instant access via the object by calling the corresponding getter.

                              At this point I guess that everybody's aware that using Object instead of a List < String > is simply how you build Object oriented application ;-)


                              • 12. Re: Dynamic datatable in RichFaces
                                Jayesh Mahato Newbie

                                Hi,

                                 

                                I am stucked with datatable. I am using this on jsp page:

                                <h:dataTable value="#{AList.getMyArr}" var="capt"  border="1" 
                                                headerClass="header"   rowClasses="row1,row2">
                                                    <h:column >
                                                        <f:facet name="header" > <f:verbatim>Name List</f:verbatim> </f:facet>
                                                        <h:outputText value="#{capt}" />
                                                    </h:column>
                                </h:dataTable>
                                

                                I am using an array list in the bean Alist:

                                 

                                package demo;
                                
                                import java.util.*;
                                import java.io.*; 
                                
                                public class Alist {
                                          ArrayList<String> myArr = new ArrayList<String>(10);
                                          public Alist() 
                                          { 
                                    myArr.add("Italian Riviera");
                                    myArr.add("Jersey Shore");
                                    myArr.add("Puerto Rico");
                                    myArr.add("Los Cabos Corridor");
                                          }
                                          public ArrayList<String> getMyArr() {
                                                    return myArr;
                                          }
                                          public void setMyArr(ArrayList<String> myArr) {
                                                    this.myArr = myArr;
                                          }
                                }
                                

                                 

                                The display page only displays the header and no data from the arraylist. Kindly help me...

                                • 13. Re: Dynamic datatable in RichFaces
                                  shadowcreeper Apprentice

                                  True, easier does not always mean better or cleaner, but in this instance I think it can be both. It can also be ugly and messier, but that all depends on how you choose to code it.

                                   

                                  As long as you know the index of the row you want, you can just say array[ index ] or list.get( index ), voila! Your AJAX call can also pass in any rowIndex value or ID value that your object uses or key value for that row if you are using a Map.

                                   

                                  I guess I just don't get what you are trying to say. Maybe I am just missing something.

                                  • 14. Re: Dynamic datatable in RichFaces
                                    shadowcreeper Apprentice

                                    You should be able to safely drop the setter. RichFaces knows how to add/remove stuff to/from collections (list/map/set).

                                     

                                    That looks right to me. Are you sure you have your Alist bean setup in your faces-config.xml file?