如何仅为根添加Dojo树右键单击上下文菜单?不是为了剩下的孩子

时间:2010-04-09 05:42:40

标签: dojo

我使用dojo创建了一个树。 我想在树的根上添加右键单击上下文菜单。 以下是代码

<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();
        }
    });
</script>

但问题是这个代码创建了所有非叶子项的右键单击上下文菜单。 我想只为root用户右击上下文菜单。 怎么做到这一点?

1 个答案:

答案 0 :(得分:0)


根节点或第一级节点? 如果只有根tn.getParent(),那么如果这是根节点则为null。