下面的代碼顯示了樹節點
<!DOCTYPE html>
<html >
<head>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.7.2/dijit/themes/claro/claro.css" />
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/resources/dojo.css" />
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dijit/themes/tundra/tundra.css" />
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js" data-dojo-config="parseOnLoad: true"></script>
<script>require(["dojo/parser", "dojo/store/Memory", "dijit/Menu", "dijit/MenuItem", "dijit/tree/ObjectStoreModel", "dijit/Tree"]);</script>
<script type="text/javascript">
dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dijit.Tree");
dojo.require("dijit.Menu");
dojo.require("dijit.MenuItem");
dojo.require("dijit.tree.ForestStoreModel");
dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dijit.Tree");
var rawdata = [ {
label: 'Vegetables',
id: '1',
children: [ { label: 'tomato', id: '1.1' }, { label: 'onion', id: '1.2' } ]
}, {
label: 'Fruits',
id: '2',
children: [
{ id: '2.1', label: 'Apple' },
{ id: '2.3', label: 'Grapes' }
]
} ];
function prepare(){
var store = new dojo.data.ItemFileReadStore({
data: { identifier: 'id', label : 'label', items: rawdata }
});
var treeModel = new dijit.tree.ForestStoreModel({ store: store });
var treeControl = new dijit.Tree({
model: treeModel,
showRoot: false,
_createTreeNode: function(/*Object*/ args){
var tnode = new dijit._TreeNode(args);
tnode.labelNode.innerHTML = args.label;
return tnode;
}
}, "treeOne");
var AllMenu = new dijit.Menu({ style: "display: none;"});
var menuItem1 = new dijit.MenuItem({
label: "Add Instance",
iconClass:"",
style:"background-color:#4B57FA",
onClick: function(){ alert('started'); }
});
AllMenu.addChild(menuItem1);
AllMenu.bindDomNode(treeControl.domNode);
}
dojo.ready(prepare);
</script>
</head>
<body class="claro">
<div id="treeOne"> </div>
</body>
</html>
來源
2012-08-01 10:06:02
ram
這幫助我,特別是最後一個環節的菜單的onclick。基本上,您使用dijit/Tree的onOpen()方法遍歷樹的節點,並使用dijit/Menu的bindDomNode()方法爲每個節點添加正確的dijit/Menu。 – voidstate 2012-10-18 20:19:43