Ext CheckTreeNode Example

实例:
Ext CheckTreeNode Example

实例是模仿dhtmlxTree的
在render中主要添加了这一行'<img src="', a.isCheck ? "/images/tc/iconCheckAll.gif" : "/images/tc/iconUnCheckAll.gif",'" class="x-tree-ec-icon">',
再获取这个节点 this.ckNode = cs[2];
在initEvents中添加该节点的事件:
E.on(this.ckNode, "click", this.ckClick, this, true);
很简单!

Java代码 复制代码
  1. Ext.tree.TreeNodeUI.prototype.initEvents = function(){   
  2.         this.node.on("move"this.onMove, this);   
  3.         var E = Ext.EventManager;   
  4.         var a = this.anchor;   
  5.         var el = Ext.fly(a);   
  6.         if(Ext.isOpera){ // opera render bug ignores the CSS   
  7.             el.setStyle("text-decoration""none");   
  8.         }   
  9.         el.on("click"this.onClick, this);   
  10.         el.on("dblclick"this.onDblClick, this);   
  11.         el.on("contextmenu"this.onContextMenu, this);   
  12.         var icon = Ext.fly(this.iconNode);   
  13.         icon.on("click"this.onClick, this);   
  14.         icon.on("dblclick"this.onDblClick, this);   
  15.         icon.on("contextmenu"this.onContextMenu, this);   
  16.         E.on(this.ecNode, "click"this.ecClick, thistrue);   
  17.         E.on(this.ckNode, "click"this.ckClick, thistrue);   
  18.         if(this.node.disabled){   
  19.             this.addClass("x-tree-node-disabled");   
  20.         }   
  21.         if(this.node.hidden){   
  22.             this.addClass("x-tree-node-disabled");   
  23.         }   
  24.         var ot = this.node.getOwnerTree();   
  25.         var dd = ot.enableDD || ot.enableDrag || ot.enableDrop;   
  26.         if(dd && (!this.node.isRoot || ot.rootVisible)){   
  27.             Ext.dd.Registry.register(this.elNode, {   
  28.                 node: this.node,   
  29.                 handles: [this.iconNode, this.textNode],   
  30.                 isHandle: false  
  31.             });   
  32.         }   
  33.     };   
  34.        
  35.     Ext.tree.TreeNodeUI.prototype.ckClick = function(e){   
  36.         if(!this.animating && this.node.hasChildNodes()){   
  37.             this.node.expand();   
  38.         }   
  39.         this.updateCheckIcon();   
  40.     };   
  41.        
  42.     Ext.tree.TreeNodeUI.prototype.updateCheckIcon = function(){   
  43.         if(this.node.attributes.isCheck)   
  44.         {         
  45.             this.ckNode.src="/images/tc/iconUnCheckAll.gif";   
  46.             this.node.attributes.isCheck=false;   
  47.             this.updateFCUNCheckIcon(this.node.parentNode);//只能放在上一句的后面   
  48.             this.updateCCUNCheckIcon(this.node.childNodes);   
  49.         }   
  50.         else  
  51.         {   
  52.             this.ckNode.src="/images/tc/iconCheckAll.gif";   
  53.             this.node.attributes.isCheck=true;   
  54.             this.updateFCheckIcon(this.node.parentNode);//只能放在上一句的后面   
  55.             this.updateCCheckIcon(this.node.childNodes);//只能放在上一句的后面   
  56.         }   
  57.     };   
  58.        
  59.     //更新父接点为选种   
  60.     Ext.tree.TreeNodeUI.prototype.updateFCheckIcon = function(pn){   
  61.         if(pn!=null)   
  62.         {   
  63.             pn.attributes.isCheck=true;   
  64.             pn.ui.ckNode.src="/images/tc/iconCheckAll.gif";   
  65.             this.updateFCheckIcon(pn.parentNode);   
  66.         }   
  67.     }   
  68.     //更新子节点为选种   
  69.     Ext.tree.TreeNodeUI.prototype.updateCCheckIcon = function(cn){   
  70.         if(cn!=null)   
  71.         {   
  72.            for(var i=0;i<cn.length;i++)   
  73.             {   
  74.                 cn[i].attributes.isCheck=true;   
  75.                 cn[i].ui.ckNode.src="/images/tc/iconCheckAll.gif";   
  76.                 if(cn[i].hasChildNodes()){   
  77.                     cn[i].expand();   
  78.                     var temp=cn[i].childNodes;   
  79.                     this.updateCCheckIcon(cn[i].childNodes);   
  80.                 }   
  81.             }    
  82.         }   
  83.     }   
  84.        
  85.     //更新父接点为不�?�?   
  86.     Ext.tree.TreeNodeUI.prototype.updateFCUNCheckIcon = function(n){   
  87.         //如果有其它的兄弟接点未勾选掉。不更改父节�?   
  88.         var flag=true;   
  89.         if(n!=null)//如果是父节点,此处为空�?�?��要在这进行判�?   
  90.         {   
  91.             var nodes = n.childNodes;   
  92.             for(var i=0;i<nodes.length;i++)   
  93.             {   
  94.                 if(nodes[i].attributes.isCheck)   
  95.                 {   
  96.                     flag=false  
  97.                     break;   
  98.                 }   
  99.                    
  100.             }   
  101.             if(n!=null && flag)   
  102.             {   
  103.                 n.attributes.isCheck=false;   
  104.                 n.ui.ckNode.src="/images/tc/iconUnCheckAll.gif";   
  105.                 this.updateFCUNCheckIcon(n.parentNode);   
  106.             }   
  107.         }   
  108.     }   
  109.        
  110.     //更新子接点为不�?�?   
  111.     Ext.tree.TreeNodeUI.prototype.updateCCUNCheckIcon = function(cn){   
  112.         if(cn!=null)   
  113.         {   
  114.            for(var i=0;i<cn.length;i++)   
  115.             {   
  116.                 cn[i].attributes.isCheck=false;   
  117.                 cn[i].ui.ckNode.src="/images/tc/iconUnCheckAll.gif";   
  118.                 if(cn[i].hasChildNodes()){   
  119.                     cn[i].expand();   
  120.                     var temp=cn[i].childNodes;   
  121.                     this.updateCCUNCheckIcon(cn[i].childNodes);   
  122.                 }   
  123.             }    
  124.         }   
  125.     }   
  126.        
  127.        
  128. Ext.tree.TreeNodeUI.prototype.render=function(bulkRender){   
  129.         var n = this.node;   
  130.         var targetNode = n.parentNode ?    
  131.               n.parentNode.ui.getContainer() : n.ownerTree.container.dom;   
  132.         if(!this.rendered){   
  133.             this.rendered = true;   
  134.             var a = n.attributes;   
  135.            
  136.             // add some indent caching, this helps performance when rendering a large tree   
  137.             this.indentMarkup = "";   
  138.             if(n.parentNode){   
  139.                 this.indentMarkup = n.parentNode.ui.getChildIndent();   
  140.             }   
  141.                
  142.             var buf = ['<li class="x-tree-node"><div class="x-tree-node-el ', n.attributes.cls,'">',   
  143.                 '<span class="x-tree-node-indent">',this.indentMarkup,"</span>",   
  144.                 '<img src="'this.emptyIcon, '" class="x-tree-ec-icon">',   
  145.                 '<img src="', a.isCheck ? "/images/tc/iconCheckAll.gif" : "/images/tc/iconUnCheckAll.gif",'" class="x-tree-ec-icon">',   
  146.                 '<img src="', a.icon || this.emptyIcon, '" class="x-tree-node-icon',(a.icon ? " x-tree-node-inline-icon" : ""),'" unselectable="on">',   
  147.                 '<a hidefocus="on" href="',a.href ? a.href : "#",'" tabIndex="1" ',   
  148.                  a.hrefTarget ? ' target="'+a.hrefTarget+'"' : ""'><span unselectable="on">',n.text,"</span></a></div>",   
  149.                 '<ul class="x-tree-node-ct" style="display:none;"></ul>',   
  150.                 "</li>"];   
  151.                    
  152.             if(bulkRender !== true && n.nextSibling && n.nextSibling.ui.getEl()){   
  153.                 this.wrap = Ext.DomHelper.insertHtml("beforeBegin",   
  154.                                     n.nextSibling.ui.getEl(), buf.join(""));   
  155.             }else{   
  156.                 this.wrap = Ext.DomHelper.insertHtml("beforeEnd", targetNode, buf.join(""));   
  157.             }   
  158.             this.elNode = this.wrap.childNodes[0];   
  159.             this.ctNode = this.wrap.childNodes[1];   
  160.             var cs = this.elNode.childNodes;   
  161.             this.indentNode = cs[0];   
  162.             this.ecNode = cs[1];   
  163.             this.ckNode = cs[2];   
  164.             this.iconNode = cs[3];   
  165.             this.anchor = cs[4];   
  166.             this.textNode = cs[4].firstChild;   
  167.             if(a.qtip){   
  168.                if(this.textNode.setAttributeNS){   
  169.                    this.textNode.setAttributeNS("ext""qtip", a.qtip);   
  170.                    if(a.qtipTitle){   
  171.                        this.textNode.setAttributeNS("ext""qtitle", a.qtipTitle);   
  172.                    }   
  173.                }else{   
  174.                    this.textNode.setAttribute("ext:qtip", a.qtip);   
  175.                    if(a.qtipTitle){   
  176.                        this.textNode.setAttribute("ext:qtitle", a.qtipTitle);   
  177.                    }   
  178.                }    
  179.             }   
  180.             this.initEvents();   
  181.             //this.renderIndent(); cached above now instead call updateExpandIcon   
  182.             this.updateExpandIcon();   
  183.         }else{   
  184.             if(bulkRender === true) {   
  185.                 targetNode.appendChild(this.wrap);   
  186.             }   
  187.         }   
  188.     };   
  189. //===============================================================================================================   
  190.   
  191. Ext.onReady(function(){   
  192.     // shorthand   
  193.     <!-- 删除等待进度条�?-->   
  194.     Ext.get("loading").remove();   
  195.     var Tree = Ext.tree;   
  196.     var dh = Ext.DomHelper;   
  197.        
  198.     var tree = new Tree.TreePanel('tree-div', {   
  199.         animate:true,    
  200.         loader: new Tree.TreeLoader({dataUrl:'/tree/getTreeJson'}),   
  201.         containerScroll: true  
  202.         //uiProvider: MyNodeUI   
  203.     });   
  204.     tree.on('contextmenu',contextmenu,tree);   
  205.     //tree.on('expand',insertCheckbox,tree);   
  206.   
  207.     // set the root node   
  208.     var root = new Tree.AsyncTreeNode({   
  209.         text: 'Topic',   
  210.         draggable:false,   
  211.         isCheck:true,   
  212.         id:'1'  
  213.     });   
  214.     tree.setRootNode(root);   
  215.     //render the tree   
  216.     tree.render();   
  217.     var editor = new Ext.tree.TreeEditor(tree, {allowBlank: false});   
  218.     editor.setSize(5,5);   
  219.     //root.expand(true,false);   
  220.     //容许树可以编�?   
  221.     //var ge = new Ext.tree.TreeEditor(tree, {allowBlank:false,blankText:'A name is required',selectOnFocus:true});   
  222.     //创建右键菜单   
  223.     function contextmenu(node,e){   
  224.             e.stopEvent();   
  225.             //var temp=ds.getAt(node.id);   
  226.            // alert(temp.data.email);   
  227.             var menu = new Ext.menu.Menu({   
  228.             id: 'mainMenu',   
  229.             items: [   
  230.                 new Ext.menu.Item({   
  231.                     text: '添加'  
  232.                 }),   
  233.                 new Ext.menu.Item({   
  234.                     text: '删除'  
  235.                 }),   
  236.                 new Ext.menu.Item({   
  237.                     text: '更新'  
  238.                 }),   
  239.                 new Ext.menu.Item({   
  240.                     text: '刷新'  
  241.                 })   
  242.             ]   
  243.         });   
  244.         var coords = e.getXY();   
  245.         menu.showAt([coords[0], coords[1]]);   
  246.     };   
  247. });   
  248.   
  249.   
  250. //treeLoader.on('beforeload', function(loader,node,callback){   
  251. //  if(node.attributes.tlevel != 3){   
  252. //      loader.dataUrl ='../getChildTreeNodeJsonObject';   
  253. //          }else{   
  254. //              return false;                      
  255. //          }   
  256. //      });  

你可能感兴趣的:(UI,css,prototype,Opera,ext)