JS+Checkbox Grant User For Management

来源:互联网 发布:netdvr监控软件下载 编辑:程序博客网 时间:2024/05/17 00:58
  1. function NodeAction(actionUrl,actionIcon,actionTitle,actionTarget){
  2.     this.actionUrl=actionUrl;
  3.     this.actionIcon=actionIcon;
  4.     this.actionTarget=actionTarget;
  5.     this.actionTitle=actionTitle;
  6. };
  7. // Node object
  8. function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
  9.     this.id = id;
  10.     this.pid = pid;
  11.     this.name = name;
  12.     this.url = url;
  13.     this.title = title;
  14.     this.target = target;
  15.     this.icon = icon;
  16.     this.iconOpen = iconOpen;
  17.     this._io = open || false;
  18.     this._is = false;
  19.     this._ls = false;
  20.     this._hc = false;
  21.     this._ai = 0;
  22.     this._p;
  23. };
  24. function Node(id, pid, name, url, title, target, icon, iconOpen, open,nodeActionList) {
  25.     this.id = id;
  26.     this.pid = pid;
  27.     this.name = name;
  28.     this.url = url;
  29.     this.title = title;
  30.     this.target = target;
  31.     this.icon = icon;
  32.     this.iconOpen = iconOpen;
  33.     this._io = open || false;
  34.     this._is = false;
  35.     this._ls = false;
  36.     this._hc = false;
  37.     this._ai = 0;
  38.     this.nodeActionList=nodeActionList;
  39.     this._p;
  40. };
  41. // Tree object
  42. function dTree(objName) {
  43.     this.config = {
  44.         imageDir                :null,
  45.         target                  : null,
  46.         folderLinks         : true,
  47.         useSelection        : true,
  48.         useCookies          : true,
  49.         useLines                : true,
  50.         useIcons                : true,
  51.         useStatusText       : false,
  52.         closeSameLevel  : false,
  53.         inOrder                 : false,
  54.         check:false//默认没有 checkbox
  55.         
  56.         mycheckboxName: "mycheckbox" //checkbox的name属性 用作form表单提交
  57.     }
  58.     this.icon = {
  59.         root                :(this.config.imageDir!=null)?this.config.imageDir="/"+base.gif: 'img/base.gif',
  60.         folder          : 'img/folder.gif',
  61.         folderOpen  : 'img/folderopen.gif',
  62.         node                : 'img/page.gif',
  63.         empty               : 'img/empty.gif',
  64.         line                : 'img/line.gif',
  65.         join                : 'img/join.gif',
  66.         joinBottom  : 'img/joinbottom.gif',
  67.         plus                : 'img/plus.gif',
  68.         plusBottom  : 'img/plusbottom.gif',
  69.         minus               : 'img/minus.gif',
  70.         minusBottom : 'img/minusbottom.gif',
  71.         nlPlus          : 'img/nolines_plus.gif',
  72.         nlMinus         : 'img/nolines_minus.gif',
  73.         iconUnCheckAll : 'img/iconUnCheckAll.gif',
  74.         iconCheckAll :'img/iconCheckAll.gif',
  75.         iconCheckGray : 'img/iconCheckGray.gif'
  76.     };
  77.     this.obj = objName;
  78.     this.aNodes = [];
  79.     this.aIndent = [];
  80.     this.root = new Node(-1);
  81.     this.selectedNode = null;
  82.     this.selectedFound = false;
  83.     this.completed = false;
  84. };
  85. // Tree object
  86. dTree.prototype.reSetImagePath=function(){
  87.  this.icon = {
  88.         root                :(this.config.imageDir!=null)?this.config.imageDir+'/base.gif''img/base.gif',
  89.         folder          : (this.config.imageDir!=null)?this.config.imageDir+'/folder.gif':'img/folder.gif',
  90.         folderOpen  : (this.config.imageDir!=null)?this.config.imageDir+'/folderopen.gif':'img/folderopen.gif',
  91.         node                : (this.config.imageDir!=null)?this.config.imageDir+'/page.gif':'img/page.gif',
  92.         empty               : (this.config.imageDir!=null)?this.config.imageDir+'/empty.gif':'img/empty.gif',
  93.         line                : (this.config.imageDir!=null)?this.config.imageDir+'/line.gif':'img/line.gif',
  94.         join                : (this.config.imageDir!=null)?this.config.imageDir+'/join.gif':'img/join.gif',
  95.         joinBottom  : (this.config.imageDir!=null)?this.config.imageDir+'/joinbottom.gif':'img/joinbottom.gif',
  96.         plus                : (this.config.imageDir!=null)?this.config.imageDir+'/plus.gif':'img/plus.gif',
  97.         plusBottom  : (this.config.imageDir!=null)?this.config.imageDir+'/plusbottom.gif':'img/plusbottom.gif',
  98.         minus               : (this.config.imageDir!=null)?this.config.imageDir+'/minus.gif':'img/minus.gif',
  99.         minusBottom : (this.config.imageDir!=null)?this.config.imageDir+'/minusbottom.gif':'img/minusbottom.gif',
  100.         nlPlus          : (this.config.imageDir!=null)?this.config.imageDir+'/nolines_plus.gif':'img/nolines_plus.gif',
  101.         nlMinus         : (this.config.imageDir!=null)?this.config.imageDir+'/nolines_minus.gif':'img/nolines_minus.gif',
  102.         //checkbox 图片
  103.         iconUnCheckAll : (this.config.imageDir!=null)?this.config.imageDir+'/iconUnCheckAll.gif':'img/iconUnCheckAll.gif',
  104.         iconCheckAll : (this.config.imageDir!=null)?this.config.imageDir+'/iconCheckAll.gif':'img/iconCheckAll.gif',
  105.         iconCheckGray : (this.config.imageDir!=null)?this.config.imageDir+'/iconCheckGray.gif':'img/iconCheckGray.gif'
  106.     };
  107. }
  108. // Adds a new node to the node array
  109. dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {
  110.     this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);
  111. };
  112. // Adds a new node to the node array
  113. dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open,actionUrl,actionIcon) {
  114.     this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open,actionUrl,actionIcon);
  115. };
  116. // Open/close all nodes
  117. dTree.prototype.openAll = function() {
  118.     this.oAll(true);
  119. };
  120. dTree.prototype.closeAll = function() {
  121.     this.oAll(false);
  122. };
  123. // Outputs the tree to the page
  124. dTree.prototype.toString = function() {
  125.     var str = '<div class="dtree">/n';
  126.     if (document.getElementById) {
  127.         if (this.config.useCookies) this.selectedNode = this.getSelected();
  128.         str += this.addNode(this.root);
  129.     } else str += 'Browser not supported.';
  130.     str += '</div>';
  131.     if (!this.selectedFound) this.selectedNode = null;
  132.     this.completed = true;
  133.     
  134.     return str;
  135. };
  136. // Creates the tree structure
  137. dTree.prototype.addNode = function(pNode) {
  138.     var str = '';
  139.     var n=0;
  140.     if (this.config.inOrder) n = pNode._ai;
  141.     for (n; n<this.aNodes.length; n++) {
  142.         if (this.aNodes[n].pid == pNode.id) {
  143.             var cn = this.aNodes[n];
  144.             cn._p = pNode;
  145.             cn._ai = n;
  146.             this.setCS(cn);
  147.             if (!cn.target && this.config.target) cn.target = this.config.target;
  148.             if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
  149.             if (!this.config.folderLinks && cn._hc) cn.url = null;
  150.             if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
  151.                     cn._is = true;
  152.                     this.selectedNode = n;
  153.                     this.selectedFound = true;
  154.             }
  155.             str += this.node(cn, n);
  156.             if (cn._ls) break;
  157.         }
  158.     }
  159.     return str;
  160. };
  161. // Creates the node icon, url and text
  162. dTree.prototype.node = function(node, nodeId) {
  163.     var str = '<div class="dTreeNode">' + this.indent(node, nodeId);
  164.     if (this.config.useIcons) {
  165.         if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
  166.         if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
  167.         //新加的 checkbox
  168.         if(this.config.check==true){
  169.             str+= '<img checked=0  src="'+this.icon.iconUnCheckAll+'" value='+node.id+' id="c'+  this.obj + nodeId + '" onclick="javascript:'+this.obj+'.cc('+nodeId+')"/>';
  170.             str+='<input type="checkbox" style="display:none" name="'+this.config.mycheckboxName+'"  value='+node.id+' id="ccc'+  this.obj + nodeId + '" onclick="javascript:'+this.obj+'.changeCheckbox('+nodeId+')"/>';
  171.         }
  172.         if (this.root.id == node.pid) {
  173.             node.icon = this.icon.root;
  174.             node.iconOpen = this.icon.root;
  175.         }
  176.         str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
  177.     }
  178.     
  179.     
  180.     if (node.url) {
  181.         str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
  182.         if (node.title) str += ' title="' + node.title + '"';
  183.         if (node.target) str += ' target="' + node.target + '"';
  184.         if (this.config.useStatusText) str += ' onmouseover="window.status=/'' + node.name + '/';return true;" onmouseout="window.status=/'/';return true;" ';
  185.         if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
  186.             str += ' onclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
  187.         str += '>';
  188.     }
  189.     else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id){
  190.         str += '<a href="#" onclick="javascript:' + this.obj + '.o(' + nodeId + ');" class="node">';
  191.     }
  192.     str += node.name;
  193.     if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
  194.     if(node.nodeActionList){
  195.         for(i=0;i<node.nodeActionList.size();i++){
  196.             nodeActionObj= node.nodeActionList.get(i);
  197.             str+=" ";
  198.             
  199.             if(nodeActionObj.actionUrl.indexOf("javascript:")==-1){
  200.                 str+='<a href="' +nodeActionObj.actionUrl+ '" ';
  201.                 str+=' target="' +((nodeActionObj.actionTarget) ? nodeActionObj.actionTarget : this.config.target)  + '" ';
  202.             }else{
  203.                 str+='<a href="#" onclick="' +nodeActionObj.actionUrl+ '" ';
  204.             }
  205.             str+=' >';
  206.             str += '<img id="i' + this.obj + nodeId + '" src="' + ((nodeActionObj.actionIcon) ? nodeActionObj.actionIcon : node.icon)
  207.                     + '" alt="'+nodeActionObj.actionTitle+'" />';
  208.             str+='</a>';
  209.         }
  210.     }
  211.     str += '</div>';
  212.     if (node._hc) {
  213.         str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
  214.         str += this.addNode(node);
  215.         str += '</div>';
  216.     }
  217.     
  218.     this.aIndent.pop();
  219.     return str;
  220. };
  221. // Adds the empty and line icons
  222. dTree.prototype.indent = function(node, nodeId) {
  223.     var str = '';
  224.     if (this.root.id != node.pid) {
  225.         for (var n=0; n<this.aIndent.length; n++)
  226.             str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
  227.         (node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
  228.         if (node._hc) {
  229.             str += '<a href="#" onclick="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
  230.             if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
  231.             else str += ( (node._io) ? ((node._ls && this.config.useLines) ? this.icon.minusBottom : this.icon.minus) : ((node._ls && this.config.useLines) ? this.icon.plusBottom : this.icon.plus ) );
  232.             str += '" alt="" /></a>';
  233.         } else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
  234.     }
  235.     return str;
  236. };
  237. // Checks if a node has any children and if it is the last sibling
  238. dTree.prototype.setCS = function(node) {
  239.     var lastId;
  240.     for (var n=0; n<this.aNodes.length; n++) {
  241.         if (this.aNodes[n].pid == node.id) node._hc = true;
  242.         if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
  243.     }
  244.     if (lastId==node.id) node._ls = true;
  245. };
  246. // Returns the selected node
  247. dTree.prototype.getSelected = function() {
  248.     var sn = this.getCookie('cs' + this.obj);
  249.     return (sn) ? sn : null;
  250. };
  251. // Highlights the selected node
  252. dTree.prototype.s = function(id) {
  253.     if (!this.config.useSelection) return;
  254.     var cn = this.aNodes[id];
  255.     if (cn._hc && !this.config.folderLinks) return;
  256.     if (this.selectedNode != id) {
  257.         if (this.selectedNode || this.selectedNode==0) {
  258.             eOld = document.getElementById("s" + this.obj + this.selectedNode);
  259.             eOld.className = "node";
  260.         }
  261.         eNew = document.getElementById("s" + this.obj + id);
  262.         eNew.className = "nodeSel";
  263.         this.selectedNode = id;
  264.         if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
  265.     }
  266. };
  267. // Toggle Open or close
  268. dTree.prototype.o = function(id) {
  269.     var cn = this.aNodes[id];
  270.     this.nodeStatus(!cn._io, id, cn._ls);
  271.     cn._io = !cn._io;
  272.     if (this.config.closeSameLevel) this.closeLevel(cn);
  273.     if (this.config.useCookies) this.updateCookie();
  274. };
  275. // Open or close all nodes
  276. dTree.prototype.oAll = function(status) {
  277.     for (var n=0; n<this.aNodes.length; n++) {
  278.         if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
  279.             this.nodeStatus(status, n, this.aNodes[n]._ls)
  280.             this.aNodes[n]._io = status;
  281.         }
  282.     }
  283.     if (this.config.useCookies) this.updateCookie();
  284. };
  285. // Opens the tree to a specific node
  286. dTree.prototype.openTo = function(nId, bSelect, bFirst) {
  287.     if (!bFirst) {
  288.         for (var n=0; n<this.aNodes.length; n++) {
  289.             if (this.aNodes[n].id == nId) {
  290.                 nId=n;
  291.                 break;
  292.             }
  293.         }
  294.     }
  295.     var cn=this.aNodes[nId];
  296.     if (cn.pid==this.root.id || !cn._p) return;
  297.     cn._io = true;
  298.     cn._is = bSelect;
  299.     if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
  300.     if (this.completed && bSelect) this.s(cn._ai);
  301.     else if (bSelect) this._sn=cn._ai;
  302.     this.openTo(cn._p._ai, falsetrue);
  303. };
  304. // Closes all nodes on the same level as certain node
  305. dTree.prototype.closeLevel = function(node) {
  306.     for (var n=0; n<this.aNodes.length; n++) {
  307.         if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
  308.             this.nodeStatus(false, n, this.aNodes[n]._ls);
  309.             this.aNodes[n]._io = false;
  310.             this.closeAllChildren(this.aNodes[n]);
  311.         }
  312.     }
  313. }
  314. // Closes all children of a node
  315. dTree.prototype.closeAllChildren = function(node) {
  316.     for (var n=0; n<this.aNodes.length; n++) {
  317.         if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
  318.             if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
  319.             this.aNodes[n]._io = false;
  320.             this.closeAllChildren(this.aNodes[n]);      
  321.         }
  322.     }
  323. }
  324. // Change the status of a node(open or closed)
  325. dTree.prototype.nodeStatus = function(status, id, bottom) {
  326.     eDiv    = document.getElementById('d' + this.obj + id);
  327.     eJoin   = document.getElementById('j' + this.obj + id);
  328.     if (this.config.useIcons) {
  329.         eIcon   = document.getElementById('i' + this.obj + id);
  330.         eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
  331.     }
  332.     eJoin.src = (this.config.useLines)?
  333.     ((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
  334.     ((status)?this.icon.nlMinus:this.icon.nlPlus);
  335.     eDiv.style.display = (status) ? 'block''none';
  336. };
  337. // [Cookie] Clears a cookie
  338. dTree.prototype.clearCookie = function() {
  339.     var now = new Date();
  340.     var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
  341.     this.setCookie('co'+this.obj, 'cookieValue', yesterday);
  342.     this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
  343. };
  344. // [Cookie] Sets value in a cookie
  345. dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
  346.     document.cookie =
  347.         escape(cookieName) + '=' + escape(cookieValue)
  348.         + (expires ? '; expires=' + expires.toGMTString() : '')
  349.         + (path ? '; path=' + path : '')
  350.         + (domain ? '; domain=' + domain : '')
  351.         + (secure ? '; secure' : '');
  352. };
  353. // [Cookie] Gets a value from a cookie
  354. dTree.prototype.getCookie = function(cookieName) {
  355.     var cookieValue = '';
  356.     var posName = document.cookie.indexOf(escape(cookieName) + '=');
  357.     if (posName != -1) {
  358.         var posValue = posName + (escape(cookieName) + '=').length;
  359.         var endPos = document.cookie.indexOf(';', posValue);
  360.         if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
  361.         else cookieValue = unescape(document.cookie.substring(posValue));
  362.     }
  363.     return (cookieValue);
  364. };
  365. // [Cookie] Returns ids of open nodes as a string
  366. dTree.prototype.updateCookie = function() {
  367.     var str = '';
  368.     for (var n=0; n<this.aNodes.length; n++) {
  369.         if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
  370.             if (str) str += '.';
  371.             str += this.aNodes[n].id;
  372.         }
  373.     }
  374.     this.setCookie('co' + this.obj, str);
  375. };
  376. // [Cookie] Checks if a node id is in a cookie
  377. dTree.prototype.isOpen = function(id) {
  378.     var aOpen = this.getCookie('co' + this.obj).split('.');
  379.     for (var n=0; n<aOpen.length; n++)
  380.         if (aOpen[n] == id) return true;
  381.     return false;
  382. };
  383. //新添加 checkbox 操作
  384. /*
  385.  dTree.prototype.changeCheckbox=function(nodeId){
  386.   var cs = document.getElementById("ccc"+this.obj+nodeId).checked;
  387.   var n,node = this.aNodes[nodeId];
  388.   var len =this.aNodes.length;
  389.   for (n=0; n<len; n++) {
  390.        if (this.aNodes[n].pid == node.id) {
  391.         document.getElementById("ccc"+this.obj+n).checked=cs;
  392.         this.changeCheckbox(n);  
  393.        }
  394.   }
  395.   if(cs==false)return;
  396.   var pid=node.pid;
  397.   var bSearch;
  398.   do{
  399.    bSearch=false;
  400.    for(n=0;n<len;n++){
  401.     if(this.aNodes[n].id==pid){
  402.      document.getElementById("ccc"+this.obj+n).checked=true;
  403.      pid=this.aNodes[n].pid;
  404.      bSearch = true;    
  405.      break;
  406.     }
  407.    }
  408.   }while(bSearch==true);
  409.  }
  410. */
  411. dTree.prototype.cc=function(nodeId){
  412.     
  413.     var currentNode =document.getElementById("c"+this.obj+nodeId);
  414.     var cs = currentNode.checked;
  415.     //对自己和子节点进行操作
  416.     if(cs<1){
  417.         this.changeChildNodes(nodeId,1,this.icon.iconCheckAll);
  418.     }else{
  419.         this.changeChildNodes(nodeId,0,this.icon.iconUnCheckAll);
  420.     }
  421.     //对父节点进行操作
  422.     var n,node = this.aNodes[nodeId];
  423.     var len =this.aNodes.length;
  424.     var pid=node.pid;
  425.     for(n=0;n<len;n++){
  426.         if(this.aNodes[n].id==pid){
  427.              this.changeParentNodes(n);
  428.              break;
  429.         }
  430.     }
  431. }
  432. //对子节点进行操作
  433. dTree.prototype.changeChildNodes=function(nodeId,status,img){
  434.     document.getElementById("c"+this.obj+nodeId).checked=status;
  435.     document.getElementById("c"+this.obj+nodeId).src=img;
  436.     if(status>0){
  437.         document.getElementById("ccc"+this.obj+nodeId).checked=true;
  438.     }else{
  439.         document.getElementById("ccc"+this.obj+nodeId).checked=false;
  440.     }
  441.     var n,node = this.aNodes[nodeId];
  442.     var len =this.aNodes.length;
  443.     for (n=0; n<len; n++) {
  444.         if (this.aNodes[n].pid == node.id) {
  445.             document.getElementById("c"+this.obj+n).checked=status;
  446.             document.getElementById("c"+this.obj+n).src=img;
  447.             if(status>0){
  448.                 document.getElementById("ccc"+this.obj+nodeId).checked=true;
  449.             }else{
  450.                 document.getElementById("ccc"+this.obj+nodeId).checked=false;
  451.             }   
  452.             this.changeChildNodes(n,status,img);  
  453.         }
  454.     }
  455. }
  456. //对所有父节点进行操作
  457. dTree.prototype.changeParentNodes=function(nodeId){
  458.     this.changeParentNode(nodeId);
  459.     var n,node = this.aNodes[nodeId];
  460.     var len =this.aNodes.length;
  461.     var pid=node.pid;
  462.     var bSearch;
  463.     do{
  464.         bSearch=false;
  465.         for(n=0;n<len;n++){
  466.             if(this.aNodes[n].id==pid){
  467.                 this.changeParentNode(n);
  468.                 pid=this.aNodes[n].pid;
  469.                 bSearch = true;    
  470.                 break;
  471.             }
  472.         }
  473.     }while(bSearch==true);
  474. }
  475. //对单个父节点进行操作
  476. dTree.prototype.changeParentNode=function(nodeId){
  477.     var n,node = this.aNodes[nodeId];
  478.     var len =this.aNodes.length;
  479.     var statuFlag;
  480.     var tempNum=0;
  481.     var childsCount=0;
  482.     for(n=0; n<len; n++){
  483.         if (this.aNodes[n].pid == node.id) {
  484.             childsCount++;
  485.             var tempNode =  document.getElementById("c"+this.obj+n);
  486.             if(tempNode.checked<0){
  487.                 tempNum=-1;
  488.                 break;
  489.             }else{
  490.                 if(tempNode.checked==1){
  491.                     tempNum+=1;
  492.                 }
  493.             }   
  494.         }
  495.     }
  496.     if(tempNum==-1||(tempNum<childsCount&&tempNum>0)){
  497.         document.getElementById("c"+this.obj+nodeId).checked=-1;
  498.         document.getElementById("c"+this.obj+nodeId).src=this.icon.iconCheckGray;
  499.         document.getElementById("ccc"+this.obj+nodeId).checked=true;
  500.     }else if(tempNum==0){
  501.         document.getElementById("c"+this.obj+nodeId).checked=0;
  502.         document.getElementById("c"+this.obj+nodeId).src=this.icon.iconUnCheckAll;
  503.         document.getElementById("ccc"+this.obj+nodeId).checked=false;
  504.     }else if(tempNum==childsCount){
  505.         document.getElementById("c"+this.obj+nodeId).checked=1;
  506.         document.getElementById("c"+this.obj+nodeId).src=this.icon.iconCheckAll;
  507.         document.getElementById("ccc"+this.obj+nodeId).checked=true;
  508.     }   
  509. }
  510. //默认选中
  511. dTree.prototype.setCheck=function(nodeIds){
  512.     var tempIds = this.getLeafNodes(nodeIds);//得到叶子节点
  513.     var n;
  514.     var len =this.aNodes.length;
  515.     for (n=0; n<len; n++) {
  516.         var nodeObj = document.getElementById("c"+this.obj+n);      
  517.         if(this.isInIds(nodeObj.value,tempIds)){
  518.             nodeObj.click();        
  519.         }
  520.     }
  521. }
  522. //判断id是否属于ids
  523. dTree.prototype.isInIds=function(id,ids){
  524.     var idarray = ids.split(",");
  525.     for(var i=0;i<idarray.length;i++){
  526.         if(id==idarray[i]){
  527.             return true;
  528.         }
  529.     }
  530.     return false;
  531. }
  532. //得到叶子节点
  533. dTree.prototype.getLeafNodes=function(ids){
  534.     var tempIds = ids.split(",");
  535.     var returnIds="";
  536.     for(var i=0;i<tempIds.length;i++){
  537.         if(this.isLeafNode(tempIds[i])){
  538.             returnIds+=","+tempIds[i];
  539.         }
  540.     }
  541.     return returnIds;
  542. }
  543. //判断是否叶子节点
  544. dTree.prototype.isLeafNode=function(id){
  545.     var len =this.aNodes.length;
  546.     for (n=0; n<len; n++) {             
  547.         if(this.aNodes[n].pid==id){
  548.             return false;   
  549.         }
  550.     }
  551.     return true;
  552. }
  553. // If Push and pop is not implemented by the browser
  554. if (!Array.prototype.push) {
  555.     Array.prototype.push = function array_push() {
  556.         for(var i=0;i<arguments.length;i++)
  557.             this[this.length]=arguments[i];
  558.         return this.length;
  559.     }
  560. };
  561. if (!Array.prototype.pop) {
  562.     Array.prototype.pop = function array_pop() {
  563.         lastElement = this[this.length-1];
  564.         this.length = Math.max(this.length-1,0);
  565.         return lastElement;
  566.     }
  567. };

 

