jQuery.uuzz = $.uuzz || {}; jQuery.uuzz.Tree = { _treeView: null, _treeManager: null, _url: null, loadData: function(param){ var g = this; g.destroy(); g.getTreeManager().loadData(null, g._url, param); }, /** * 初始化节点选择 */ onAfterAppend: function(){ var g = $.uuzz.Tree; var checkedNodes = g.getTreeManager().getChecked(); $(checkedNodes).each(function () { g.updateCheckedStatus(this); }); }, /** * 获取选择的节点Id字符串,用“_”分割 * @returns {String} */ getCheckedNodes: function(){ var g = $.uuzz.Tree; var nodes = g.getTreeManager().getChecked(); var privileges = ""; if(nodes != null){ $.each(nodes, function(i,node){ var parent = g.getTreeManager().getParent(node.data); if(parent == null){ return true; } if(!$.uuzz.Common.isEmpty(parent.parentNode) && privileges.indexOf(parent.nodeId, 0) == -1){ privileges += parent.nodeId + "_"; } privileges += nodes[i].data.nodeId + "_"; }); } return privileges; }, /** * 销毁树 */ destroy: function(){ if(this.getTreeManager()){ this.getTreeManager().clear(); } }, /** * 获取url * @returns 数据请求url */ getUrl: function(){ return this._url; }, setUrl: function(url){ return this._url = url; }, getTreeManager: function(){ return this._treeManager; }, setTreeManager: function(treeManager){ return this._treeManager = treeManager; }, getTreeView: function(){ return this._treeView; }, /** * 更新节点状态,初始化时使用 * @param node */ updateCheckedStatus: function(node){ var g = this; var treedataindex = parseInt(node.target.attributes['treedataindex'].nodeValue); var treeitem = $("li[treedataindex="+treedataindex+"]"); var obj = treeitem.find(".l-box"); //chekcbox even if ($(obj).hasClass("l-checkbox")) { if (g.getTreeManager().options.autoCheckboxEven){ g.getTreeManager()._setParentCheckboxStatus(treeitem); } else { // 状态:未选中 if ($(obj).hasClass("l-checkbox-unchecked")) { g.trigger('check', [ { data : treenodedata, target : treeitem[0] }, true ]); } // 状态:选中 else if ($(obj).hasClass("l-checkbox-checked")) { g.trigger('check', [ { data : treenodedata, target : treeitem[0] }, false ]); } } } } };
权限树:
jQuery.ux = $.ux || {}; jQuery.ux.PrivilegeTree = jQuery.extend($.uuzz.Tree, { /** * 构建权限树 * @param id divId * @param url 数据请求url * @param delayLoad 是否延迟加载,默认为true * @param param 请求参数 */ buildTree: function(id,url,options){ var g = this; g.setUrl(url); g._treeView = $(id).ligerTree({ method: 'POST', nodeWidth: 150, onCheck: g.onCheck, onAfterAppend:g.onAfterAppend }); g.setTreeManager($(id).ligerGetTreeManager()); var param = null; if(options.param){ param = options.param; } if(options.delayLoad){ g.getTreeManager().loadData(null, g.getUrl(), options.param); } }, /** * 当选则的节点为非查看权限,则默认勾选查看权限 * @param node 当前选择节点 * @param checked 是否选择 */ onCheck: function(node, checked){ var g = $.ux.PrivilegeTree; // 更新几点选择状态 node.data.ischecked = checked; // 获取父节点数据 var parentNode = g.getTreeManager().getParent(node.target); // 获取子节点数据 var childrenData = null; if(parentNode != null){ childrenData = parentNode.children; } if(childrenData != null){ /** 当节点为查看权限时,如果checked为false,则判断同级是否有checked为true的节点,有则查看权限不得取消勾选 **/ if(node.data.isBase && !checked){ // 循环所有子节点,选中isBase=true的子节点 $.each(childrenData, function(i,childNode){ if(!childNode.isBase && childNode.ischecked){ $.ux.Page.uuzzAlertWin('此权限为基本权限,请先取消此模块其它权限', '警告', 'warn', function() { var selectedNode = g.getTreeManager().getNodeDom(node.data); g.getTreeManager().selectNode(selectedNode); }); return false; } }); } /** 当选则的节点为非查看权限,则默认勾选查看权限 **/ if(node.data.leaf && checked && !node.data.isBase){ // 循环所有子节点,选中isBase=true的子节点 $.each(childrenData, function(i,child){ if(child.isBase && !node.isBase && checked){ var selectedNode = g.getTreeManager().getNodeDom(child); g.getTreeManager().selectNode(selectedNode); } }); } } } });
用法:
buildTree: function(){ // 权限树 if($.uuzz.Common.isEmpty(userPermissions._roleId)){ userPermissions._roleId = -1; } var param = { 'queryPrivilegePojo.userId' : userPermissions._userId, 'queryPrivilegePojo.roleId' : userPermissions._roleId}; if(userPermissions._privilegeTree.getTreeView() == null){ var options = { delayLoad: false, param: param }; userPermissions._privilegeTree.buildTree("#treeView",getContextPath('/admin/system/privilege/appPrivilege!loadUserPrivilege.action'),options); } userPermissions._privilegeTree.loadData(param); },