zzirna
zzirna

Reputation: 69

Primefaces TreeNode deleted at second action

In my jsf page I have a <p:treeTable> structure. I am trying to delete a node when clicking on a small icon placed on one of the columns:

<p:column rendered="#{node.isLeaf}"  style="width:70px">  
     <p:commandLink  styleClass="entity-icon"  update=":clipboard-tree" ajax="true"
                          action="#{clipboardManager.removeClipboard(node)}" >
       <p:graphicImage value="/resources/images/delete.png" styleClass="entity-icon-tree"/>
    </p:commandLink>
 </p:column>

And my removeClipboard function looks something like this:

public void removeClipboard(FindResult result){
  TreeNode node = result.getNode(); 

  node.getChildren().clear();  
  node.getParent().getChildren().remove(node);  

  node.setParent(null);  
  node = null;  

}

This is similar to the showcase offered by primefaces. However the tree get's updated only on the second click and I get the following exception:

[Index: 0, Size: 0] with root cause java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

This happens only when I try to delete the last child of a node. Does anybody have an idea why this is happening? Or how I can fix it? Thank you in advance.

Upvotes: 0

Views: 2137

Answers (2)

Benjamin Judas
Benjamin Judas

Reputation: 41

I am suffering the same problem. Primefaces seems to have issues when revalidating the component model after an ajax-call was made. The strange thing I noticed was, that this only happened during deletion on an element.

The workaround I found was to disable ajax for the delete action. In the example given above, that would be as follows:

<p:column rendered="#{node.isLeaf}"  style="width:70px">  
  <p:commandLink
    styleClass="entity-icon"
    update=":clipboard-tree" 
    ajax="false" <!-- this one to 'false' -->
    action="#{clipboardManager.removeClipboard(node)}">
    <p:graphicImage value="/resources/images/delete.png" styleClass="entity-icon-tree"/>
  </p:commandLink>
</p:column>

Unfortunately, the following complete page reload is not the best thing for usability.

Upvotes: 0

zzirna
zzirna

Reputation: 69

In case anybody else has this problem I found a workaround. It is not ideal but have no other better idea.

Given that the error message showed only for the last child of a node, I change the tree generation such that the last element added is always an empty node.

This way I get a nice spacing between my parent nodes and I can delete the last node without errors. Hope this helps.

Upvotes: 0

Related Questions