學(xué)習(xí)YUI.Ext 第六天--關(guān)于樹(shù)TreePanel(Part 1)
學(xué)習(xí)YUI.Ext 第五天--關(guān)于樹(shù)TreePanel(Part 1)
效果演示:http://www.ajaxjs.com/yuicn/demos/order_tree.asp
樹(shù)組件是YUI.Ext 0.40 新增的組件。雖然YUI已經(jīng)自帶有TREE VIEW的組件,但JACK還是決定重新開(kāi)發(fā)。具體原因在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/ (原文)
一、加載一個(gè)同步Tree:
var TreeTest = function(){
var Tree = YAHOO.ext.tree;// 快捷方式
return {
init : function(){
var tree = new Tree.TreePanel(’tree_div’, {//需要一個(gè)tree_div的holder
animate:true, //是否動(dòng)畫(huà)
loader: new Tree.TreeLoader({dataUrl:’get_nodes.asp’}), //調(diào)用一個(gè)JSON
enableDD:false,// 是否支持拖放
containerScroll: true
});
// 設(shè)置根節(jié)點(diǎn)
var root = new Tree.AsyncTreeNode({
text: ’Frank的作品’, //根節(jié)點(diǎn)文字
draggable:false, //根節(jié)點(diǎn)是否可拖放
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);
通過(guò)XHR調(diào)用這個(gè)get_nodes.asp文件,假設(shè)服務(wù)器返回這樣一個(gè)JSON(有關(guān)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;
解釋?zhuān)?br />“text”-->顯示的文本
"id"-->id值
“leaf”-->Boolean值,如果“葉子”是真的話,則不能包含子節(jié)點(diǎn)Children nodes
"cls"-->選用的樣式,通常在這里選定圖標(biāo)
”href“-->指定的url,還有一個(gè)”hrefTarget“的屬性
另外,除了以上的屬性,您還可以在JSON加入任何的屬性,作為節(jié)點(diǎn)的屬性,見(jiàn)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會(huì)繼續(xù)解釋這個(gè)問(wèn)題。
FQA常見(jiàn)問(wèn)題:
1.Tree支持XML數(shù)據(jù)交換嗎?
A:暫不支持,據(jù)FOURM上的話,以后會(huì)提供支持,見(jiàn):
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.我想用單擊代替雙擊展開(kāi)子節(jié)點(diǎn),可以嗎?
A:可以,見(jiàn):
tree.on(’click’, function(node){
if(!node.isLeaf()){
node.toggle();
}
});
3.事件處理的幾種情形:
A: a.當(dāng)加入某個(gè)節(jié)點(diǎn)時(shí),為其增加事件
tree.on(’append’, function(tree, node){
if(node.id == ’foo’){
// 這里加入你的事件(如click)和(addListener())
}
});b.針對(duì)某個(gè)節(jié)點(diǎn)的單擊事件
tree.on(’click’, function(node){
if(node.id == ’foo’){
// do something
}
});c.針對(duì)某個(gè)區(qū)域(集合)的事件
// 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中的自定義字段(或稱(chēng)作參數(shù) parameters)
A:JSON對(duì)象已經(jīng)被構(gòu)建函數(shù) construction傳遞到TreeNode中,作為node.attributes 出現(xiàn),所以調(diào)用屬性node.attributes 便可獲取。詳見(jiàn):http://www.yui-ext.com/forum/viewtopic.php?t=2253
tree.on(’click’, function(node){
if(!node.isLeaf()){
node.toggle();
}
});
相關(guān)文章
Ext面向?qū)ο箝_(kāi)發(fā)實(shí)踐代碼
這是自己在學(xué)習(xí)Ext過(guò)程中一個(gè)寫(xiě)的一個(gè)示例程序,僅為練習(xí),功能并不全,現(xiàn)將其記錄在我的博客中,希望可以和學(xué)習(xí)Ext的朋友一起分享2008-11-11
Javascript YUI 讀碼日記之 YAHOO.util.Dom - Part.3
在 YAHOO.util.Dom 中能發(fā)現(xiàn)很多有趣的東西。下面先說(shuō)下 toCamel 的函數(shù),感謝 小馬 幫助我理解了這個(gè)函數(shù)。toCamel 把指定名稱(chēng)替換為駝峰寫(xiě)法,比如把 border-width 替換為 borderWidth 。2008-03-03
如何簡(jiǎn)單地用YUI做JavaScript動(dòng)畫(huà)
如何簡(jiǎn)單地用YUI做JavaScript動(dòng)畫(huà)...2007-03-03
學(xué)習(xí)YUI.Ext第七日-View&JSONView Part Two-一個(gè)畫(huà)室網(wǎng)站的案例
這篇文章主要介紹了學(xué)習(xí)YUI.Ext第七日-View&JSONView Part Two-一個(gè)畫(huà)室網(wǎng)站的案例2007-03-03
Ext面向?qū)ο箝_(kāi)發(fā)實(shí)踐(續(xù))
我的上一篇文章《Ext面向?qū)ο箝_(kāi)發(fā)實(shí)踐》中簡(jiǎn)述了如何編寫(xiě)一個(gè)面向?qū)ο蟮臄?shù)據(jù)維護(hù)小程序,但這一些都是基于一個(gè)客戶端數(shù)據(jù),即用戶一旦刷新頁(yè)面,所有的操作都將丟失,現(xiàn)在我們就接著上一篇文章來(lái)繼續(xù)講一下如何對(duì)數(shù)據(jù)表進(jìn)行增、刪、改、查操作。2008-11-11
Javascript YUI 讀碼日記之 YAHOO.util.Dom - Part.2 0
繼續(xù)在 YAHOO.util.Dom 中徘徊。由于 YAHOO.util.Dom 多次調(diào)用 batch 方法,所以先看看這個(gè)函數(shù)是怎么寫(xiě)的。有關(guān) batch 的用法,可以參見(jiàn)這里,相關(guān)的代碼如下2008-03-03
對(duì)YUI擴(kuò)展的Gird組件 Part-2
對(duì)YUI擴(kuò)展的Gird組件 Part-2...2007-03-03
學(xué)習(xí)YUI.Ext 第六天--關(guān)于樹(shù)TreePanel(Part 2異步獲取節(jié)點(diǎn))
學(xué)習(xí)YUI.Ext 第六天--關(guān)于樹(shù)TreePanel(Part 2異步獲取節(jié)點(diǎn))...2007-03-03
yui3的AOP(面向切面編程)和OOP(面向?qū)ο缶幊?
這篇文章主要介紹了yui3的AOP(面向切面編程)和OOP(面向?qū)ο缶幊?,需要的朋友可以參考下2015-05-05

