0
votes

in a first form i ve a primefaces multiple selection tree(populated from LDAP) and in another form i ve a selectOneMenu primefaces component. i would like to make the default selectOneMenu value(the first displayed on this menu) to be the value of the tree selected node. i ve tryed to use f:ajax.. but it doesnt work as these elements doesnt belong to the same form (the selectOneMenu name in the render attribute was unknown..)

here is my index.xhtml which contain all these elements :

 <?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Insert title here</title>
    <script src="JS/general.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="CSS/general.css" />
    <script src="clean/clean-ajax-all.js"></script>
    <script>
        var crudmenu = "crudmenu";
        var adduserform = "adduserform";
    </script>
</h:head>
<body>

    <h:form id="form">

        <p:growl id="messages" showDetail="true" escape="false" />

        <p:tree value="#{treeBean.root}" var="node"
            onNodeClick="this.form.submit();" selectionMode="multiple"
            selection="#{treeBean.selectedNodes}" id="treeMultiple">

            <p:treeNode>
                <h:outputText value="#{node}" />
            </p:treeNode>
        </p:tree>


        <p:commandButton value="Display Selected" update="messages"
            actionListener="#{treeBean.displaySelectedMultiple}" id="btnDisplay" rendered="false"/>  

    </h:form>


    <div id="adduserform" name="adduserform"
        style="text-align: center; overflow: hidden; height: 0px; width: 270px; border: #040600 1px;">


        <h:form>
            <h:panelGrid border="1" columns="2">
            ID :  <p:selectOneMenu value="" panelStyle="width:150px"
                    effect="fade" var="p" style="width:160px" filter="true"
                    filterMatchMode="startsWith">
                    <f:selectItem itemLabel="#{treeBean.selectedNodeValue}"
                        itemValue="" />

                </p:selectOneMenu> 


    objectClass : <p:selectOneMenu value="" panelStyle="width:150px"
                    effect="fade" var="p" style="width:160px" filter="true"
                    filterMatchMode="startsWith">
                    <f:selectItem itemLabel="Select One" itemValue="" />

                </p:selectOneMenu>


            </h:panelGrid>
        </h:form>
    </div>



</body>
</html>

and here is my managed bean :

package org.primefaces.examples.view;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
//other imports..

@ManagedBean(name = "treeBean")
public class TreeBean implements Serializable {


    private TreeNode root;

    private TreeNode[] selectedNodes;

    private String selectedNodeValue;


    public TreeBean() {

        populateTreeFromLdap();

    }


    public String getSelectedNodeValue() {
        if (selectedNodes != null && selectedNodes.length == 1)
            for (TreeNode node : selectedNodes)
            selectedNodeValue = node.getData().toString(); 
        return selectedNodeValue;
    }


    public void setSelectedNodeValue(String selectedNodeValue) {
        this.selectedNodeValue = selectedNodeValue;
    }


    public void populateTreeFromLdap(){
     //code to get entries from LDAP..
            }               


    }

    public TreeNode getRoot() {
        return root;
    }

    public TreeNode[] getSelectedNodes() {
        return selectedNodes;
    }

    public void setSelectedNodes(TreeNode[] selectedNodes) {
        this.selectedNodes = selectedNodes;
    }

    public void displaySelectedMultiple(ActionEvent event) {
        if (selectedNodes != null && selectedNodes.length > 0) {
            StringBuilder builder = new StringBuilder();

            for (TreeNode node : selectedNodes) {
                builder.append(node.getData().toString());
                builder.append("<br />");
            }

            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Selected", builder.toString());

            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }
}

The problem is how to refresh the "#{treeBean.selectedNodeValue}" of the selectOneMenu 's itemLabel after the tree node selection happen..

By the way, i m new in jsf and i m really upset that the traditional ajax use for loading a content in a div from another page doesnt work anymore in jsf..i used that in some previous jsp/servlet applications and it work fine..

thnx for help !!

1
Is there a reason that you need 2 forms? Things will be much simpler if you use a single form. - SteveS
i would prefer keep each form apart but i ll try that ! thank u :) - Bardelman
If I understand what you are trying to do, I would next bind the selectOneMenu to a value on the backing bean. In your getSelectedNodeValue() I would set that value. The final piece to make this work is to use an lisenter events as shown here: primefaces.org/showcase-labs/ui/treeEvents.jsf. - SteveS
Actually, does the selectOneMenu serve a purpose other than to show the selected node? It looks like your dropdown is only going to have a single item in the selection list. If this is the case, can you just use an h:outputtext to display the data you want? - SteveS
No SteveS, it s an unfinished code :) i ve put all in one form as u said and had the "<f:ajax> contains an unknown id" again but resolved it by getting the right id from the page source code thnx to this post stackoverflow.com/questions/9858699/… i ve also added all p:ajax with lisenter events from your example to my tree and now it works fine !! u deserve 10 reputation points man ! ^^ - Bardelman

1 Answers

1
votes

If I understand what you are trying to do, I would next bind the selectOneMenu to a value on the backing bean. In your getSelectedNodeValue() I would set that value. The final piece to make this work is to use an listener events as shown here: primefaces.org/showcase-labs/ui/treeEvents.jsf.