开发者

How to add Dojo tree right click context menu only for the root? not for remaining children

开发者 https://www.devze.com 2022-12-25 23:40 出处:网络
I have created a tree using dojo. I want to add right click context menu on the root of the tree. Following is the code

I have created a tree using dojo. I want to add right click context menu on the root of the tree. Following is the code

<head>
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.3/dijit/themes/tundra/tundra.css">
    <style type="text/css">
        body, html { font-family:helvetica,arial,sans-serif; font-size:90%; }
    </style>
</head>

<body class="tundra ">
    <ul dojoType="dijit.Menu" id="tree_menu" style="display: none;">
        <li dojoType="dijit.MenuItem" onClick="alert('Hello world');">
            Item #1
        </li>
        <li dojoType="dijit.MenuItem">
            Item #2
        </li>
    </ul>
    <div dojoType="dojo.data.ItemFileReadStore" jsId="menuContinentStore"
    url="http://docs.dojocampus.org/moin_static163/js/dojo/trunk/dijit/tests/_data/countries.json">
    </div>
    <div dojoType="dijit.tree.ForestStoreModel" jsId="menuContinentModel"
    store="menuContinentStore" query="{type:'continent'}" rootId="continentRoot"
    rootLabel="Continents" childrenAttrs="children">
    </div>
    <div dojoType="dijit.Tree" id="menuTree" model="menuContinentModel" showRoot="false"
    openOnClick="true">
        <script type="dojo/connect">
            var menu = dijit.byId("tree_menu");
            // when we right-click anywhere on the tree, make sure we open the menu
            menu.bindDomNode(this.domNode);

            dojo.connect(menu, "_openMyself", this, function(e) {
                // get a hold of, and log out, the tree node that was the source of this open event
                var tn = dijit.getEnclosingWidget(e.target);
                console.debug(tn);

                // now inspect the data store item that backs the tree node:
                console.debug(tn.item);

                // contrived condition: if this tree node doesn't have any children, disable all of the menu items
                menu.getChildren().forEach(function(i) {
                    i.attr('disabled', !tn.item.children);
                });

                // IMPLEMENT CUSTOM MENU BEHAVIOR HERE
            });
        </script>
    </div>
</body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.3/dojo/dojo.xd.js"
djConfig="parseOnLoad: true">
</script>
<script>
    dojo.require("dijit.Menu");
    dojo.require("dijit.MenuItem");
    dojo.require("dijit.tree.ForestStoreModel");
    dojo.require("dojo.data.ItemFileReadStore");
    dojo.require("dijit.Tree");
</script>
<!-- NOTE: the following script tag is not intended for usage in real
world!! it is part of the CodeGlass and you should just remove it when
you use the code -->
<script type="text/javascript">
    dojo.addOnLoad(function() {
        if (window.pub) {
            window.pub(开发者_StackOverflow中文版);
        }
    });
</script>

But the problem is this code creats right click context menu for all non leaf children. I want right click context menu only for root. How to achieve this?



root node or first level's node ? If for root only than tn.getParent() is null if this is a root node.

0

精彩评论

暂无评论...
验证码 换一张
取 消