分享

DWZ中修改tree可以自定义节点的图标

 aaie_ 2018-04-30

由于DWZ中只能统一的改变图标,这个地方对它进行了修改可以给节点添加对应的icon。使用方法是:

  1. <li class="t_1" value="123" iconCls="home_icon"><a href="#">asdasd</a>  
  2.                                 <ul>  
  3.                                     <li class="t_1" iconCls="home_icon"><a href="#" target="navTab">asd</a></li>  
  4.                                     <li class="t_1"><a href="#" target="navTab">asdasd</a></li>  
  5.                                 </ul>  
  6.                             </li>  
  7.                             <li class="t_1" value="123" iconCls="home_icon"><a href="#">asdasd</a>  
  8.                                 <ul>  
  9.                                     <li class="t_1"><a href="#" target="navTab">asd</a></li>  
  10.                                     <li class="t_1"><a href="#" target="navTab">asdasd</a></li>  
  11.                                 </ul>  
  12.                             </li>  


对于LI添加iconCls即可,cls是对应的css,给个样例的css:

.tree .home_icon {background:url(../images/home.png) no-repeat}

图标是22px*22px的,但是里面真正的占用的是16px*16px居中的。

 

下面是修改好的dwz.tree.js

修改的地方坐了备注的

 

[javascript] view plain copy
print?
  1. /** 
  2.  * @author Roger Wu 
  3.  * @version 1.0 
  4.  * added extend property oncheck 
  5.  */  
  6.  (function($){  
  7.     $.extend($.fn, {  
  8.         jTree:function(options) {  
  9.             var op = $.extend({checkFn:null, selected:"selected", exp:"expandable", coll:"collapsable", firstExp:"first_expandable", firstColl:"first_collapsable", lastExp:"last_expandable", lastColl:"last_collapsable", folderExp:"folder_expandable", folderColl:"folder_collapsable", endExp:"end_expandable", endColl:"end_collapsable",file:"file",ck:"checked", unck:"unchecked"}, options);  
  10.             return this.each(function(){  
  11.                 var $this = $(this);  
  12.                 var cnum = $this.children().length;  
  13.                 $(">li", $this).each(function(){  
  14.                     var $li = $(this);  
  15.                       
  16.                     var first = $li.prev()[0]?false:true;  
  17.                     var last = $li.next()[0]?false:true;   
  18.                     $li.genTree({  
  19.                         selfIcon:(typeof($li.attr("iconCls"))=="undefined")?'':$li.attr('iconCls'),//自加,用于判断节点是否添加了iconCls  
  20.                         icon:$this.hasClass("treeFolder"),  
  21.                         ckbox:$this.hasClass("treeCheck"),  
  22.                         options: op,  
  23.                         level: 0,  
  24.                         exp:(cnum>1?(first?op.firstExp:(last?op.lastExp:op.exp)):op.endExp),  
  25.                         coll:(cnum>1?(first?op.firstColl:(last?op.lastColl:op.coll)):op.endColl),  
  26.                         showSub:(!$this.hasClass("collapse") && ($this.hasClass("expand") || (cnum>1?(first?true:false):true))),  
  27.                         isLast:(cnum>1?(last?true:false):true)  
  28.                     });  
  29.                 });  
  30.                 setTimeout(function(){  
  31.                     if($this.hasClass("treeCheck")){  
  32.                         var checkFn = eval($this.attr("oncheck"));  
  33.                         if(checkFn && $.isFunction(checkFn)) {  
  34.                             $("div.ckbox", $this).each(function(){  
  35.                                 var ckbox = $(this);  
  36.                                 ckbox.click(function(){  
  37.                                     var checked = $(ckbox).hasClass("checked");  
  38.                                     var items = [];  
  39.                                     if(checked){  
  40.                                         var tnode = $(ckbox).parent().parent();  
  41.                                         var boxes = $("input", tnode);  
  42.                                         if(boxes.size() > 1) {  
  43.                                             $(boxes).each(function(){  
  44.                                                 items[items.length] = {name:$(this).attr("name"), value:$(this).val(), text:$(this).attr("text")};  
  45.                                             });  
  46.                                         } else {  
  47.                                             items = {name:boxes.attr("name"), value:boxes.val(), text:boxes.attr("text")};  
  48.                                         }         
  49.                                     }                                 
  50.                                     checkFn({checked:checked, items:items});                                                          
  51.                                 });  
  52.                             });  
  53.                         }  
  54.                     }  
  55.                     $("a", $this).click(function(event){  
  56.                         $("div." + op.selected, $this).removeClass(op.selected);  
  57.                         var parent = $(this).parent().addClass(op.selected);  
  58.                         $(".ckbox",parent).trigger("click");  
  59.                         event.stopPropagation();  
  60.                         $(document).trigger("click");  
  61.                         if (!$(this).attr("target")) return false;  
  62.                     });  
  63.                 },1);  
  64.             });  
  65.         },  
  66.         subTree:function(op, level) {  
  67.             return this.each(function(){  
  68.                 $(">li"this).each(function(){  
  69.                     var $this = $(this);  
  70.                       
  71.                     var isLast = ($this.next()[0]?false:true);  
  72.                     $this.genTree({  
  73.                         selfIcon:(typeof($this.attr("iconCls"))=="undefined")?'file':$this.attr('iconCls'),//自加属性,用于具有父亲节点的节点  
  74.                         icon:op.icon,  
  75.                         ckbox:op.ckbox,  
  76.                         exp:isLast?op.options.lastExp:op.options.exp,  
  77.                         coll:isLast?op.options.lastColl:op.options.coll,  
  78.                         options:op.options,  
  79.                         level:level,  
  80.                         space:isLast?null:op.space,  
  81.                         showSub:op.showSub,  
  82.                         isLast:isLast  
  83.                     });  
  84.                       
  85.                 });  
  86.             });  
  87.         },  
  88.         genTree:function(options) {  
  89.             var op = $.extend({selfIcon:options.selfIcon,icon:options.icon,ckbox:options.ckbox,exp:"", coll:"", showSub:false, level:0, options:null, isLast:false}, options);  
  90.             return this.each(function(){  
  91.                 var node = $(this);  
  92.                 var tree = $(">ul", node);  
  93.                 var parent = node.parent().prev();  
  94.                 var checked = 'unchecked';  
  95.                 if(op.ckbox) {  
  96.                     if($(">.checked",parent).size() > 0) checked = 'checked';  
  97.                 }  
  98.                 if (tree.size()>0) {  
  99.                     node.children(":first").wrap("<div></div>");  
  100.                     // 设置class,设置的是具有子节点的node。  
  101.                     var cls = '';  
  102.                     if(op.icon) {  
  103.                         if(op.showSub) {  
  104.                             if(op.selfIcon != '') {cls = op.selfIcon;}  
  105.                             else {cls = op.options.folderColl;}  
  106.                         } else {  
  107.                             if(op.selfIcon != '') {cls = op.selfIcon;}  
  108.                             else {cls = op.options.folderExp}  
  109.                         }  
  110.                     }  
  111.                     $(">div", node).prepend("<div class='" + (op.showSub ? op.coll : op.exp) + "'></div>"+(op.ckbox ?"<div class='ckbox " + checked + "'></div>":"")+(op.icon?"<div class='"+cls+"'></div>":""));  
  112.                     op.showSub ? tree.show() : tree.hide();  
  113.                     $(">div>div:first,>div>a", node).click(function(){  
  114.                         var $fnode = $(">li:first",tree);  
  115.                         if($fnode.children(":first").isTag('a')) tree.subTree(op, op.level + 1);  
  116.                         var $this = $(this);  
  117.                         var isA = $this.isTag('a');  
  118.                         var $this = isA?$(">div>div", node).eq(op.level):$this;  
  119.                         if (!isA || tree.is(":hidden")) {  
  120.                             $this.toggleClass(op.exp).toggleClass(op.coll);  
  121.                             if (op.icon) {  
  122.                                 $(">div>div:last", node).toggleClass(op.options.folderExp).toggleClass(op.options.folderColl);  
  123.                             }  
  124.                         }  
  125.                         (tree.is(":hidden"))?tree.slideDown("fast"):(isA?"":tree.slideUp("fast"));  
  126.                         return false;  
  127.                     });  
  128.                     addSpace(op.level, node);  
  129.                     if(op.showSub) tree.subTree(op, op.level + 1);  
  130.                 } else {  
  131.                     // 具有父亲节点的节点的属性,在subTree中已经处理过,这个地方队没有父亲节点的节点做一个补充处理  
  132.                     var cls = '';  
  133.                     if(op.selfIcon == '') {  
  134.                         cls = 'file';  
  135.                     } else {  
  136.                         cls = op.selfIcon;  
  137.                     }  
  138.                     node.children().wrap("<div></div>");              
  139.                     $(">div", node).prepend("<div class='node'></div>"+(op.ckbox?"<div class='ckbox "+checked+"'></div>":"")+(op.icon?"<div class='"+cls+"'></div>":""));  
  140.                     addSpace(op.level, node);  
  141.                     if(op.isLast)$(node).addClass("last");  
  142.                 }  
  143.                 if (op.ckbox) node._check(op);  
  144.                 $(">div",node).mouseover(function(){  
  145.                     $(this).addClass("hover");  
  146.                 }).mouseout(function(){  
  147.                     $(this).removeClass("hover");  
  148.                 });  
  149.                 if($.browser.msie)  
  150.                     $(">div",node).click(function(){  
  151.                         $("a"this).trigger("click");  
  152.                         return false;  
  153.                     });  
  154.             });  
  155.             function addSpace(level,node) {  
  156.                 if (level > 0) {                   
  157.                     var parent = node.parent().parent();  
  158.                     var space = !parent.next()[0]?"indent":"line";  
  159.                     var plist = "<div class='" + space + "'></div>";  
  160.                     if (level > 1) {  
  161.                         var next = $(">div>div", parent).filter(":first");  
  162.                         var prev = "";  
  163.                         while(level > 1){  
  164.                             prev = prev + "<div class='" + next.attr("class") + "'></div>";  
  165.                             next = next.next();  
  166.                             level--;  
  167.                         }  
  168.                         plist = prev + plist;  
  169.                     }  
  170.                     $(">div", node).prepend(plist);  
  171.                 }  
  172.             }  
  173.         },  
  174.         _check:function(op) {  
  175.             var node = $(this);  
  176.             var ckbox = $(">div>.ckbox", node);  
  177.             var $input = node.find("a");  
  178.             var tname = $input.attr("tname"), tvalue = $input.attr("tvalue");  
  179.             var attrs = "text='"+$input.text()+"' ";  
  180.             if (tname) attrs += "name='"+tname+"' ";  
  181.             if (tvalue) attrs += "value='"+tvalue+"' ";  
  182.               
  183.             ckbox.append("<input type='checkbox' style='display:none;' " + attrs + "/>").click(function(){  
  184.                 var cked = ckbox.hasClass("checked");  
  185.                 var aClass = cked?"unchecked":"checked";  
  186.                 var rClass = cked?"checked":"unchecked";  
  187.                 ckbox.removeClass(rClass).removeClass(!cked?"indeterminate":"").addClass(aClass);  
  188.                 $("input", ckbox).attr("checked", !cked);  
  189.                 $(">ul", node).find("li").each(function(){  
  190.                     var box = $("div.ckbox"this);  
  191.                     box.removeClass(rClass).removeClass(!cked?"indeterminate":"").addClass(aClass)  
  192.                        .find("input").attr("checked", !cked);  
  193.                 });  
  194.                 $(node)._checkParent();  
  195.                 return false;  
  196.             });  
  197.             var cAttr = $input.attr("checked") || false;  
  198.             if (cAttr) {  
  199.                 ckbox.find("input").attr("checked"true);  
  200.                 ckbox.removeClass("unchecked").addClass("checked");  
  201.                 $(node)._checkParent();  
  202.             }  
  203.         },  
  204.         _checkParent:function(){  
  205.             if($(this).parent().hasClass("tree")) return;  
  206.             var parent = $(this).parent().parent();  
  207.             var stree = $(">ul", parent);  
  208.             var ckbox = stree.find(">li>a").size()+stree.find("div.ckbox").size();  
  209.             var ckboxed = stree.find("div.checked").size();  
  210.             var aClass = (ckboxed==ckbox?"checked":(ckboxed!=0?"indeterminate":"unchecked"));  
  211.             var rClass = (ckboxed==ckbox?"indeterminate":(ckboxed!=0?"checked":"indeterminate"));  
  212.             $(">div>.ckbox", parent).removeClass("unchecked").removeClass("checked").removeClass(rClass).addClass(aClass);  
  213.             parent._checkParent();  
  214.         }  
  215.     });  
  216. })(jQuery);  


 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约