学习YUI.Ext 第六天--关于树TreePanel(Part 1)


Posted in Javascript onMarch 10, 2007

学习YUI.Ext 第五天--关于树TreePanel(Part 1) 
效果演示:http://www.ajaxjs.com/yuicn/demos/order_tree.asp
    树组件是YUI.Ext 0.40 新增的组件。虽然YUI已经自带有TREE VIEW的组件,但JACK还是决定重新开发。具体原因在http://www.ajaxjs.com/yuicn/article.asp?id=20070245(翻译文章)或http://www.jackslocum.com/blog/2006/12/29/preview-drag-and-drop-enhancements-and-the-new-treepanel/ (原文) 
一、加载一个同步Tree:

var TreeTest = function(){  
var Tree = YAHOO.ext.tree;// 快捷方式  
return {  
    init : function(){  
    var tree = new Tree.TreePanel('tree_div', {//需要一个tree_div的holder  
    animate:true, //是否动画  
    loader: new Tree.TreeLoader({dataUrl:'get_nodes.asp'}), //调用一个JSON  
    enableDD:false,// 是否支持拖放  
    containerScroll: true  
});  
// 设置根节点  
var root = new Tree.AsyncTreeNode({  
text: 'Frank的作品',  //根节点文字  
draggable:false, //根节点是否可拖放  
id:'source'  
});  
tree.setRootNode(root);  
// 渲染 tree  
tree.render(false,false);  
// false for not recursive (the default), false to disable animation  
root.expand(false,false);  
}  
};  
}();  
YAHOO.ext.EventManager.onDocumentReady(TreeTest.init, TreeTest, true);

