简单灵活的权限树

dree 作了一些修改:

1、   增加 Node 的属性,目的是将原来的名称链接改成可选择的 checkbox

 

function Node(id, pid, cname, cvalue, cshow, cchecked, cdisabled, url, title, target, icon, iconOpen, open) {

    this.id = id;

    this.pid = pid;

    //chechbox的名称

    this.cname = cname;

    //chechbox的值

    this.cvalue = cvalue;

    //chechbox的显示

    this.cshow = cshow;

    //chechbox是否被选中,默认是不选

    this.cchecked = cchecked||false;

    //chechbox是否可用,默认是可用

    this.cdisabled = cdisabled||false;

    //节点链接,默认是虚链接

    this.url = url||'#';

    this.title = title;

    this.target = target;

    this.icon = icon;

    this.iconOpen = iconOpen;

    this._io = open || false;

    this._is = false;

    this._ls = false;

    this._hc = false;

    this._ai = 0;

    this._p;

};

2、   将原来节点显示改为 checkbox ,根节点不变,考虑了是否已选和是否可用的状态

 

if(node.pid == this.root.id){

        str += node.cname;

    }else{

        /**组装checkbox开始*/

        checkboxSyntax = "<input type='checkbox' desc='" + node.cshow + "' name='" + node.cname + "' id='" + node.cname + "_" + node.id + "' value='" + node.cvalue + "' onClick='javascript: " + this.obj + ".checkNode(" + node.id+","+node.pid+","+node._hc + ",this.checked);' ";

        //是否被选中

        if(node.cchecked)

            checkboxSyntax += " checked ";

        //是否可用

        if(node.cdisabled)

            checkboxSyntax += " disabled ";        

        checkboxSyntax += ">" + node.cshow;

        /**组装checkbox结束*/

               

        str += checkboxSyntax;

    }

3、   增加一些选中的方法

功能是:

l          选中叶节点时递归选中父节点;

l          选中有子孙的节点时子孙节点递归选中;

l          去掉节点选择时如果兄弟节点没有选中的也去掉直接父节点的选中;

 

//===============================

// 作用:选中节点对象

// 参数: nobj node 对象

//      cobj checkbox 对象

//===============================

dTree.prototype.checkNode = function(id,pid,_hc,checked) {

    //1 、递归选父节点对象(无论是叶节点还是中间节点)

    // 判断同级中有无被选中的,如果有选中的就不可以反选

    if(!this.isHaveBNode(id,pid)){

        if(checked){

            // 选中就一直选到根节点

            this.checkPNodeRecursion(pid,checked);

        }else{

            // 去掉选中仅将其父节点去掉选中

            this.checkPNode(pid,checked);

        }

    }  

   

    //2 、如果是中间结点,具有儿子,递归选子节点对象      

    if(_hc)    

        this.checkSNodeRecursion(id,checked);

   

}

 

//===============================

// 作用:判断同级中有无被选中的

// 参数: id 节点 id

//      pid 节点的父节点 id

//===============================

dTree.prototype.isHaveBNode = function(id,pid) {   

    var isChecked = false

    for (var n=0; n<this.aNodes.length; n++) {

        // 不是节点自身、具有同父节点兄弟节点

        if (this.aNodes[n].pid!=-1&&this.aNodes[n].id!=id&&this.aNodes[n].pid == pid) {         

            if(eval("document.all."+ this.aNodes[n].cname + "_" + this.aNodes[n].id + ".checked"))

                isChecked = true;          

        }

    }

   

    return isChecked;

};

 

//===============================

// 作用:递归选中父节点对象

// 参数: pid 节点的父节点 id

//      ischecked 是否被选中

//===============================

dTree.prototype.checkPNodeRecursion = function(pid,ischecked) {

    for (var n=0; n<this.aNodes.length; n++) {

        if (this.aNodes[n].pid!=-1&&this.aNodes[n].id == pid) {        

            eval("document.all."+ this.aNodes[n].cname + "_" + this.aNodes[n].id + ".checked = " + ischecked);

            this.checkPNodeRecursion(this.aNodes[n].pid,ischecked);

            break;

        }

    }

};

 

//===============================

// 作用:递归选中子节点对象

// 参数: id 节点 id

//      ischecked 是否被选中

//===============================

dTree.prototype.checkSNodeRecursion = function(id,ischecked) { 

    for (var n=0; n<this.aNodes.length; n++) {

        if (this.aNodes[n].pid!=-1&&this.aNodes[n].pid == id) {        

            eval("document.all."+ this.aNodes[n].cname + "_" + this.aNodes[n].id + ".checked = " + ischecked);

            this.checkSNodeRecursion(this.aNodes[n].id,ischecked);         

        }

    }

};

 

//===============================

// 作用:仅选中父节点对象

// 参数: pid 节点的父节点 id

//      ischecked 是否被选中

//===============================

dTree.prototype.checkPNode = function(pid,ischecked) { 

    for (var n=0; n<this.aNodes.length; n++) {

        if (this.aNodes[n].pid!=-1&&this.aNodes[n].id == pid) {        

            eval("document.all."+ this.aNodes[n].cname + "_" + this.aNodes[n].id + ".checked = " + ischecked);          

            break;

        }

    }

};

 

这棵树用 js 编写,调用方法很简单,能够很好的与 struts 结合使用,可用于权限管理的权限分配方面。节点的属性对应用权限的属性:

l          Node(id, pid, cname, cvalue, ctext, cchecked, cdisabled)

l          权限(资源 id ,资源父 id checkbox 控件名用于最终取值,权限 id ,权限描述,是否已拥有该权限,是否可用)

源码下载: AuthorityTree 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值