学习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 相关文章推荐
style、 currentStyle、 runtimeStyle区别分析
Aug 01 Javascript
菜鸟javascript基础资料整理3 正则
Dec 06 Javascript
鼠标事件延时切换插件
Mar 12 Javascript
js取滚动条的尺寸的函数代码
Nov 30 Javascript
怎么判断js脚本加载完成
Feb 28 Javascript
javascript弹出拖动窗口
Aug 11 Javascript
ES6新数据结构Map功能与用法示例
Mar 31 Javascript
Angular2的管道Pipe的使用方法
Nov 07 Javascript
详解bootstrap-fileinput文件上传控件的亲身实践
Mar 21 Javascript
javascript中的闭包概念与用法实践分析
Jul 26 Javascript
解决使用layui的时候form表单中的select等不能渲染的问题
Sep 18 Javascript
原生js实现表格循环滚动
Nov 24 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
建立文件交换功能的脚本(三)
2006/10/09 PHP
PHP递归调用的小技巧讲解
2013/02/19 PHP
coreseek 搜索英文的问题详解
2013/06/08 PHP
Symfony2使用第三方库Upload制作图片上传实例详解
2016/02/04 PHP
CodeIgniter生成静态页的方法
2016/05/17 PHP
PHP编程 SSO详细介绍及简单实例
2017/01/13 PHP
thinkphp5引入公共部分header、footer的方法详解
2018/09/14 PHP
js 键盘记录实现(兼容FireFox和IE)
2010/02/07 Javascript
Wordpress ThickBox 点击图片显示下一张图的修改方法
2010/12/11 Javascript
JavaScript去掉空格的方法集合
2010/12/28 Javascript
Textarea与懒惰渲染实现代码
2012/01/04 Javascript
jquery无缝向上滚动实现代码
2013/03/29 Javascript
基于jquery编写的横向自适应幻灯片切换特效的实例代码
2013/08/06 Javascript
JQuery表单验证插件EasyValidator用法分析
2014/11/15 Javascript
Javascript实现Web颜色值转换
2015/02/05 Javascript
基于jQuery实现音乐播放试听列表
2016/04/14 Javascript
Json解析的方法小结
2016/06/22 Javascript
Bootstrap选项卡学习笔记分享
2017/02/13 Javascript
jQuery animate()实现背景色渐变效果的处理方法【使用jQuery.color.js插件】
2017/03/15 Javascript
如何使用JS在HTML中自定义字符串格式化
2017/07/20 Javascript
vue路由拦截及页面跳转的设置方法
2018/05/24 Javascript
利用Psyco提升Python运行速度
2014/12/24 Python
Python selenium 三种等待方式解读
2016/09/15 Python
Python可变参数用法实例分析
2017/04/02 Python
Python中单例模式总结
2018/02/20 Python
对python生成业务报表的实例详解
2019/02/03 Python
python3使用matplotlib绘制散点图
2019/03/19 Python
python pip源配置,pip配置文件存放位置的方法
2019/07/12 Python
利用 Python ElementTree 生成 xml的实例
2020/03/06 Python
解释DataSet(ds) 和 ds as DataSet 的含义
2014/07/27 面试题
资深生产主管自我评价
2013/09/22 职场文书
技术经理的自我评价范文
2013/12/03 职场文书
村创先争优活动总结
2014/08/28 职场文书
2015年幼儿园元旦亲子活动方案
2014/12/09 职场文书
自主招生自荐信怎么写
2015/03/24 职场文书
html+css实现环绕倒影加载特效
2021/07/07 HTML / CSS