0

I am trying to create a datatable using JSF primefaces. I know there is a tag called datatable but am really unclear on how to use it. As in I can't picture its relationship with the bean. My table should be ID, Status, Details. Any idea how to do this or where to go for it? oh forgot to add that the number of rows will depend on returned from resultset from database. Thanks,

sys_debug
  • 3,883
  • 17
  • 67
  • 98

2 Answers2

3

You need to convert your result set into the List of entity (for example List<Book> ) and then set it as Bean 's property,

and use following code in XHTML

 <p:dataTable id="books" value="#{yourBean.books}" var="book">  

    <p:column>  
        <f:facet name="header">  
            <h:outputText value="Title" />  
        </f:facet>  
        <h:outputText value="#{book.title}" />  
    </p:column>  

    <p:column>  
        <f:facet name="header">  
            <h:outputText value="Author" />  
        </f:facet>  
        <h:outputText value="#{book.author}" />  
    </p:column>  

</p:dataTable>  

Book POJO

public class Book{
  private String author;
  private String title; 
  //accessors + constructors 
}

Managed Bean

@ManagedBean
public class YourBean{
  private List<Book> books;
  //accesors + constructors 
} 

See Also

jmj
  • 237,923
  • 42
  • 401
  • 438
  • yourBean.books is what? a variable holding what? any glimpse into java code into the bean? sorry this is the first time to test datatable :( I also need to make some of the fields clickable like ID details for example :/ – sys_debug Dec 17 '11 at 10:40
  • actually the code there is useful. what about clicking the field to go to another page to view request? – sys_debug Dec 17 '11 at 10:42
  • ok i changed many things to try and make this work smoothly. Just stuck when I do the table where it says book.title and book.author. I have now created a class ie RequestClass where i store the details such as ID, details and status of request. then i have a method which returns a list with all request objects, one for each request. How can I access it the way it is mentioned here?? :( – sys_debug Dec 18 '11 at 09:57
  • @sys_debug can you elaborate the structure in the question please – jmj Dec 19 '11 at 06:22
  • Jigar could u help me with URL parameter issue? – sys_debug Dec 19 '11 at 06:56
  • I am trying to pass parameter and ive been using managedProperty. The only thing I need to post again from that page, but the id variable holding the value is null. I heard that if i intend to post again, then I should use f:param. been trying but no luck. the code below is working but only with managedproperty. how can I add f:param correctly? – sys_debug Dec 19 '11 at 06:59
  • can you please open new post for new question – jmj Dec 19 '11 at 07:02
0

the answer I needed after Jigar helped me greatly is:

               <p:dataTable style="width:50px;" id="requestList" value="#
                {requestBean.requestsList}" var="requestClass">  
                <p:column>  
                    <f:facet name="header">  
                        <h:outputText value="ID" />  
                    </f:facet> 
                     <a href="review.xhtml?id=#{requestClass.requestID}">
                        <h:outputText value="#{requestClass.requestID}" />  
                     </a>

                </p:column>  

                <p:column>  
                    <f:facet name="header">  
                        <h:outputText value="Status" />  
                    </f:facet>  
                    <h:outputText value="#{requestClass.requestStatus}" />  
                </p:column>  

                  <p:column>  
                    <f:facet name="header">  
                        <h:outputText value="Details" />  
                    </f:facet>  
                      <h:outputText value="#{requestClass.requestTitle}" />  
                </p:column>
            </p:dataTable>  
sys_debug
  • 3,883
  • 17
  • 67
  • 98