Tree non-async creation "bug" - Ext JS

本文探讨了在ExtJS中正确配置树形组件的方法,特别是如何使用AsyncTreeNode及TreeLoader来实现多级节点的自动加载。文章通过示例代码详细解释了如何避免常见错误,并确保所有子节点都能被正确加载和显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When building a tree without using a treeloader, I can't seem to be able to specify the children of a node. Instead, I have to manually call "new Ext.tree.TreeNode()" for every node I want and then manually add it to the tree using the appendChild() method. Ideally, we should be able to pass a config as deep as necessary (like we can with AsyncTreeNode) and it'll all get added to the tree.

Does not work (grandchildren aren't created):

      Ext.onReady(function() { 
       
         var tree = new Ext.tree.TreePanel('tree'); 

         var root = new Ext.tree.TreeNode({ 
            text : 'My Root', 
            id : 'my-root'
         }); 
         
         tree.setRootNode(root);

         var child1 = new Ext.tree.TreeNode({ text: 'Child 1', id: 'child-1' });
         var child2 = new Ext.tree.TreeNode({ text: 'Child 2', id: 'child-2' });
         var child3 = new Ext.tree.TreeNode({ text: 'Child 3', id: 'child-3', children: [ 
                  { text: 'Grandchild 1', id: 'grandchild-1' }, 
                  { text: 'Grandchild 2', id: 'grandchild-2' } 
               ]});
          root.appendChild(child1, child2, child3);
          
         tree.render(); 
      });
Also does not work (children aren't even created):

      Ext.onReady(function() { 
       
         var tree = new Ext.tree.TreePanel('tree'); 

         var root = new Ext.tree.TreeNode({ 
            text : 'My Root', 
            id : 'my-root', 
            children: [ 
               { text: 'Child 1', id: 'child-1' }, 
               { text: 'Child 2', id: 'child-2' }, 
               { text: 'Child 3', id: 'child-3', children: [ 
                  { text: 'Grandchild 1', id: 'grandchild-1' }, 
                  { text: 'Grandchild 2', id: 'grandchild-2' } 
               ]} 
            ] 
         }); 
          
         tree.setRootNode(root);      // Generates error: tree has no properties! 
          
         tree.render(); 
      });
Reply With Quote
  #2  
Old 03-04-2007, 09:21 PM
Default

In order to get the automated conversion of JSON to nodes, it goes through the TreeLoader. Think of the TreeLoader as not only loading through the server, but also in the browser as well. So you need to use an AsyncTreeNode instead of TreeNode. Luckily this involves no extra work.
Reply With Quote
  #3  
Old 03-04-2007, 10:06 PM
Default

Ok, fair enough. So, theoretically, changing Ext.tree.TreeNode to Ext.tree.AsyncTreeNode should do the trick. However, the following doesn't work:

      Ext.onReady(function() { 
       
         var tree = new Ext.tree.TreePanel('tree'); 

         var root = new Ext.tree.AsyncTreeNode({ 
            text : 'My Root', 
            id : 'my-root', 
            children: [ 
               { text: 'Child 1', id: 'child-1' }, 
               { text: 'Child 2', id: 'child-2' }, 
               { text: 'Child 3', id: 'child-3', children: [ 
                  { text: 'Grandchild 1', id: 'grandchild-1' }, 
                  { text: 'Grandchild 2', id: 'grandchild-2' } 
               ]} 
            ] 
         }); 
          
         tree.setRootNode(root);
          
         tree.render(); 
      });
It shows a folder with a plus next to "My Root", but clicking it it switches to the spinning loading image but never does anything after that. There aren't any JS errors and the children are never loaded.

Thinking maybe it's just an issue with trying to stuff all that in the root before it's set as the root node, I tried this:

      Ext.onReady(function() { 
       
         var tree = new Ext.tree.TreePanel('tree'); 

         var root = new Ext.tree.TreeNode({ 
            text : 'My Root', 
            id : 'my-root'
         }); 
          
         tree.setRootNode(root);
          
         var child1 = new Ext.tree.TreeNode({ text: 'Child 1', id: 'child-1' }); 
         var child2 = new Ext.tree.TreeNode({ text: 'Child 2', id: 'child-2' }); 
         var child3 = new Ext.tree.AsyncTreeNode({ text: 'Child 3', id: 'child-3', children: [ 
                   { text: 'Grandchild 1', id: 'grandchild-1' }, 
                   { text: 'Grandchild 2', id: 'grandchild-2' } 
               ]}); 
         root.appendChild(child1, child2, child3); 

         tree.render(); 
      });
The AsyncTreeNode always ends up spinning when you try to expand it.
Reply With Quote
  #4  
Old 03-04-2007, 10:09 PM
Default

> In order to get the automated conversion of JSON to nodes, it goes through the TreeLoader

Reply With Quote
  #5  
Old 03-04-2007, 10:19 PM
Default

Ok, so the solution is to explicitly define a loader for the TreePanel.

      Ext.onReady(function() { 
       
        var tree = new Ext.tree.TreePanel('tree', {
          loader: new Ext.tree.TreeLoader()
        }); 

        var root = new Ext.tree.AsyncTreeNode({ 
          text : 'My Root', 
          id : 'my-root', 
          children: [ 
            { text: 'Child 1', id: 'child-1', leaf: true }, 
            { text: 'Child 2', id: 'child-2', leaf: true }, 
            { text: 'Child 3', id: 'child-3', children: [ 
              { text: 'Grandchild 1', id: 'grandchild-1', leaf: true }, 
              { text: 'Grandchild 2', id: 'grandchild-2', leaf: true } 
            ]} 
          ] 
        }); 
        
        tree.setRootNode(root);
        
        tree.render(); 
      });
Notice I also indicated which nodes are leaves. If you don't do that then attempting to expand a leaf node will result in a never-ending loading indicator.
Reply With Quote
  #6  
Old 03-05-2007, 08:55 AM
Default

Thanks for figuring this out Jeff/Jack. I tried 20 different ways to get this to work and, I don't think I would have ever thought to try that...

loader: new Ext.tree.TreeLoader()
Simeon
Reply With Quote
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值