1
votes

I am using PrimeFaces JSF library, and I have a tree where each node is a checkbox:

<p:tree selectionMode="checkbox"...>

This tree represents some files, and it is dynamicaly refreshed (polled) every 5 seconds since the files/nodes can be added or removed:

<p:poll interval="5" update="treeId"... />

User can select files via tree node checkboxes. Selected nodes are saved in an array of TreeNodes:

<p:tree selection="#{BackingBean.selectedNodes}"...>

Where selectedNodes is defined as:

private TreeNode[] selectedNodes;

The problem is: since the tree is constantly being refreshed (every 5 sec), I loose the tree state! What user has checked becomes unchecked again.

How would I keep the tree state remembered between 5-sec refreshes?

1

1 Answers

1
votes

If you are using JSF 2:

Make your backing bean @ViewScoped:

@ManagedBean
@ViewScoped
public class BackingBean {
    ...
}

If you are using JSF 1.2:

Use component a4j:keepAlive from Richfaces:

<a4j:keepAlive bean="BackingBean" />

You also have an option to use Tomahawk's saveState (with the Tomahawk's version appropriate for your project), to put only the selectedNodes in the view:

<t:saveState value="#{BackingBean.selectedNodes}" />

Just put Tomahawk and its dependencies in the WEB-INF/lib, register the filter in your web.xml and the namespace on your .xhtml page and you're good to go.