collections - Component to iterate and render a nested tree-like object structure in JSF -


given class definition below:

public class comment {      string username;     string comment;     list<comment> replies;      // ... } 

is possible use construct jsf page renders data contained in comment instance in tree structure follows?

comments userone said blah blah ----     userthree replied     blah blah blah     ----     userthree replied     blah blah blah ---- usertwo said blah blah ---- userone said blah blah 

if nesting 1 level deep, or has fixed amount of maximum depth, nest jsf repeater components <ui:repeat> or <h:datatable> in each other usual way.

<ul>     <ui:repeat value="#{bean.comments}" var="comment">         <li>#{comment.username} #{comment.comment}             <ul>                 <ui:repeat value="#{comment.replies}" var="reply">                     <li>#{reply.username} #{reply.comment}</li>                 </ui:repeat>             </ul>         </li>     </ui:repeat> </ul> 

but if nesting level "unlimited", need jsf component instead can render tree hierarchy. not available in standard jsf component set. you'd need @ 3rd party component libraries primefaces <p:tree>, or richfaces <rich:tree>, or omnifaces <o:tree>. omnifaces 1 allows have full control on tree markup, while others you'd possibly need fiddle css want.

<o:tree value="#{bean.comments}" var="comment">     <o:treenode>         <ul>             <o:treenodeitem>                 <li>#{comment.username} #{comment.comment}                     <o:treeinsertchildren />                 </li>             </o:treenodeitem>         </ul>     </o:treenode> </o:tree> 

i'd clarity rename string comment property message or text orso.

if you're on jsf 2.x, consider <my:comments comment="#{bean.comments}"> composite component below.

<cc:interface componenttype="commentscomposite">     <cc:attribute name="comment" type="com.example.comment" required="true" /> </cc:interface> <cc:implementation>     <c:if test="#{not empty cc.comment.replies}">         <ul>             <c:foreach items="#{cc.comment.replies}" var="comment" varstatus="loop">                 <li>                     #{comment.username} #{comment.comment}                     <my:comments comment="#{cc.parent.comment.replies[loop.index]}" />                 </li>             </c:foreach>         </ul>     </c:if> </cc:implementation> 

@facescomponent("commentscomposite") public class commentscomposite extends uinamingcontainer {      private comment comment;      @override     public void setvalueexpression(string name, valueexpression expression) {         if ("comment".equals(name)) {             setcomment((comment) expression.getvalue(getfacescontext().getelcontext()));         }         else {             super.setvalueexpression(name, expression);         }     }      public comment getcomment() {         return comment;     }      public void setcomment(comment comment) {         this.comment = comment;     }  } 

see blog on subject, recursive tree of composite components.


Comments

Popular posts from this blog

How do you change vbulletin's home page to the forums instead of the default (CMS or Activity Stream)? -

c++ - Troubles when compiling phash program -