多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 控制器 ## 思路 一:存储 `session名admin_menu_index` ,有什么用? 二:查询 `AdminMenu` 表得到数组 三:利用 `Tree` 类 和 `变换的数组` 形成树状结构 四:组合成 `treeTable` 插件的形式(这里可能是比较旧的方式) ## 代码 ``` <?php namespace app\agent\controller; use think\Controller; use think\Db; use tree\Tree; class MenuController extends Controller { /** * 后台菜单管理 */ public function index() { session('admin_menu_index', 'Menu/index'); $result = Db::name('AdminMenu')->order(["list_order" => "ASC"])->select()->toArray(); $tree = new Tree(); $tree->icon = ['   │ ', '   ├─ ', '   └─ ']; $tree->nbsp = '   '; $newMenus = []; foreach ($result as $m) { $newMenus[$m['id']] = $m; } foreach ($result as $key => $value) { $result[$key]['parent_id_node'] = ($value['parent_id']) ? ' class="child-of-node-' . $value['parent_id'] . '"' : ''; $result[$key]['style'] = empty($value['parent_id']) ? '' : 'display:none;'; $result[$key]['str_manage'] = '<a href="' . url("Menu/add", ["parent_id" => $value['id'], "menu_id" => $this->request->param("menu_id")]) . '">添加子菜单</a> <a href="' . url("Menu/edit", ["id" => $value['id'], "menu_id" => $this->request->param("menu_id")]) . '">编辑</a> <a class="js-ajax-delete" href="' . url("Menu/delete", ["id" => $value['id'], "menu_id" => $this->request->param("menu_id")]) . '">删除</a> '; $result[$key]['status'] = $value['status'] ? '显示' : '隐藏'; if (APP_DEBUG) { $result[$key]['app'] = $value['app'] . "/" . $value['controller'] . "/" . $value['action']; } } $tree->init($result); $str = "<tr id='node-\$id' \$parent_id_node style='\$style'> <td style='padding-left:20px;'><input name='list_orders[\$id]' type='text' size='3' value='\$list_order' class='input input-order'></td> <td>\$id</td> <td>\$spacer\$name</td> <td>\$app</td> <td>\$status</td> <td>\$str_manage</td> </tr>"; $category = $tree->getTree(0, $str); $this->assign("category", $category); return $this->fetch(); } } ```