实现根据父节点显示子节点的无极限分类的完整操作
发布时间:2023-10-07


    数据表是设计 包含 id  name   pid   三者 就可以了
    第一步: controller:


    public function actionFunctionPoint()
        {
            $items = FunctionPoint::getFunctionPoint();
    
            return $this->render('function-point' [
                'items' => $items
            ]);
        }


    第二部 model操作数据库 

    /**
         * 获得所有系统功能
         * @return array
         */
        public static function getFunctionPoint()
        {
            $data = [];
            self::_getFunctionPointList($data01);
            return $data;
        }
    
        /**根据父类元素 查询所有子类原生
         * @param $parentId  pid
         * @return array
         */
        public static function _getFunctionPointList(&$data$parentId$level)
        {
            $sql = 'SELECT * FROM t_boss_function_point WHERE is_deleted =0 AND parent_id =:parentId';
            $cmd = Yii::$app->db->createCommand($sql);
            $cmd->bindValue(':parentId' $parentId);
            $results = $cmd->queryAll();
            foreach ($results as $result) {
                $result['level'] = $level;
                array_push($data$result);
                self::_getFunctionPointList($data$result['id']$level+1);
            }
        }


    第三步 :view视图的展示
    <table class="table table-bordered table-hover">
                <thead>
                <tr>
                    <th>功能点名称</th>
                    <th>功能点所对应的controller</th>
                    <th>功能点所对应的action</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td style="text-align: left">系统功能点</td>
                    <td></td>
                    <td></td>
                    <td>
                        <a class="btn btn-link" href="javascript:onOperateData('add')">添加子菜单</a>
                    </td>
                </tr>
                <?php foreach ($items as $item): ?>
                    <tr>
                        <td style="text-align: left;padding-left: <?= $item['level']*25 ?>px"><?= $item['name'] ?></td>
                        <td>
                            <?= $item['controller'] ?>
                        </td>
                        <td><?= $item['action'] ?></td>
                        <td>
                            <a class="btn btn-link" href="javascript:onOperateData('add''<?= $item['id'] ?>')">添加下级功能点</a>
                            <a class="btn btn-link" href="javascript:onOperateData('edit''<?= $item['id'] ?>')">编辑</a>
                            <a class="btn btn-link" href="javascript:deleteFunctionPonit('<?= $item['id'] ?>')">删除</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>


    功能点二 递归删除父节点下面的所有子节点
    public function actionFunctionPointDeleteHandler()
        {
            $request = Yii::$app->request;
            $id = $request->post('id');
            $ids = FunctionPoint::getAllChildIds($id);
            $ids = $id.$ids;
            $result= FunctionPoint::deleteFunctionPoint($ids);
    
            return $this->renderJson(null $result ? 0 : 1 $result ? '操作成功' : '操作失败');
        }


    控制器




    / * 删除系统功能点
         * @param $ids
         * @return bool
         */
        public static function deleteFunctionPoint($ids)
        {
    //        $cmd = Yii::$app->db->createCommand('UPDATE t_boss_function_point SET is_deleted = 1 WHERE FIND_IN_SET(id:ids);');
            $cmd = Yii::$app->db->createCommand('CALL p_boss_function_point_delete(:ids);');
            $cmd->bindValue(':ids' $ids);
            $result = $cmd->execute();
            return $result > 0;
        }
    
        /**
         * 获得所有的子节点 的id值
         * @param $parentId
         * @return string   返回id字符串
         */
        public static function getAllChildIds($parentId)
        {
            $ids ='';
            $sql = 'SELECT `id` FROM t_boss_function_point WHERE is_deleted =0 AND parent_id =:parentId';
            $cmd = Yii::$app->db->createCommand($sql);
            $cmd->bindValue(':parentId' $parentId);
            $results = $cmd->queryAll();
            foreach($results as $v){
                $ids .="".$v['id'];
                $ids .= self::getAllChildIds($v['id']);
            }
            return $ids;
        }
    }