dTree.js

Test.htm

  1. <html>
  2.     <head>
  3.         <title>MENU</title>
  4.         <script language=javascript src="dtree.js"></script>
  5.         <link rel="stylesheet" href="dtree.css" type="text/css" />
  6.     </head>
  7.     <body bgcolor="#bddcff">
  8.     <form action="example01.html" method="post" id="testform">
  9.         <font size='2px'>只展开一个菜单</font><input type='checkbox' id='closeSameLevelCheckbox' onclick='changeCloseStatus();' checked/>
  10.         
  11.             <script language="JavaScript">
  12.             function changeCloseStatus(){
  13.                 var closeSameLevelCheckbox = document.getElementById("closeSameLevelCheckbox");
  14.                 if(closeSameLevelCheckbox.checked){
  15.                     d.config.closeSameLevel = true;
  16.                 }else{
  17.                     d.config.closeSameLevel = false;
  18.                 }
  19.             }
  20.                                 d = new dTree('d');
  21.                                 d.config.target = "mainFrame";
  22.                                 d.config.useCheckBox=true;
  23.                                 d.config.imageDir = '../dtree/img';
  24.                                 d.reSetImagePath();
  25.                                 d.config.folderLinks = true;
  26.                                 d.config.closeSameLevel = true;
  27.                                 d.config.check=true;//显示复选框
  28.                                 d.config.mycheckboxName="ids";//设置<input type='checkbox' name="ids"/>name的属性
  29.                                 var isOpen;
  30.                                 //根节点
  31.                                 d.add(0,-1,'Root');
  32.                                  
  33.                              
  34.                                 d.add('315', '87', '企业信息维护', '../webContent/accountMana/toModiEnterprise.do?whichmethod=toModiEnterprise', 'node title', 'mainFrame');
  35.                                 
  36.                                      d.add('320', '0', '市场信息库');
  37.                                     
  38.                              
  39.                                 d.add('323', '320', '信息检索', '../bidding/bid_queryFrontAction.do', 'node title', 'mainFrame');
  40.                                 
  41.                              
  42.                                 d.add('325', '320', '市场信息创建', '../bidding/bidRegPreAction.do', 'node title', 'mainFrame');
  43.                                 
  44.                                      d.add('335', '0', '应用订购管理');
  45.                                     
  46.                              
  47.                                 d.add('337', '335', '订购新应用', 'orderMana/app_exclude_listAction.do', 'node title', 'mainFrame');
  48.                                 
  49.                              
  50.                                 d.add('338', '335', '订单查询', 'orderMana/query_orderAction.do', 'node title', 'mainFrame');
  51.                                 
  52.                              
  53.                                 d.add('339', '335', '订单管理', 'orderMana/manager_OrderAction.do', 'node title', 'mainFrame');
  54.                                 
  55.                                      d.add('520', '0', '企业产品信息管理');
  56.                                     
  57.                              
  58.                                 d.add('522', '520', '产品信息创建', 'productInfoMana.do', 'node title', 'mainFrame');
  59.                                 
  60.                              
  61.                                 d.add('523', '520', '产品信息维护', 'productMana.do', 'node title', 'mainFrame');
  62.                                 
  63.                              
  64.                                 d.add('184', '87', '新建帐户', 'accountMana/toCreateAccount.do', 'node title', 'mainFrame');
  65.                                 
  66.                              
  67.                                 d.add('186', '87', '帐户维护', 'accountMana/toAccountList.do?menu=editRole', 'node title', 'mainFrame');
  68.                                 
  69.                              
  70.                                 d.add('185', '87', '重置密码', 'accountMana/toAccountList.do?menu=changePs', 'node title', 'mainFrame');
  71.                                 
  72.                              
  73.                                 d.add('206', '87', '用户信息查询', 'accountMana/toAccountList.do?menu=query', 'node title', 'mainFrame');
  74.                                 
  75.                              
  76.                                 d.add('242', '87', '角色管理', 'roleMana/toRoleList.do', 'node title', 'mainFrame');
  77.                                 
  78.                              
  79.                                 d.add('240', '87', '组织结构管理', 'DeptMana/toDeptList.do', 'node title', 'mainFrame');
  80.                                 
  81.                                      d.add('87', '0', '企业管理');
  82.                                     
  83.                              
  84.                                 d.add('243', '0', '个人信息', 'accountMana/currentAccountInfo.do', 'node title', 'mainFrame');
  85.                                 
  86.                                 d.closeAll();
  87.                                 document.write(d);
  88.                                 //d.setCheck("315,87,320,323"); //设置默认选中的项目
  89.          </script>
  90.          <input type="button" value="测试提交" onClick="subtest();">
  91.         </form>
  92.         <script>
  93.         function subtest(){
  94.             var str="";
  95.             var logstr="";
  96.             var subids = document.all.item("ids");
  97.             for(var i=0;i<subids.length;i++){
  98.                 if(document.all.item("ids",i).checked){
  99.                     logstr=document.all.item("ids",i).value;
  100.                     (i==0)?str=logstr:str+=","+logstr;
  101.                 }
  102.             }
  103.             alert(str);
  104.         }
  105.         </script>
  106.     </body>
  107. </html>
  108. sql="select * from myTable Where id in ("&str&")"
  109. 用MzTreeView 进行多用户管理时,这个就派上用场了.