2015-05-28 44 views
0

我想在每次加载层(从服务器获取数据)动态加载dojo中的树结构,只有在点击到下一层之后。这可以帮助我没有将整个树加载到内存中,但是当某人单击树的某个层级时,才会使用dojo中的Ajax请求从服务器获取下一级的所有元素。 有人可以帮助我如何去做这件事?DOJO:动态创建/加载/获取树

+0

这个答案就在这里如下:使用dijit的树,它只有3个步骤来创建懒洋洋地加载一个动态的树 – ssam

回答

0

对此的答案如下所示:使用dijit树,它只需3个步骤即可创建动态树,可以加载懒惰:1.创建存储区,2.创建模型,3.创建树。下面是我使用的代码片段,其完美地工作:

function createLazyTreeStore() 
{ 
    var store = new JsonRest({ 
     target: "location where your file is", 

      getChildren: function(object) { 
       // object may just be stub object, so get the full object first and then return it's 
       // list of children 
       return this.get(object.id).then(function(fullObject){ 
        console.log(fullObject.children); 
        return fullObject.children; 
       }); 
      } 
     }); 
    return store; 
} 
function createModel(store) 
{ 
    var model = new ObjectStoreModel({ 
     store: store, 
     getRoot: function(onItem) { 
      this.store.get("the .php file").then(function(item) 
      { 
       onItem(item[0]); 
      }); 
     }, 

     getChildren: function(object, onComplete, onError) 
     { 
      this.store.get("your url to fetch the data with parent child relation").then(function(fullObject) { 
      object.children = fullObject; 
      onComplete(object.children); 
      }, function(error) 
      { 
       console.error(error); 
       onComplete([]); 
      }); 
     }, 
     mayHaveChildren: function(object) { 

      return true; 
     } 
    }); 
    return model; 
} 
function createTree(model) 
{ 
    var tree = new Tree({ 
     model: model, 
     style: "your preference", 
     id: "tree", 
    }); 
    return tree; 
} 

Then call these functions in the following order: 
var store = createLazyTreeStore(); 
var model = createModel(store); 
var tree = createTree(model); 
accordionPane.addChild(tree);