ddddd

权限分配 点击子权限,上级权限也选中-添加

作者:程序员11 时间:2025-08-28 人气:12 QQ交流群\邮箱:1003265987@qq.com
dataid 为 id-1-2-3的形式,id-1-2是id-1-2-3的父权限,以此类推递归获取所有父权限,并通过asort()排序数组,implode把数组转换为字符串,class必须有checkbox-parent,如果是子权限的话加上checkbox-child必须有dataid
要展示的内容

html 代码

<table class="table table-hover">
    <thead class="bordered-darkorange">
    <tr>
        <th>
            配置权限
        </th>
    </tr>
    </thead>
    <tbody>
    {volist name="authRules" id="authRule"}
    <tr>
        <td>
            <label style="cursor: pointer;">
                <?php echo str_repeat('&nbsp;',$authRule['level']*3);?>
                <input name="rules[]" value="{$authRule['id']}"
                       dataid="id-{$authRule['dataid']}" type="checkbox"
                       class="inverted checkbox-parent {if $authRule['level'] != 0}checkbox-child{/if}">
                <span class="text" {if $authRule['level']==0} style="font-weight: bold;" {/if}>
                {$authRule.title}</span>
            </label>
        </td>
    </tr>
    {/volist}
    </tbody>
</table>

js代码

<script type="text/javascript">
    /* 权限配置 */
    $(function () {
        //动态选择框,上下级选中状态变化
        $('input.checkbox-parent').on('change', function () {
            var dataid = $(this).attr("dataid");
            $('input[dataid^=' + dataid + ']').prop('checked', $(this).is(':checked'));
        });
        $('input.checkbox-child').on('change', function () {
            var dataid = $(this).attr("dataid");
            dataid = dataid.substring(0, dataid.lastIndexOf("-"));
            var parent = $('input[dataid=' + dataid + ']');
            if ($(this).is(':checked')) {
                parent.prop('checked', true);
                //循环到顶级
                while (dataid.lastIndexOf("-") != 2) {
                    dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                    parent = $('input[dataid=' + dataid + ']');
                    parent.prop('checked', true);
                }
            } else {
                //父级
                if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                    parent.prop('checked', false);
                    //循环到顶级
                    while (dataid.lastIndexOf("-") != 2) {
                        dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                        parent = $('input[dataid=' + dataid + ']');
                        if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                            parent.prop('checked', false);
                        }
                    }
                }
            }
        });
    });
</script>

php代码

/*
* 获取层级1
* **/
public function authRuleTree(){
   $authRules = $this->order('sort desc')->select();
   return $this->sort($authRules);
}
/*
* 获取层级2
* **/
public function sort($data,$pid=0,$level=0){
   static  $arr = array(); //注意这里面一定要用静态初始化,静态变量初始化算不会把原来的值给覆盖了,会保留原来的值;仅在第一次进入函数时初始化,后续调用不会改变其值
   foreach ($data as $k=>$v){
       if ($v['pid']==$pid){
           $v['dataid'] = $this->getParentId($v['id']);
           $arr[]=$v;
           $this->sort($data,$v['id'],$v['level']);
       }
   }
   return $arr;
}
/*
* 获取父级权限1
* **/
public function getParentId($athRuleId){
   $authRule = $this->select();
   return $this->_getParentId($authRule,$athRuleId,true);
}
/*
* 获取父级权限2
* **/
public function _getParentId($data,$id,$clear = false){
   static $arr = array(); //注意这里面一定要用静态初始化,静态变量初始化算不会把原来的值给覆盖了,会保留原来的值;仅在第一次进入函数时初始化,后续调用不会改变其值
   if ($clear){
       $arr = array();
   }
   foreach ($data as $k=>$v){
       if ($v['id']==$id){
           $arr[]=$v['id'];
           $this->_getParentId($data,$v['pid'],false);
       }
   }
   asort($arr);
   $arrStr = implode('-',$arr);
   return $arrStr;
}
/*
* 添加方法
* **/
public function add()
{
   if ($this->request->isPost()){
       $data = input('post.');
       $validate = new AuthGroupValidate;
       if (!$validate->scene('add')->check($data)) {
           return $this->error($validate->getError());
       }
       if (empty($data['status'])){
           $data['status'] = '0';
       }
       //权限
       if (!empty($data['rules'])){
           $data['rules'] = implode( ',',$data['rules']);
       }
       $result = $this->auth_group->add($data);
       if ($result){
           if ($result === 2){
               $this->error('用户组添加失败,用户组名称不能为空!');
           }
           $this->success('用户组添加成功!','lst');
       }else{
           $this->error('用户组添加失败!');
       }
       return;
   }
   $authRule = new appadminmodelAuthRule();
   $authRules = $authRule->authRuleTree();
   $this->assign('authRules',$authRules);
   return $this->fetch();
}


温馨提示:

欢迎阅读本文章,觉得有用就多来支持一下,没有能帮到您,还有很多文章,希望有一天能帮到您。

权限分配 点击子权限,上级权限也选中-添加---相关文章


评论区

ddddd

程序员-学习的网站-想学习编程的码农可以进来看看

首页

视频教程

购物车

我的订单