1

I want to set multiple child nodes under same parent to be selected or check marked. I saw this answer: https://stackoverflow.com/a/17025086/1297935 and tried the following and it does not work:

@ManagedBean
@ViewScoped
public class BackingBean extends GenericViewBean {

        private TreeNode[] selectedNodes;

        public void showChildSelected(){
             ...
             ...
             selectedNodes = new TreeNode[]{p1child1,p1child2}; //two child nodes of same parent added
             ...
             ...
             for(TreeNode treeNode : selectedNodes){
                 treeNode.setSelected(true);
             }
             RequestContext.getCurrentInstance().update("listView:tree");
        }

}

Above code only shows the last child selected / check marked. Is there any Solution to select multiple child nodes of the same parent from Managed Bean? Thanks in advance.

Community
  • 1
  • 1
dev009
  • 755
  • 8
  • 13
  • Maybe [this](http://stackoverflow.com/a/33789199/3803447) will help. In your case do some `if` conditions if you want only particular node and children. – Geinmachi Mar 28 '16 at 11:29
  • @Geinmachi - i think the code that you pointed out does the samething, the line treeNode.setSelected(true); actually selects the node. the problem is it does not show both child nodes selected only the last one in the TreeNode[] array. – dev009 Mar 28 '16 at 12:00

1 Answers1

0

I think it seems to be some bug in Primefaces as the TreeNode[] selectedNodes in the Managed Bean properly shows 2 selected nodes but in the Javascript object PrimeFaces.widgets.treeWidget.selections array shows only one rowKey. This happens only when 2 children of the same parent are selected. And even after manually selecting the treenode using setSelected(true) in Managed Bean there is no change in Behaviour.

Currently i am using a Javascript workaround posted by alexSunder: https://stackoverflow.com/a/25716716, in his post he is using PrimeFaces.widgets.treeWidget.selectNode(node,silent) but i had a look at the tree.js and got another method for checkboxes: PrimeFaces.widgets.treeWidget.toggleCheckboxNode(node). Following is the change i did and it is working!!

@ManagedBean
@ViewScoped
public class BackingBean extends GenericViewBean {

        private TreeNode[] selectedNodes;

        public void showChildSelected(){
             ...
             ...
             selectedNodes = new TreeNode[]{p1child1,p1child2}; //two child nodes of same parent added
             ...
             ...
             for(TreeNode treeNode : selectedNodes){
                 StringBuilder sb = new StringBuilder();
                 sb.append("PrimeFaces.widgets.treeWidget.toggleCheckboxNode(");
                 sb.append("$(\"#listView\\\\:tree\\\\:");
                 sb.append(treeNode.getRowKey());
                 sb.append("\"))");
                 RequestContext.getCurrentInstance().execute(sb.toString());
             }
             RequestContext.getCurrentInstance().update("listView:tree");
        }

}

Method Description:
PrimeFaces.widgets.treeWidget.selectNode(node,silent): silent boolean parameter when passed as false, will not fire the Node Select Event, but does not change checkbox state, only selects/highlights the Node.

PrimeFaces.widgets.treeWidget.toggleCheckboxNode(node): check box state is toggled along with selecting of Node and then Node Select Event is fired.

Community
  • 1
  • 1
dev009
  • 755
  • 8
  • 13