Refreshing TreeTable after saving item

278
January 07, 2018, at 3:53 PM

I'm working on a reddit-like system using Primefaces, where users should be able to post articles and comment on them.

A user can highlight an article in a p:dataTable and click a p:commandButton for more details:

<h:form id="storyListForm">
            <p:dataTable id="datalist">               
                <p:columns with data...>
                <.../p:columns>
                    <p:commandButton id="viewButton" icon="ui-icon-search" value="#{bundle.View}" update=":StoryViewForm" oncomplete="PF('PostViewDialog').show()" disabled="#{empty postController.selected}"/>
            </p:dataTable>
        </h:form>

This will display a p:dialog with details on that article, including a p:treeTable containing all comments made on that article which looks like:

<p:dialog id="PostViewDlg" width="750"  widgetVar="PostViewDialog" modal="true" resizable="false" appendTo="@(body)" header="#{bundle.ViewPostTitle}">
        <h:form id="StoryViewForm">
            <h:panelGroup id="display">
                <p:panelGrid columns="2" rendered="#{postController.selected != null}">
                    details abot the article.....
                </p:panelGrid>
                <p:treeTable
                    rendered="#{postController.selected!=null}"
                    id="tree"
                    value="#{postController.treeNode}" 
                    selectionMode="single"
                    selection="#{postController.treeNodeParent}"
                    var="comment" 
                    style="margin-top:0" 
                    scrollable="true"
                    scrollHeight="150">
                    <p:ajax event="select" update=":StoryViewForm:replyButton" />
                    <p:ajax event="unselect" update=":StoryViewForm:replyButton" />
                    <p:column headerText="Comment">
                        <h:outputText value="#{comment.postText}" />
                    </p:column>
                    <p:column headerText="Created">
                        <h:outputText value="#{comment.createdDate}" />
                    </p:column>
                </p:treeTable>
                <p:commandButton 
                    id="replyButton" 
                    icon="ui-icon-plus" 
                    value="Reply" 
                    disabled="#{empty postController.treeNodeParent}" 
                    actionListener="#{postController.prepareReplyCreate()}"
                    update=":ReplyCommentCreateForm" 
                    oncomplete="PF('ReplyCommentCreateDialog').show()"
                    />
            </h:panelGroup>
        </h:form>
    </p:dialog>

The user can highlight a comment in the p:treeTable and hit the reply-button, which will display another dialog for entering a reply:

<p:dialog id="ReplyCommentCreateDlg" 
              widgetVar="ReplyCommentCreateDialog"
              height="400"
              width="600"
              modal="true" 
              resizable="false" 
              appendTo="@(body)" 
              header="#{bundle.CreatePostTitle}">
        <h:form id="ReplyCommentCreateForm">
            <h:panelGroup id="display" >
                <p:panelGrid columns="2" rendered="#{postController.reply != null}" >
                    <p:outputLabel value="Comment" for="postText" />
                    <p:inputTextarea id="postText" value="#{postController.reply.postText}" title="#{bundle.CreatePostTitle_postText}" style="width: 100%;"/>
                </p:panelGrid>
                <p:commandButton actionListener="#{postController.replyComment()}" 
                                 value="#{bundle.Save}" 
                                 update="display,:StoryViewForm:display,:growl"
                                 oncomplete="handleSubmit(args,'ReplyCommentCreateDialog');">
                </p:commandButton>
            </h:panelGroup>
        </h:form>
    </p:dialog>

The replyComment()-method and the methods that render the p:treeTable looks like this:

    private Post reply (JPA-entity);
    private Post selected (JPA-entity);
    private Post parentComment (JPA-entity);
    private TreeNode treeNode parent;
    private TreeNode treeNode;
    getters/setters....
    public void replyComment() {
    ...
    parentComment = (Post) treeNodeParent.getData();
    reply.setParentId(parentComment); 
    persistReply();
}
public TreeNode getTreeNode() {
    if (selected != null) {
        treeNode = new DefaultTreeNode(selected, null);
        if (null != selected.getPostList()) {
            for (Post post : selected.getPostList()) {
                TreeNode comments = new DefaultTreeNode(post, treeNode);
                createSubNode(post, comments);
            }
        }
    }
    return treeNode;
}
private void createSubNode(Post parrent, TreeNode node) {
    List<Post> postList = parrent.getPostList();
    if (null != postList) {
        for (Post subPost : postList) {
            TreeNode subNode = new DefaultTreeNode(subPost, node);
            createSubNode(subPost, subNode);
        }
    }
}

This works as far as saving the reply-comments to the database with the right parent/child settings etc. But I can't get the p:treeTable to refresh, so that the newly added reply is displayed.

When the ReplyCommentCreateDlg closes, the (still open) PostViewDlg should update the tableTree with the new comment. Any ideas would be greatly appreciated, thanks in advance

READ ALSO
Adding JFoenix JavaFX Element to FXML won&#39;t launch after compile

Adding JFoenix JavaFX Element to FXML won't launch after compile

I am using Netbeans 82 and I am building a simple FXML project

403
using Mockito, doReturn() calling original method

using Mockito, doReturn() calling original method

I am trying to partial mock on a local variableThen trying doReturn() so that it does not calls the original method

310
Generic class type parameter value in Java

Generic class type parameter value in Java

I have declared a Class AnimalAnd I have an interface Interface1

280
How to &ldquo;preload&rdquo; all the assets needed from the disk

How to “preload” all the assets needed from the disk

i just started a 2d game project in java where i make my own little engine, never used java before but i have some basic knowledge with OOP and game engine logic

217