AJAX式多数据源及节点异步加载树_meetrice

本文介绍如何使用ExtJS框架实现AJAX式多数据源及节点异步加载功能。通过自定义`MyTreeLoader`类,实现从不同URL获取数据,并在树形组件中动态加载和显示这些数据。

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

预览:
1211963475-01-15kb.jpg
demo.html:

None.gif<script type="text/javascript" src="js/Ext.ux.MyTreeLoader/MyTreeLoader.js"></script>
None.gif
<script type="text/javascript" src="treeloader.js"></script>
None.gif
<div id="tree-ct"></div>

treeloader.js:

ExpandedBlockStart.gifContractedBlock.gif/**//**
InBlock.gif * @include "js/Ext.ux.MyTreeLoader/ColumnNodeUI.js"
InBlock.gif * @include "js/Ext.ux.MyTreeLoader/MyTreeLoader.js"
InBlock.gif * @include "js/Ext.ux.MyTreeLoader/TreeNodeProvider.js"
ExpandedBlockEnd.gif 
*/

None.gifExt.BLANK_IMAGE_URL 
= 'js/ext-2.0/resources/images/default/s.gif';
None.gif
var flag = true;// To toggle the refresh
None.gif

ExpandedBlockStart.gifContractedBlock.gifExt.onReady(
function() dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var treeNodeProvider = dot.gif{
InBlock.gif        data : [],
// Property in which are set the data to elaborate
InBlock.gif
        dataUrl : 'addressBook.do?dispatch=queryAddressBook',
ExpandedSubBlockStart.gifContractedSubBlock.gif        getNodes : 
function() dot.gif// Here you process your data
InBlock.gif
            return Ext.decode(this.data);
ExpandedSubBlockEnd.gif        }
,
ExpandedSubBlockStart.gifContractedSubBlock.gif        setData : 
function(data) dot.gif{// Called internally by
InBlock.gif
            // Ext.tree.MyTreeLoader by the method
InBlock.gif
            // updateTreeNodeProvider
InBlock.gif
            this.data = data;
ExpandedSubBlockEnd.gif        }
,
InBlock.gif        scope : 
this
InBlock.gif            
// Could be useful to use when you elaborates data to switch the
InBlock.gif
            // contextdot.gifnot used in this example and it's not required
ExpandedSubBlockEnd.gif
    }
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var treeNodeProvider2 = dot.gif{
InBlock.gif        data : [],
// Property in which are set the data to elaborate
InBlock.gif
        dataUrl : 'companyGroup.do?dispatch=queryGroup',
ExpandedSubBlockStart.gifContractedSubBlock.gif        getNodes : 
function() dot.gif// Here you process your data
InBlock.gif
            return Ext.decode(this.data);
ExpandedSubBlockEnd.gif        }
,
ExpandedSubBlockStart.gifContractedSubBlock.gif        setData : 
function(data) dot.gif{// Called internally by
InBlock.gif
            // Ext.tree.MyTreeLoader by the method
InBlock.gif
            // updateTreeNodeProvider
InBlock.gif
            this.data = data;
ExpandedSubBlockEnd.gif        }
,
InBlock.gif        scope : 
this
InBlock.gif            
// Could be useful to use when you elaborates data to switch the
InBlock.gif
            // contextdot.gifnot used in this example and it's not required
ExpandedSubBlockEnd.gif
    }
;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var myTreeLoader = new Ext.tree.MyTreeLoader(dot.gif{
InBlock.gif        treeNodeProvider : treeNodeProvider
ExpandedSubBlockEnd.gif    }
);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var myTreeLoader2 = new Ext.tree.MyTreeLoader(dot.gif{
InBlock.gif        treeNodeProvider : treeNodeProvider2
ExpandedSubBlockEnd.gif    }
);
InBlock.gif
InBlock.gif    myTreeLoader.updateTreeNodeProvider(myTreeLoader);
// if you want to
InBlock.gif
    // "preload"
