Search code examples
jqueryjquery-pluginsstruts2-json-plugin

Struts2JQuery JS Tree generation & binding


How to implement Struts2Jquery js tree to generate a tree and bind a function to the nodes ?


Solution

  • <%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <%@ taglib prefix="sjt" uri="/struts-jquery-tree-tags"%>
    <html>
    <head>
    <sj:head />
    <script type="text/javascript"
        src="http://www.trendskitchens.co.nz/jquery/contextmenu/jquery.contextmenu.r2.packed.js"></script>
        <script type='text/javascript'>
            jQuery(document).ready(function() {
                jQuery.struts2_jquery.require("js/struts2/jquery.tree.struts2-3.2.1"
                    + jQuery.struts2_jquery.minSuffix + ".js");
                //Binding a function to the click of the nodes
                $.subscribe('clickFunction', function(event,element) {
                    var key = $(".jstree-clicked").parent().attr("key");
                    foo(key);
                });
                refreshTree(id);
            });
            function foo(key) {
                alert(key);
            }
    
            // This function is used to draw the tree and can also be called to refresh the tree.
            function refreshTree(id) {
                var options_jsonTree = {};
                options_jsonTree.treetheme = "default";
                options_jsonTree.url = "StrutsAction.action?id=" + id;
                options_jsonTree.onclick = "clickFunction";
                options_jsonTree.plugins = "contextmenu";
                options_jsonTree.jqueryaction = "tree";
                options_jsonTree.id = "jsonTree";
                jQuery.struts2_jquery_tree.bind(jQuery('#jsonTree'),options_jsonTree);
            }
        </script>
    </head>
    <body>
    <div id="jsonTree"></div>
    </body>
    </html>