You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Anu Padki <an...@gmail.com> on 2005/10/29 00:26:59 UTC

tree2 and checkboxes

I want to add checkbox to each node of the tree. When you click on the
checkbox, the backing bean should track the node it clicked on.
At presnet the node identifier stores the key of the node, I mean the
databse key.
What would be the best way to do this>
I thought of adding a map to the backing bean that stores the key of the
node and checkbox and then display this checkbox on the web page.
However, I cannot figure out how to access this map in the tree2's present
tags. So I am thinking of extending the treeNode class and adding this map
to it.
Any suggestions, please?
Thanks
- Anu

Re: tree2 and checkboxes

Posted by Enrico Nicola Mirco <at...@gmail.com>.
Hi,
the second FOR is correct in the first position where I wrote it,
because it must be execute only if the node is selected.

Ciao
Nicola

On 10/29/05, Enrico Nicola Mirco <at...@gmail.com> wrote:
> Hi,
> I think the right way is extend the TreeNodeBase class!
>
> See you this example:
>
> ***** TREENODE CLASS *****
> public class TreeNodeWithSelection extends TreeNodeBase {
>
>    private boolean m_selected;
>
>    public TreeNodeWithSelection() {
>    }
>
>    public TreeNodeWithSelection(String type, String description,
> boolean leaf) {
>        super(type, description, leaf);
>    }
>
>    public TreeNodeWithSelection(String type, String description,
> String identifier, boolean leaf) {
>        super(type, description, identifier, leaf);
>    }
>
>    public boolean isSelected() {
>        return m_selected;
>    }
>
>    public void setSelected(boolean selected) {
>        this.m_selected = selected;
>    }
>
>    public void setSelectedRecursively() {
>        Iterator iterator = this.getChildren().iterator();
>        while (iterator.hasNext()) {
>            TreeNodeWithSelection child =
> (TreeNodeWithSelection)iterator.next();
>            child.setSelected(this.isSelected());
>            child.setSelectedRecursively();
>        }
>    }
>
> }
> ***** TREENODE CLASS *****
>
> ***** CODE IN JSP PAGE *****
> <x:tree2 value="#{BackBean.treeData}"
> binding="#{BackBean.treeDataObject}" var="node" varNodeToggler="t"
> showRootNode="true">
>
>    <f:facet name="MYTYPE">
>
>            <x:commandLink action="#{BackBean.ChangeSelection}"
> styleClass="#{t.nodeSelected ? 'nodeFolderSelected':'nodeFolder'}"
> actionListener="#{t.setNodeSelected}">
>                <x:selectBooleanCheckbox value="#{node.selected}" onclick="submit();"/>
>                <x:outputText value="#{node.description}"/>
>                <x:outputText value=" (#{node.childCount})" styleClass="childCount"
> rendered="#{!empty node.children}"/>
>            </x:commandLink>
>
>    </f:facet>
>
> </x:tree2>
> ***** CODE IN JSP PAGE *****
>
>
>
> ***** BACKBEAN CLASS *****
> public class BackBean {
>
>    private TreeNodeWithSelection       m_treeData;
>    private HtmlTree            m_treeDataObject = new HtmlTree();
>
>    public String ChangeSelection() {
>
>        // Current node selected
>        String[] NodePathInfo = m_treeDataObject.getNodeId().split(":");
>        TreeNodeWithSelection selected_node = m_treeData;
>        for (int i=2; i<=NodePathInfo.length; i++){
>            selected_node =
> (TreeNodeWithSelection)selected_node.getChildren().get(Integer.valueOf(NodePathInfo[i-1]));
>        }
>
>        // Extend the selection to all children recursively
>        selected_node.setSelectedRecursively();
>
>        // If you want to select all parents...
>        // You can insert this code in the previous for...
>        if (selected_node.isSelected()) {
>            selected_node = m_treeData;
>            selected_node.setSelected(true);
>            for (int i=2; i<NodePathInfo.length; i++){
>                selected_node =
> (TreeNodeWithSelection)selected_node.getChildren().get(Integer.valueOf(NodePathInfo[i-1]));
>                selected_node.setSelected(true);
>            }
>        }
>
>        return null;
>    }
>
>    public TreeNodeWithSelection getTreeData() {
>        return m_treeData;
>    }
>
>    public void setTreeData(TreeNodeWithSelection treeData) {
>        this.m_treeData = treeData;
>    }
>
>    public HtmlTree getTreeDataObject() {
>        return m_treeDataObject;
>    }
>
>    public void setTreeDataObject(HtmlTree treeDataObject) {
>        this.m_treeDataObject = treeDataObject;
>    }
>
> }
> ***** BACKBEAN CLASS *****
>
> I hope this will help you
> Nicola
>
>
>
> On 10/29/05, Anu Padki <an...@gmail.com> wrote:
> > I want to add checkbox to each node of the tree.  When you click on the
> > checkbox, the backing bean should  track the node it clicked on.
> > At presnet the node identifier stores the key of the node, I mean the
> > databse key.
> > What would be the best way to do this>
> > I thought of adding a map to the backing bean that stores the key of the
> > node and checkbox and then display this checkbox on the web page.
> > However, I cannot figure out how to access this map in the tree2's present
> > tags. So I am thinking of extending the treeNode class and adding this map
> > to it.
> > Any suggestions, please?
> > Thanks
> > - Anu
> >
>

