require(["dojo/aspect", "dojo/_base/window","dojo/store/Memory", "dojo/store/Observable",
"dijit/tree/ObjectStoreModel", "dijit/Tree", "dojo/parser",
"dijit/tree/dndSource","dojo/topic"], function(aspect, win, Memory, Observable, ObjectStoreModel, Tree, parser, dndSource, topic){
try{
var treeJSON = [{"id": "0", "name":"TreeTop", \t "type":"Enterprise", "parent":"", "sort_key":"0",},{"id": "1", "name":"West", "type":"Region", "parent":"0", "sort_key":"1"},{"id": "2", "name":"East", "type":"Region", "parent":"0", "sort_key":"2"},{"id": "3", "name":"SE", "type":"Region", "parent":"2", "sort_key":"0"}];
var myStore = new Memory({data: treeJSON});
myStore.getChildren = function(object) {
return this.query({parent: object.id}, {sort: [{attribute: "sort_key"}]});
};
aspect.around(myStore, "put", function(originalPut) {
\t return function(obj, options) {
\t if (options && options.parent) {
\t obj.parent = options.parent.id;
\t }
\t return originalPut.call(myStore, obj, options);
}
});
myStore = new Observable(myStore);
EvModel = new ObjectStoreModel({
store: myStore,
query: { id: "0" }
});
topic.subscribe("/dnd/drop",treeDropEvt2);
tree = new Tree({
autoExpand: true, // <== this was missing
model: EvModel,
dndController: dndSource,
//onDndDrop: treeDropEvt,
checkAcceptance:dndAccept,
\t checkItemAcceptance:itemTreeCheckItemAcceptance,
\t dragThreshold:8,
\t betweenThreshold: 5
});
tree.placeAt('currTree');
\t \t tree.onLoadDeferred.then(function(){
console.log('onLoad event');
});
tree.set('paths',[['0','2','3']]); // Expand tree and highligh 'SE'
tree.startup();
} catch(err) {
alert(err);
}
})
function treeDropEvt(source, nodes, copy) {
console.log('treeDropEvt');
console.dir(source);
console.dir(nodes);
console.dir(copy);
}
function treeDropEvt2(source, nodes, copy, target) {
console.log('treeDropEvt2');
console.dir(source);
console.dir(nodes);
console.dir(copy);
}
function dndAccept(source,nodes){
console.log('dndAccept');
console.dir(source);
console.dir(nodes);
return this.tree.id != "myTree";
}
function itemTreeCheckItemAcceptance(node,source,position){
source.forInSelectedItems(function(item){
console.log("testing to drop item of type " + item.type[0] + " and data " + item.data + ", position " + position);
});
var item = dijit.getEnclosingWidget(node).item;
console.log('getEnclosingWidget(node).item: ');
console.dir(item);
console.dir(dijit.getEnclosingWidget(node));
return position;
}
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dijit/themes/claro/claro.css" />
<body class="claro">
<table border=1>
<tr><td style="text-align: center;">Current Tree</td></tr>
<tr><td style="vertical-align: top">
<div id="currTree"></div>
</td></tr>
</table>
</body>
我不能相信我錯過了樹的屬性..大聲笑!謝謝!! – GoinOff
更多的看這個解決方案之後,它不是我想要的。這有效,但樹必須開始擴展,這不是我想要的。如果您想從完全展開的樹開始,這是一個很好的解決方案。 – GoinOff
是的,樹在開始的時候是擴展的,但是相同的情況在鏈接的演示中。您可以在創建後在樹上調用'collapseAll'方法(請參閱演示中的第一個按鈕)。 – barbsan