最近完成的两个项目都用到Ext,写了些ext js 的代码,总体感觉写这种JS代码还是比较好玩的,遇到问题看看文档,看看源代码,然后再敲敲打打就这样一个ext的tree就出来了,用tree来做权限控制,似乎没啥不好的,就应该这样对各个维度各个层次进行对应的权限控制。
下面是我写的一个维度树的JS类....
要用的地方就直接 var dt = new rs.tbm.DimmensionTree({}); 就可以了
Rs.namespace("rs.tbm"); (function(){ rs.tbm.DimensionTree = function(config){ rs.tbm.DimensionTree.superclass.constructor.call(this, Ext.applyIf(config, { useArrows:false, autoScroll:true, animate:false, enableDD:false, rootVisible:false, containerScroll: true, attributes:{}, lines:true, root:{ id:-1, level:-1, final:false, identifier:"root", desc:"root", path:"root", dimType:-1, rightType:"" }, loader:new rs.tbm.DimensionLoader({ right:config.right, available:config.available }) })); }; Ext.extend(rs.tbm.DimensionTree, Ext.tree.TreePanel, { onRender:function(ct, p){ rs.tbm.DimensionTree.superclass.onRender.call(this, ct, p); }, initEvents:function(){ rs.tbm.DimensionTree.superclass.initEvents.apply(this, arguments); } }); rs.tbm.DimensionLoader = function(config){ rs.tbm.DimensionLoader.superclass.constructor.call(this, config); } Ext.extend(rs.tbm.DimensionLoader, Ext.tree.TreeLoader, { dataUrl:"/rs/tbm/tbmpub/dimension/listener", requestMethod:"post", processResponse:function(response){ try { var o = response.getJSON(); var requset = response.getRequest(); var callback = requset.callback; var node = requset.node; var list = o.dimMember?(o.dimMember instanceof Array?o.dimMember:[o.dimMember]):[]; node.beginUpdate(); for(var i = 0; i < list.length; i++){ var n = this.createNode(list[i], node); if(n){ node.appendChild(n); } } node.endUpdate(); if(typeof callback == "function"){ callback(this, node); } }catch(e){} }, getParams:function(node){ var params = { dimMember:{} }; var dimMember = node.attributes.dimMember; for(var key in dimMember){ if(typeof dimMember[key] != "function"){ params.dimMember[key] = dimMember[key]; } } var bp = this.baseParams; for(var key in bp){ if(typeof bp[key] != "function"){ params[key] = bp[key]; } } return params; }, requestData:function(node, callback){ if(!this.conn) this.conn = rs.net.ConnectionPool.getInstance(); if(this.fireEvent("beforeload", this, node, callback) !== false){ var request = new rs.net.HttpRequest(this.requestMethod, this.dataUrl||this.url, -1); var params = this.getParams(node); params["method"] = "getSubDimMembers"; params["right"] = this.right; params["available"] = this.available; request.setParameter(params); request.callback = callback; request.node = node; this.conn.send(request, this.handleResponse, this); }else{ if(typeof callback == "function"){ callback(); } } }, handleResponse : function(response){ this.transId = false; this.processResponse(response); this.fireEvent("load", this, response.getRequest().node, response); }, createNode:function(attr){ var cfg = { leaf:attr['final'], text:attr['desc'], dimMember:attr, id:this.createIndex(attr) }; return new Ext.tree[cfg.leaf?"TreeNode":"AsyncTreeNode"](cfg); }, createIndex:function(dim){ return [dim['id']||'', dim['dimType']].join('@'); } }); })();