基于treeview组件的通用节点树封装
2018-01-19 14:01
274 查看
基于treeview组件的通用节点树封装
最近一直在研究实现菜单树,表格树之类的tree组件,发现treeview使用起来简单,工具提供的也足够丰富,于是基于它再封装一版通用树插件,效果如下:附上代码,仅供参考:
function TreeProto(selector) { this.selector = selector; this.content = $(selector); this.data= null;// 数据源 this.backColor="#F5F5F5"; //背景颜色 this.borderColor="#DBDBDB"; //边框颜色 this.color="#666666"; //全局字体颜色 //this.checkedIcon="glyphicon glyphicon-ok"; //选中时候的复选框图标 this.emptyIcon=""; //设置列表树中没有子节点的节点的图标 this.highlightSelected= false; //是否高亮选中 this.onhoverColor="#E5E5E5"; //设置用户 鼠标滑过 时 树节点 背景颜色。 this.multiSelect= false; //多选 this.nodeIcon=""; //设置所有列表树节点上的默认图标 this.showCheckbox= true; //是否显示复选框 this.emptyIcon= ''; //没有子节点的节点图标 this.onNodeChecked=null; this.onNodeUnchecked=null; this.showTags=true; //是否在每个节点右边显示tags标签。tags值必须在每个列表树的data结构中给出 } //加载树 TreeProto.prototype.initTree = function(url, param,sesxhopt,treeviewoption) { //绑定选中方法,选中子节点同时选中父节点,选中父节点同时选中所有子节点 var currentObj = this; var nodeCheckedSilent = false; function nodeChecked (event, node){ if(nodeCheckedSilent){ return; } nodeCheckedSilent = true; currentObj.checkAllParent(node,currentObj.selector); currentObj.checkAllSon(node,currentObj.selector); nodeCheckedSilent = false; } //绑定取消选中方法,取消所有子节点同时取消父节点,取消父节点同时取消所有子节点 var nodeUncheckedSilent = false; function nodeUnchecked (event, node){ if(nodeUncheckedSilent) return; nodeUncheckedSilent = true; currentObj.uncheckAllParent(node,currentObj.selector); currentObj.uncheckAllSon(node,currentObj.selector); nodeUncheckedSilent = false; } this.onNodeChecked = nodeChecked; this.onNodeUnchecked=nodeUnchecked; $.extend(sesxhopt,treeviewoption); $.extend(this,sesxhopt); var opt = { data: this.data,// 数据源 backColor:this.backColor, //背景颜色 borderColor:this.borderColor, //边框颜色 color:this.color, //全局字体颜色 //checkedIcon:"glyphicon glyphicon-ok", //选中时候的复选框图标 emptyIcon:this.emptyIcon, //设置列表树中没有子节点的节点的图标 highlightSelected: this.highlightSelected, //是否高亮选中 onhoverColor:this.onhoverColor, //设置用户 鼠标滑过 时 树节点 背景颜色。 multiSelect: this.multiSelect, //多选 nodeIcon:this.nodeIcon, //设置所有列表树节点上的默认图标 showCheckbox: this.showCheckbox, //是否显示复选框 emptyIcon: this.emptyIcon, //没有子节点的节点图标 onNodeChecked:this.onNodeChecked , onNodeUnchecked:this.onNodeUnchecked, showTags:this.showTags //是否在每个节点右边显示tags标签。tags值必须在每个列表树的data结构中给出 } if(typeof url === "string"){ sesxhAjax.AjaxToDo(url,param,function(d){ opt.data = d; this.data = d; currentObj.content.treeview(opt); }); }else{ opt.data = url; this.data = url; currentObj.content.treeview(opt); } }, ////选中全部父节点 TreeProto.prototype.checkAllParent = function(node, selector) { this.content.treeview('checkNode', node.nodeId, { silent : true }); var parentNode = this.content.treeview('getParent', node.nodeId); if (!("nodeId" in parentNode)) { return; } else { this.checkAllParent(parentNode, selector); } }, //取消全部父节点 TreeProto.prototype.uncheckAllParent = function(node, selector) { this.content.treeview('uncheckNode', node.nodeId, { silent : true }); var siblings = this.content.treeview('getSiblings', node.nodeId); var parentNode = this.content.treeview('getParent', node.nodeId); if (!("nodeId" in parentNode)) { return; } c42a var isAllUnchecked = true; //是否全部没选中 for ( var i in siblings) { if (siblings[i].state.checked) { isAllUnchecked = false; break; } } if (isAllUnchecked) { this.uncheckAllParent(parentNode, selector); } }, //级联选中所有子节点 TreeProto.prototype.checkAllSon = function(node, selector) { this.content.treeview('checkNode', node.nodeId, { silent : true }); if (node.nodes != null && node.nodes.length > 0) { for ( var i in node.nodes) { this.checkAllSon(node.nodes[i], selector); } } }, //级联取消所有子节点 TreeProto.prototype.uncheckAllSon = function(node, selector) { this.content.treeview('uncheckNode', node.nodeId, { silent : true }); if (node.nodes != null && node.nodes.length > 0) { for ( var i in node.nodes) { this.uncheckAllSon(node.nodes[i], selector); } } }, //绑定全选方法 TreeProto.prototype.selectAll = function(selector) { this.content.treeview('checkAll', { silent : true }); }, //绑定重置方法 TreeProto.prototype.resetAll = function(selector) { this.content.treeview('uncheckAll', { silent : true }); }, //绑定选中 TreeProto.prototype.checkNode = function(selector) { this.content.treeview('checkNode', [ nodeId, { silent: true } ]); }, //添加操作节点 TreeProto.prototype.bindRowOperation = function(data, json) { if(!data || !json) return ; var _this = this; $.each(data, function(i, item) { item.tags = []; var items = JSON.stringify(item).replace(/"/g, '''); $.each(json, function(j, k) { if(undefined == k.pkey){ //全部都有的 $.each(k.option, function(m, n) { var str = "<a href='javascript:void(0)' "; if(n.fn){ str += "onclick='("+n.fn+")("+items+")'"; } str += "><i class='glyphicon "; if(n.icon){ str += n.icon; } str += "'>"; if(n.name){ str += " "+n.name+" "; } str += "</li></a>"; item.tags.push(str); }); }else if(item[k.pkey] == k.value){ //针对此id单独设置的 $.each(k.option, function(m, n) { var str = "<a id='operation"+ k.value + m +"' href='javascript:void(0)' "; if(n.fn){ str += "onclick='("+n.fn+")("+items+")'"; } str += "><i class='glyphicon "; if(n.icon){ str += n.icon; } str += "'>"; if(n.name){ str += " "+n.name+" "; } str += "</li></a>"; item.tags.push(str); }); } }); if (item.nodes) { _this.bindRowOperation(item.nodes, json); } }) return data; }, //获取nodeid行的数据 TreeProto.prototype.getRowValue = function(nodeId) { var val = this.content.treeview('getNode',nodeId); return val; }, //获取全部选中 TreeProto.prototype.getChecked = function() { var val = this.content.treeview('getChecked'); return val; }, TreeProto.prototype.remove = function() { var val = this.content.treeview('remove'); } var $Tree = function(selector) { return new TreeProto(selector); } $Tree.export = function(fn) { jQuery.extend(TreeProto.prototype, fn); } //调用代码如下: var stree = $Tree("#tree"); //构建树对象 var param = ""; $.post("${app}/sysrole/queryRoleFunc.do",param,function(d){ var a = stree.bindRowOperation(d, [{ option:[ { name:"详情", icon:"glyphicon-pencil", fn:function(row){ alert(row.funcid) } } ] },{ pkey:"text", value:"系统管理", option:[ { name:"添加", fn:function(row){ alert(row.funcid) } } ] }]); stree.initTree(a,""); });
相关文章推荐
- 实现封装树节点拖拽操作的组件[原]
- 轻松完成自定义节点配置及读取操作(通用组件)
- ReactNative实战系列 组件封装之Dialog(iOS和Android通用)
- 《React-Native系列》13、 组件封装之Dialog(iOS和Android通用)
- android基于开源网络框架asychhttpclient,二次封装为通用网络请求组件
- 《React-Native系列》组件封装之Dialog(iOS和Android通用)
- cocos2dx 基础触碰效果封装(通用于具有触碰效果的节点或精灵,类似按钮,方便扩展),很久以前的代码
- 在Hibernate中的一个对tree树节点查询封装的通用工具类
- android基于开源网络框架asychhttpclient,二次封装为通用网络请求组件
- extjs 把html 节点 封装成exjs组件
- 继续封装个 Volley 组件
- 数据库组件 Hxj.Data (二十五)(数据库连接配置 - connectionStrings节点)
- 关于Unity中UI中的Image节点以及它的Image组件
- 《用EOM的眼光评判‘我要做全国最最好的标准权限组件和通用权限管理软件’3》(2010/04/08)
- 通用权限管理系统组件 中集成多个子系统的单点登录(网站入口方式)附源码
- 写一个通用数据访问组件
- 通用权限管理系统组件 给信息管理系统加一个初始化的功能,调用存储过程
- ExtJS 动态组件与组件封装
- Mybatis通用DAO设计封装(mybatis)
- Android通用对话框组件设计