通过XHR调用这个get_nodes.asp文件,假设服务器返回这样一个JSON(有关JSON的介绍:http://www.json.org/json-zh.html):
[{
"text":"yui-ext.js","id":"\/yui-ext.js","leaf":true,"cls":"file"
} ,{
"text":"yui-ext-1118.php","id":"\/yui-ext-1118.php","leaf":true,"cls":"file"
} ,{
"text":"yui-ext-1228.php","id":"\/yui-ext-1228.php","leaf":true,"cls":"file"
} ,{
"text":"build","id":"\/build","cls":"folder"
} ,{
"text":"source","id":"\/source","cls":"folder"
} ,{
"text":"yui-ext-1123.php","id":"\/yui-ext-1123.php","leaf":true,"cls":"file"
} ,{
"text":"yui-ext-1203.php","id":"\/yui-ext-1203.php","leaf":true,"cls":"file"
} ]
  Server端JSON的输出(ASP JScript)  
var goods = new dbOpen();  
goods.GetSQL ="select * from goodsbigclass";  
with(goods){  
    GetRS(1);  
    var str="";  
    str+="[";  
    do{  
        str+='{"text":"'+rs("BigClassName")+'","id":"\/yui-ext.js","leaf":true,"cls":"file","href":"?b_id='+rs("BigClassID")+'"},';  
        rs.MoveNext();  
    }while(!rs.EOF);  
    str+="]";  
    Response.Write(str);  
    Close();  
}  
goods= null; 

解释:
“text”-->显示的文本
"id"-->id值 
“leaf”-->Boolean值,如果“叶子”是真的话,则不能包含子节点Children nodes 
"cls"-->选用的样式,通常在这里选定图标
”href“-->指定的url,还有一个”hrefTarget“的属性
另外,除了以上的属性,您还可以在JSON加入任何的属性,作为节点的属性,见Jack原话:
The href attribute is called "href", there's also an "hrefTarget" attribute. For capturing node clicks, you can listen on individual nodes or you can listen for "click" on the tree which will pass you the node that was clicked. FYI, you can put any attributes you want in the json config for the node and it will be available as node.attributes. FAQ.4会继续解释这个问题。
FQA常见问题:
1.Tree支持XML数据交换吗?
A:暂不支持,据FOURM上的话,以后会提供支持,见:
can I use xml instead of json for sending nodes hirerachy ?
Correct me if I'm wrong but I think the answer is no here. But that doesn't mean it won't be supported later on. 
2.我想用单击代替双击展开子节点,可以吗?
A:可以,见: 
tree.on('click', function(node){ 
    if(!node.isLeaf()){ 
        node.toggle(); 
    } 
}); 
3.事件处理的几种情形:
A: a.当加入某个节点时,为其增加事件 
tree.on('append', function(tree, node){ 
     if(node.id == 'foo'){ 
         // 这里加入你的事件(如click)侦听器(addListener())
     } 
});b.针对某个节点的单击事件 
tree.on('click', function(node){ 
     if(node.id == 'foo'){ 
         // do something 
     } 
});c.针对某个区域(集合)的事件 
// fires any time the selection in the tree changes 
tree.getSelectionModel().on('selectionchange', function(sm, node){ 
     if(node && node.id == 'foo'){ 
         // do something 
     } 
}); 
4.如何获取JSON中的自定义字段(或称作参数 parameters)
A:JSON对象已经被构建函数 construction传递到TreeNode中,作为node.attributes 出现,所以调用属性node.attributes 便可获取。详见:http://www.yui-ext.com/forum/viewtopic.php?t=2253 
tree.on('click', function(node){ 
    if(!node.isLeaf()){ 
        node.toggle(); 
    } 
});
Javascript 相关文章推荐
测试你的JS的掌握程度的代码
Dec 09 Javascript
js 连接数据库如何操作数据库中的数据
Nov 23 Javascript
javascript里绝对用的上的字符分割函数总结
Jul 31 Javascript
JS仿百度自动下拉框模糊匹配提示
Jul 25 Javascript
JS自定义混合Mixin函数示例
Nov 26 Javascript
微信小程序 video详解及简单实例
Jan 16 Javascript
微信小程序 弹框和模态框实现代码
Mar 10 Javascript
基于angular实现三级联动的生日插件
May 12 Javascript
jQuery Ajax向服务端传递数组参数值的实例代码
Sep 03 jQuery
vue 页面加载进度条组件实例
Feb 05 Javascript
vue中使用echarts制作圆环图的实例代码
Jul 27 Javascript
layer.confirm()右边按钮实现href的例子
Sep 27 Javascript
学习YUI.Ext第五日--做拖放Darg&Drop
Mar 10 #Javascript
学习YUI.Ext 第四天--对话框Dialog的使用
Mar 10 #Javascript
学习YUI.Ext 第三天
Mar 10 #Javascript
学习YUI.Ext 第二天
Mar 10 #Javascript
学习YUI.Ext基础第一天
Mar 10 #Javascript
JavaScript触发器详解
Mar 10 #Javascript
又一个图片自动缩小的JS代码
Mar 10 #Javascript
You might like
PHP显示今天、今月、上月、今年的起点/终点时间戳的代码
2011/05/25 PHP
php中将网址转换为超链接的函数
2011/09/02 PHP
Laravel5中contracts详解
2015/03/02 PHP
AJAX PHP无刷新form表单提交的简单实现(推荐)
2016/09/09 PHP
详解PHP归并排序的实现
2016/10/18 PHP
用js解决数字不能换行问题
2010/08/10 Javascript
angularJS提交表单(form)
2015/02/09 Javascript
Jquery幻灯片特效代码分享--打开页面随机选择切换方式(3)
2015/08/15 Javascript
基于JS判断iframe是否加载成功的方法(多种浏览器)
2016/05/13 Javascript
移动端横屏的JS代码(beta)
2016/05/16 Javascript
使用Web Uploader实现多文件上传
2016/06/08 Javascript
详解js中的apply与call的用法
2016/07/30 Javascript
jQuery ztree实现动态树形多选菜单
2016/08/12 Javascript
在bootstrap中实现轮播图实例代码
2017/06/11 Javascript
react-navigation之动态修改title的内容
2018/09/26 Javascript
微信小程序如何使用globalData的方法
2019/06/06 Javascript
Python学生信息管理系统修改版
2018/03/13 Python
python实现列表中由数值查到索引的方法
2018/06/27 Python
python微信好友数据分析详解
2018/11/19 Python
python opencv判断图像是否为空的实例
2019/01/26 Python
python实现贪吃蛇游戏
2020/03/21 Python
Python实现图片转字符画的代码实例
2019/02/22 Python
Python3.5 Pandas模块之Series用法实例分析
2019/04/23 Python
TensorFlow车牌识别完整版代码(含车牌数据集)
2019/08/05 Python
opencv3/python 鼠标响应操作详解
2019/12/11 Python
python 成功引入包但无法正常调用的解决
2020/03/09 Python
序列化Python对象的方法
2020/08/01 Python
基于CSS3的animation属性实现微信拍一拍动画效果
2020/06/22 HTML / CSS
企业统计员岗位职责
2013/12/13 职场文书
年度考核自我鉴定
2014/02/02 职场文书
大学秋游活动方案
2014/02/11 职场文书
早读课迟到检讨书
2014/09/25 职场文书
2014年幼师工作总结
2014/11/22 职场文书
2016春季田径运动会广播稿
2015/12/21 职场文书
查看nginx配置文件路径和资源文件路径的方法
2021/03/31 Servers
Html5生成验证码的示例代码
2021/05/10 Javascript