InBlock.gif
    // the TreePanel with this
InBlock.gif
    // data
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif    
var ajaxCallGetDataForTree = function(inputParameters) dot.gif{
InBlock.gif        treePanel.body.mask(
"Loading datadot.gif");
InBlock.gif        setTimeout(ajaxCallbackGetDataForTree, 
1000);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var ajaxCallbackGetDataForTree = function() dot.gif{
InBlock.gif
InBlock.gif        treePanel.body.unmask();
ExpandedSubBlockStart.gifContractedSubBlock.gif        treePanel.body.highlight(
'#c3daf9'dot.gif{
InBlock.gif            block : 
true
ExpandedSubBlockEnd.gif        }
);
InBlock.gif        
// Simulating that I have received the response that is treeData2 from
InBlock.gif
        // the callback of the ajaxCall
InBlock.gif
        var rootNode = treePanel.getRootNode();// get the rootnode
InBlock.gif
        var loader = treePanel.getLoader();// Get the loader, note that is of
InBlock.gif
        // type MyTreeLoader
InBlock.gif
        loader.updateTreeNodeProvider(myTreeLoader);
InBlock.gif        loader.load(rootNode);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var ajaxCallGetDataForTree2 = function(inputParameters) dot.gif{
InBlock.gif        treePanel.body.mask(
"Loading datadot.gif");
InBlock.gif        setTimeout(ajaxCallbackGetDataForTree2, 
1000);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var ajaxCallbackGetDataForTree2 = function() dot.gif{
InBlock.gif
InBlock.gif        treePanel.body.unmask();
ExpandedSubBlockStart.gifContractedSubBlock.gif        treePanel.body.highlight(
'#c3daf9'dot.gif{
InBlock.gif            block : 
true
ExpandedSubBlockEnd.gif        }
);
InBlock.gif        
// Simulating that I have received the response that is treeData2 from
InBlock.gif
        // the callback of the ajaxCall
InBlock.gif
        var rootNode = treePanel.getRootNode();// get the rootnode
InBlock.gif
        var loader = treePanel.getLoader();// Get the loader, note that is of
InBlock.gif
        // type MyTreeLoader
InBlock.gif
        loader.updateTreeNodeProvider(myTreeLoader2);
InBlock.gif        loader.load(rootNode);
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
var flag = true;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var basePanelCfg = dot.gif{
InBlock.gif        title : 
"AJAX式多数据源及节点异步加载",
InBlock.gif        preloadChildren : 
true,
InBlock.gif        lines : 
false,
InBlock.gif        clearOnLoad : 
true,
InBlock.gif        rootVisible : 
false,
InBlock.gif        containerScroll : 
true,
InBlock.gif        frame : 
false,
InBlock.gif        collapsible : 
false,
InBlock.gif        animate : 
true,
InBlock.gif        loader : myTreeLoader,
ExpandedSubBlockStart.gifContractedSubBlock.gif        tbar : [
dot.gif{
InBlock.gif            text : 
"加载通讯录",
ExpandedSubBlockStart.gifContractedSubBlock.gif            handler : 
function() dot.gif{
InBlock.gif                
// Simulating change the treeNodeProvider of the loader
InBlock.gif
                var rootNode = treePanel.getRootNode();// get the rootnode
InBlock.gif
                var loader = treePanel.getLoader();// Get the loader, note that
InBlock.gif
                // is of type MyTreeLoader
InBlock.gif
                loader.setTreeNodeProvider(treeNodeProvider);
InBlock.gif                
// Simulating an ajax call
InBlock.gif
                ajaxCallGetDataForTree();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif        }
dot.gif{
InBlock.gif            text : 
"加载组织",
ExpandedSubBlockStart.gifContractedSubBlock.gif            handler : 
function() dot.gif{
InBlock.gif                
// Simulating change the treeNodeProvider2 of the loader
InBlock.gif
                var rootNode = treePanel.getRootNode();// get the rootnode
InBlock.gif
                var loader = treePanel.getLoader();// Get the loader, note that
InBlock.gif
                // is of type MyTreeLoader
InBlock.gif
                loader.setTreeNodeProvider(treeNodeProvider2);
InBlock.gif                
// Simulating a different ajax call
InBlock.gif
                ajaxCallGetDataForTree2();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
]
ExpandedSubBlockEnd.gif    }
;
InBlock.gif
InBlock.gif    
var treePanel = new Ext.tree.TreePanel(basePanelCfg);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
var root = new Ext.tree.AsyncTreeNode(dot.gif{
InBlock.gif        text : 
'Root',
InBlock.gif        draggable : 
false,
InBlock.gif        id : 
'1'
ExpandedSubBlockEnd.gif    }
);
InBlock.gif
InBlock.gif    treePanel.setRootNode(root);
InBlock.gif    treePanel.render(
'tree-ct');
ExpandedBlockEnd.gif}
);
None.gif

js/Ext.ux.MyTreeLoader/MyTreeLoader.js:

ExpandedBlockStart.gifContractedBlock.gif/**//*
InBlock.gif * Version 0.2.3
InBlock.gif * 
InBlock.gif * Ext.tree.MyTreeLoader
InBlock.gif * 
InBlock.gif * @author Dott. Ing. Marco Bellocchi @date 24. April 2008 @license
InBlock.gif * Ext.tree.MyTreeLoader.js is licensed under the terms of the Open Source LGPL
InBlock.gif * 3.0 license.
InBlock.gif * 
InBlock.gif * @include "js/Ext.ux.MyTreeLoader/ColumnNodeUI.js" @include
InBlock.gif * "js/Ext.ux.MyTreeLoader/MyTreeLoader.js" @include
InBlock.gif * "js/Ext.ux.MyTreeLoader/TreeNodeProvider.js"
InBlock.gif * 
InBlock.gif * License details: http://www.gnu.org/licenses/lgpl.html
ExpandedBlockEnd.gif 
*/

None.gif
ExpandedBlockStart.gifContractedBlock.gifExt.tree.MyTreeLoader 
= function(config) dot.gif{
InBlock.gif    
// @private
InBlock.gif
    var canFireLoadEvent = true;// private
InBlock.gif
    // @private
InBlock.gif
    var treeNodeProvider = null;
InBlock.gif    
// @private
InBlock.gif
    var loading = false;
InBlock.gif
InBlock.gif    Ext.apply(
this, config);
InBlock.gif    Ext.tree.MyTreeLoader.superclass.constructor.call(
this, config);
InBlock.gif    
// TO FIX I need to do that for retro compatibility, but you MUST use
InBlock.gif
    // getTreeNodeProvider to have access to it!
InBlock.gif
    treeNodeProvider = this.treeNodeProvider;
InBlock.gif    
// dataUrl = this.dataUrl;
InBlock.gif

InBlock.gif    
// @private
ExpandedSubBlockStart.gifContractedSubBlock.gif
    var processResponse = function(o, node, callback) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            node.beginUpdate();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for (var i = 0, len = o.length; i < len; i++dot.gif{
InBlock.gif                
var n = this.createNode(o[i]);
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (n) dot.gif{
InBlock.gif                    node.appendChild(n);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            node.endUpdate();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (e) dot.gif{
InBlock.gif            canFireLoadEvent 
= false;
InBlock.gif            
this.fireEvent("loadexception"this, node, treeNodeProvider.data);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// make sure we notify
InBlock.gif
        // the node that we finished
ExpandedSubBlockStart.gifContractedSubBlock.gif
        if (typeof callback == "function"dot.gif{
InBlock.gif            callback();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if (canFireLoadEvent === true)
InBlock.gif            
this.fireEvent("load"this, node, treeNodeProvider.data);// Passing
ExpandedSubBlockEnd.gif
    }
.createDelegate(this);
InBlock.gif
InBlock.gif    
// @private
ExpandedSubBlockStart.gifContractedSubBlock.gif
    var requestData = function(node, callback) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (this.fireEvent("beforeload"this, node, callback) !== falsedot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.transId = Ext.Ajax.request(dot.gif{
InBlock.gif                method : 
this.requestMethod,
InBlock.gif                url : treeNodeProvider.dataUrl 
|| treeNodeProvider.url,
InBlock.gif                
// success: this.handleResponse,
InBlock.gif
                success : handleResponse,
InBlock.gif                failure : 
this.fireEvent("loadexception"this, node,
InBlock.gif                        treeNodeProvider.data),
InBlock.gif                scope : 
this,
ExpandedSubBlockStart.gifContractedSubBlock.gif                argument : 
dot.gif{
InBlock.gif                    callback : callback,
InBlock.gif                    node : node
ExpandedSubBlockEnd.gif                }
,
InBlock.gif                params : 
this.getParams(node)
ExpandedSubBlockEnd.gif            }
);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else dot.gif{
InBlock.gif            canFireLoadEvent 
= false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }
.createDelegate(this);
InBlock.gif
InBlock.gif    
// @private
ExpandedSubBlockStart.gifContractedSubBlock.gif
    var handleResponse = function(response) dot.gif{
InBlock.gif        
var a = response.argument;
InBlock.gif        
this.processResponse(response, a.node, a.callback);
InBlock.gif        
this.fireEvent("load"this, a.node, response);
ExpandedSubBlockEnd.gif    }
.createDelegate(this);
InBlock.gif
InBlock.gif    
// @public
ExpandedSubBlockStart.gifContractedSubBlock.gif
    this.load = function(node, callback) dot.gif{
InBlock.gif        canFireLoadEvent 
= true;// Reset the flag
ExpandedSubBlockStart.gifContractedSubBlock.gif
        if (this.clearOnLoad) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
while (node.firstChild) dot.gif{
InBlock.gif                
var tmpNode = node.firstChild;
InBlock.gif                node.removeChild(tmpNode);
InBlock.gif                tmpNode.destroy();
// Destroy actually cascades, see,
InBlock.gif
                // http://extjs.com/forum/showthread.php?t=14993
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (this.doPreload(node)) dot.gif// preloaded json children
ExpandedSubBlockStart.gifContractedSubBlock.gif
            if (typeof callback == "function"dot.gif{
InBlock.gif                callback();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else if (treeNodeProvider) dot.gif{
InBlock.gif            requestData(node, callback);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// @public
ExpandedSubBlockStart.gifContractedSubBlock.gif
    this.isLoading = function() dot.gif{
InBlock.gif        
return loading;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// @public
ExpandedSubBlockStart.gifContractedSubBlock.gif
    this.updateTreeNodeProvider = function(obj) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (treeNodeProvider) dot.gif{
InBlock.gif            treeNodeProvider.setData(obj);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// @public
ExpandedSubBlockStart.gifContractedSubBlock.gif
    this.getTreeNodeProvider = function() dot.gif{
InBlock.gif        
return treeNodeProvider;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
// Set a new treeNodeProvider
InBlock.gif
    // @public
ExpandedSubBlockStart.gifContractedSubBlock.gif
    this.setTreeNodeProvider = function(newTreeNodeProvider) dot.gif{
InBlock.gif        
if (newTreeNodeProvider == null
InBlock.gif                
|| (typeof newTreeNodeProvider == 'undefined'))
InBlock.gif            
throw 'setTreeNodeProvider, newTreeNodeProvider == null || (typeof newTreeNodeProvider == undefined)';
InBlock.gif        treeNodeProvider 
= newTreeNodeProvider;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}
;
None.gifExt.tree.MyTreeLoader 
= Ext.extend(Ext.tree.MyTreeLoader, Ext.tree.TreeLoader);
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值