<script>
/*create namespace for examples:*/
YAHOO.namespace("example");
/* Using Crockford's "Module Pattern": */
YAHOO.example.treeExample = function() {
var tree, currentIconMode;
function changeIconMode() {
var newVal = parseInt(this.value);
if (newVal != currentIconMode) {
currentIconMode = newVal;
}
buildTree();
}
function loadNodeData(node, fnLoadComplete) {
//We'll randomize our loader with stock data; in many implementations,
//this step would be replaced with an XMLHttpRequest call to the server
//for more data.
//Array of India's States
var aStates = ["Andhra Pradesh","Arunachal Pradesh","Assam","Bihar","Chhattisgarh","Goa","Gujarat","Haryana","Himachal Pradesh","Jammu and Kashmir","Jharkhand","Karnataka","Kerala","Madhya Pradesh","Maharashtra","Manipur","Meghalaya","Mizoram","Nagaland","Orissa","Punjab","Rajasthan","Sikkim","Tamil Nadu","Tripura","Uttaranchal","Uttar","Pradesh","West Bengal"];
//Random number determines whether a node has children
var index = Math.round(Math.random()*100);
//if our random number is in range, we'll pretend that this node
//has children; here, we'll indicate that 70% of nodes have
//children.
if (index>30) {
//We'll use a random number to determine the number of
//children for each node:
var childCount = (Math.round(Math.random()*5) + 1);
//This is important: The primary job of the data loader function
//is to determine whether the node has children and then to
//actually create the child nodes if they are needed; here, we'll
//loop through to create each child node:
for (var i=0; i<childCount; i++) {
thisState = aStates[Math.round(Math.random()*27)];
var newNode = new YAHOO.widget.TextNode(thisState, node, false);
}
}
//When we're done creating child nodes, we execute the node's
//loadComplete callback method which comes in as our loader's
//second argument (we could also access it at node.loadComplete,
//if necessary):
fnLoadComplete();
}
function buildTree() {
//create a new tree:
tree = new YAHOO.widget.TreeView("treeContainer");
//turn dynamic loading on for entire tree:
tree.setDynamicLoad(loadNodeData, currentIconMode);
//get root node for tree:
var root = tree.getRoot();
//add child nodes for tree:
var tmpNode1 = new YAHOO.widget.TextNode("First Node", root, false);
var tmpNode2 = new YAHOO.widget.TextNode("Second Node", root, false);
var tmpNode3 = new YAHOO.widget.TextNode("Third Node", root, false);
var tmpNode4 = new YAHOO.widget.TextNode("Fourth Node", root, false);
var tmpNode5 = new YAHOO.widget.TextNode("Fifth Node", root, false);
//render tree with these five nodes; all descendants of these nodes
//will be generated as needed by the dynamic loader.
tree.draw();
}
return {
init: function() {
YAHOO.util.Event.on(["mode0", "mode1"], "click", changeIconMode);
var el = document.getElementById("mode1");
if (el && el.checked) {
currentIconMode = parseInt(el.value);
} else {
currentIconMode = 0;
}
buildTree();
}
}
} ();
YAHOO.util.Event.addListener(window, "load", YAHOO.example.treeExample.init, YAHOO.example.treeExample,true)
</script>
参照网址:http://www.java2s.com/Code/JavaScript/GUI-Components/DynamicTreeViewExample.htm
分享到:
相关推荐
在源码层面,YUI的设计遵循模块化原则,通过YUI Loader组件可以动态加载所需的模块,实现了按需加载,从而减少了页面的初始加载时间。学习YUI源码有助于理解其内部机制,对于提升JavaScript编程技巧和优化代码性能有...
例如,你可以在`click`事件中执行异步加载子节点的数据,提高用户体验。 6. **性能优化** 在处理大量节点时,性能优化是关键。YUI3 TreeView支持虚拟化技术,只渲染当前可视区域内的节点,从而提高滚动性能。 7. ...
YUI包含了一系列的模块,如事件处理、动画效果、数据绑定、用户界面组件等,其中就包括了用于构建树形结构的组件——YUI TreeView。 **TreeView组件** 是YUI中的一个重要部分,它允许开发者在网页上展示层次化的...
- **YUI_config.listener**:允许开发者定义回调函数,当新的YUI组件被加载到页面时触发。 #### 二、YUI提供的Dom操作 YUI的DOM操作模块简化了对DOM树的操作,使得常见的DOM操作变得简单易行。 **2.1 Element的...
2. **创建`TreeView`**:根据数据模型创建`TreeView`实例,并将其添加到`Overlay`的内容区域中。 3. **配置`DD`**:为每个树节点启用`Drag`功能,这包括设置拖动的Handle(通常是节点的图标或文本),并注册`Drag`...
3. 初始化Tree控件:在JavaScript中,使用YUI实例化一个TreeView对象,并将数据结构传递给它。 4. 渲染Tree控件:将TreeView对象附加到HTML文档的某个元素上,使树形结构可见。 5. 添加事件监听器:可以为节点添加...
- YUI TreeView Control 支持通过XMLHttpRequest动态加载节点数据,允许自定义节点元数据。 - 它是免费的。 9. **Fonshen JS MenuTree** - 风声JS菜单树基于JavaScript、XHTML和CSS实现,支持无限级别和多种展开...
YUI_TreeView.mxp 按照W3C的HTML4.01的语法测试HTML标准插件.mxp 表单验证插件.mxp 表格行颜色交替插件.mxp 层变换特效2.0.mxp 层动画效果插件.mxp 层或框架中调用URL文件插件.mxp 插入mtx、mtz格式等Viewpoint ...