Re: tree2 and checkboxes

Posted by Enrico Nicola Mirco <at...@gmail.com>.
Hi,
I think the right way is extend the TreeNodeBase class!

See you this example:

***** TREENODE CLASS *****
public class TreeNodeWithSelection extends TreeNodeBase {

    private boolean m_selected;

    public TreeNodeWithSelection() {
    }

    public TreeNodeWithSelection(String type, String description,
boolean leaf) {
        super(type, description, leaf);
    }

    public TreeNodeWithSelection(String type, String description,
String identifier, boolean leaf) {
        super(type, description, identifier, leaf);
    }

    public boolean isSelected() {
        return m_selected;
    }

    public void setSelected(boolean selected) {
        this.m_selected = selected;
    }

    public void setSelectedRecursively() {
        Iterator iterator = this.getChildren().iterator();
        while (iterator.hasNext()) {
            TreeNodeWithSelection child =
(TreeNodeWithSelection)iterator.next();
            child.setSelected(this.isSelected());
            child.setSelectedRecursively();
        }
    }

}
***** TREENODE CLASS *****

***** CODE IN JSP PAGE *****
<x:tree2 value="#{BackBean.treeData}"
binding="#{BackBean.treeDataObject}" var="node" varNodeToggler="t"
showRootNode="true">

    <f:facet name="MYTYPE">

	    <x:commandLink action="#{BackBean.ChangeSelection}"
styleClass="#{t.nodeSelected ? 'nodeFolderSelected':'nodeFolder'}"
actionListener="#{t.setNodeSelected}">
		<x:selectBooleanCheckbox value="#{node.selected}" onclick="submit();"/>
		<x:outputText value="#{node.description}"/>
		<x:outputText value=" (#{node.childCount})" styleClass="childCount"
rendered="#{!empty node.children}"/>
	    </x:commandLink>
	
    </f:facet>

</x:tree2>
***** CODE IN JSP PAGE *****



***** BACKBEAN CLASS *****
public class BackBean {

    private TreeNodeWithSelection	m_treeData;
    private HtmlTree		m_treeDataObject = new HtmlTree();

    public String ChangeSelection() {

        // Current node selected
        String[] NodePathInfo = m_treeDataObject.getNodeId().split(":");
        TreeNodeWithSelection selected_node = m_treeData;
        for (int i=2; i<=NodePathInfo.length; i++){
            selected_node =
(TreeNodeWithSelection)selected_node.getChildren().get(Integer.valueOf(NodePathInfo[i-1]));
        }

        // Extend the selection to all children recursively
        selected_node.setSelectedRecursively();

        // If you want to select all parents...
	// You can insert this code in the previous for...
        if (selected_node.isSelected()) {
            selected_node = m_treeData;
            selected_node.setSelected(true);
            for (int i=2; i<NodePathInfo.length; i++){
                selected_node =
(TreeNodeWithSelection)selected_node.getChildren().get(Integer.valueOf(NodePathInfo[i-1]));
                selected_node.setSelected(true);
            }
        }

        return null;
    }

    public TreeNodeWithSelection getTreeData() {
        return m_treeData;
    }

    public void setTreeData(TreeNodeWithSelection treeData) {
        this.m_treeData = treeData;
    }

    public HtmlTree getTreeDataObject() {
        return m_treeDataObject;
    }

    public void setTreeDataObject(HtmlTree treeDataObject) {
        this.m_treeDataObject = treeDataObject;
    }

}
***** BACKBEAN CLASS *****

I hope this will help you
Nicola



On 10/29/05, Anu Padki <an...@gmail.com> wrote:
> I want to add checkbox to each node of the tree.  When you click on the
> checkbox, the backing bean should  track the node it clicked on.
> At presnet the node identifier stores the key of the node, I mean the
> databse key.
> What would be the best way to do this>
> I thought of adding a map to the backing bean that stores the key of the
> node and checkbox and then display this checkbox on the web page.
> However, I cannot figure out how to access this map in the tree2's present
> tags. So I am thinking of extending the treeNode class and adding this map
> to it.
> Any suggestions, please?
> Thanks
> - Anu
>