EasyUI 之 Combotree 简单扩展

EasyUI 之 Combotree简单扩展

 

     前期在QQ群众遇到群友提问个问题,就是 comboTree 的节点图标怎么移除。官网上面并没有给出 不显示图标的属性和方法。(PS: comboTree 继承于combo 和 tree)。

官网示例:

  

先做之前,咱们使用 谷歌浏览器看看经过easyUI渲染后节点图标的html代码:

鼠标对着图标右击->查看元素,如下图:

  

原来 是 span 加了class样式生成的。这样我们直接在combotree 渲染后直接删除其节点下的 span 不就ok了吗?

代码如下:

$(".easyui-tree").find("span.tree-icon").remove();

将代码拷贝到谷歌控制台,回车,效果出来了。如下图:

我们发现,节点图标已经不存在并且dom中也没有 span. tree-icon 元素了。

展开折叠节点,发现出现图标了。如下图:

 

发现是这个样式控制的。

于是我就想到那么我就在节点展开后,设置tree-folder-open 样式的 background为none。

代码如下:

1 onExpand: function(node) {
2                     $(node.target).find("span.tree-folder-open").css("background", "none");
3 }

 

这样,就实现了移除 combotree 的节点图标。于是乎我就去和群友邀功。

功劳没邀成,反而问题也来了。 若是大批量的使用,这种方法不累死才怪。想象也是。这样岂不是给别人造成麻烦了。那么就改吧。既然combotree 没有这种方法。咱们就给他扩展个。

       下面咱们来介绍下我经常使用的扩展外壳。扩展的时候直接在里面填充逻辑代码就好了,这样让我们有更多的时间去专注本质问题而不是考虑怎么写扩展。

       代码如下:

 1 (function ($) {
 2 
 3     function buildCombotree(target) { // 初始化
 4        
 5     }
 6  
 7     // 程序主入口
 8     $.fn.dcombotree = function (options, param) { 
 9         if (typeof options == 'string') {
10             var method = $.fn.dcombotree.methods[options];
11             if (method) {
12                 return method(this, param);
13             } else {
14                 return this.combotree(options, param);
15             }
16         }
17         options = options || {};
18         return this.each(function () {
19             var state = $.data(this, 'dcombotree');
20             if (state) {
21                 $.extend(state.options, options);
22             } else {
23                 $.data(this, 'dcombotree', {
24                     options: $.extend({}, $.fn.dcombotree.defaults, $.fn.dcombotree.parseOptions(this), options)
25                 });
26             }
27             buildCombotree(this);
28         });
29     };
30 
31     $.fn.dcombotree.parseOptions = function (target) {
32         return $.extend({}, $.fn.combotree.parseOptions(target), {
33         });
34     };
35 
36     // 程序方法
37     $.fn.dcombotree.methods = {  
38 
39     };
40 
41     // 程序默认属性
42     $.fn.dcombotree.defaults = {
43       
44     };
45 })(jQuery);

此段代码在直接摘自于  http://www.jeasyui.com/extension/edatagrid.php 有兴趣可以下载下来研究。

 

扩展的外壳写好了。那么就专注功能实现吧。

我的思路还是上面提到的: 在combotree加载成功后直接删除 span. tree-icon 元素。在展开事件修改设置tree-folder-open 样式的 background为none。说干就干,下面贴出代码:

 1 (function ($) {
 2 
 3     function buildCombotree(target) { // 初始化
 4         var opts = $.data(target, 'dcombotree').options;  // 获取 dcombotree 的 options
 5         $(target).combotree($.extend({}, opts, {})); //  去渲染 combotree
 6         if (!opts.showIcon) {  // 是要移除 图标
 7             var tree = $(target).dcombotree('tree');  // 取得 combotree 的 tree。
 8             tree.tree({
 9                 onLoadSuccess: function(node, data) {  // tree 数据载入成功后执行所有节点图标删除。采用的是递归遍历节点然后删除
10                     removeIcon($(this).tree("getRoots"), tree);
11                 },
12                 onExpand: function(node) {  // tree父节点展开事件。
13                     $(node.target).find("span.tree-folder-open").css("background", "none");  //设置背景样式为 none
14                 }
15             });
16         }
17     }
18 
19     // 递归删除 图标样式
20     function removeIcon(node,tree) {
21         $.each(tree.tree("getChildren",node.target), function (index, value) {  //遍历当前界面的 子节点
22             $(value.target).find("span.tree-icon").remove(); // 移除图标span
23             if (value.children != undefined && value.children.length > 0)  // 是否需要继续递归删除
24                 removeIcon(value, tree);
25         });
26     }
27 
28     $.fn.dcombotree = function (options, param) {  // 程序主入口
29         if (typeof options == 'string') {
30             var method = $.fn.dcombotree.methods[options];
31             if (method) {
32                 return method(this, param);
33             } else {
34                 return this.combotree(options, param);
35             }
36         }
37         options = options || {};
38         return this.each(function () {
39             var state = $.data(this, 'dcombotree');
40             if (state) {
41                 $.extend(state.options, options);
42             } else {
43                 $.data(this, 'dcombotree', {
44                     options: $.extend({}, $.fn.dcombotree.defaults, $.fn.dcombotree.parseOptions(this), options)
45                 });
46             }
47             buildCombotree(this);
48         });
49     };
50     
51     $.fn.dcombotree.parseOptions = function (target) {
52         return $.extend({}, $.fn.combotree.parseOptions(target), {
53         });
54     };
55 
56     $.fn.dcombotree.methods = {   // 程序方法
57        
58     };
59 
60     $.fn.dcombotree.defaults = {
61         // 程序默认属性
62         showIcon: true  // 展示combotree 的下拉数图标。默认展示
63     };
64 })(jQuery);

界面引用:

 

PS:dcombotree 是咱们的插件名称,基于 easyUI combotree 实现的。不污染里面任何的代码。使用方便。Dcombotree 在我们设计外壳的时候写的。自习阅读dome中的代码。你自然会明白其中的道理。

 

实例预览:

在线演示Dome:http://zhanghs.nat123.net/dcombotree/

从上述代码可以看出,基本上是在 扩展外壳的基础上实现的。所以在此想大家建议: 在想扩展某个插件或者想自己写个扩展时候。请大家先自己动手实现,思路清洗后在去做扩展插件或者插件的开发。

 

PS:其实js插件开发不是想象中的那么难学。难学的是编程思想,而不是语言。

 

提供源码下载,下载后记得推荐哦。

 

posted @ 2015-07-31 17:22  软软的毛毛虫  阅读(1344)  评论(4)    收藏  举报