程式名稱:JTree(樹狀控件)
結合了 XML 的長處,讓你只需幾行程式碼,就能擁有像 Windows 資源管理器一樣的 TreeView。
我曾經寫過一個 TreeView,但外觀不夠美觀;這一版在外觀上做了大幅改進,相當漂亮,執行速度也很快。
JTree 在 onclick 時有兩個可用的值:
var myTree=new JTree("showTree","vogueType.XML");
myTree.setPicPath("img/")
myTree.onclick=function(){
//alert(myTree.selectNode.parentNode.attributes.getNamedItem("eXPlain").nodeValue);
//alert(myTree.clickItem.level)
setValueById("typeCaption",getNodeAtt(myTree.selectNode,"caption"));
setValueById("parentType",getNodeAtt(myTree.selectNode.parentNode,"caption"));
setValueById("PRI",getNodeAtt(myTree.selectNode,"PRI"));
setValueById("explain",getNodeAtt(myTree.selectNode,"explain"));
setValueById("level",myTree.clickItem.level);
}
myTree.CAPTIONATT="caption";
myTree.create();
其中 myTree.clickItem 相當於一個記錄型變數,它的原型如下:
function clickNode(){
this.obj =null;
this.caption =null;
this.level =null;
this.value =null;
}
var self =this; //相當於一個引用,指向自己(見 JTree.JS 的開頭部分)
self 實際上就像一個指標。整個程式碼其實很簡單,只要用心讀,就能把這個樹狀控件輕鬆整合進自己的專案裡。