博客列表 >后台四 菜单列表

后台四 菜单列表

自由之上
自由之上原创
2021年03月13日 10:29:141127浏览


一、菜单列表 模块
1、创建 菜单列表 视图模板


resources/views/admins/menus/index.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>菜单列表</title>
  5. <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
  6. <script type="text/javascript" src="/static/layui/layui.js"></script>
  7. </head>
  8. <body style="padding: 10px;">
  9. <input type="hidden" name="mid" value="{{$pmenu['mid']}}">
  10. <div style="text-align: right;">
  11. @if($pmenu)
  12. <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="back({{$pmenu['pid']}})">返回</button>
  13. @endif
  14. <button class="layui-btn layui-btn-sm" onclick="add()"><i class="layui-icon layui-icon-addition"></i>添加</button>
  15. </div>
  16. <table class="layui-table">
  17. <thead>
  18. <tr>
  19. <td>ID</td>
  20. <td>MID</td>
  21. <td>菜单名称</td>
  22. <td>controller</td>
  23. <td>action</td>
  24. <td>是否隐藏</td>
  25. <td>是否禁用</td>
  26. <td>操作</td>
  27. </tr>
  28. </thead>
  29. <tbody>
  30. @foreach($list as $key=>$item)
  31. <tr>
  32. <td>{{$key}}</td>
  33. <td>{{$item['mid']}}</td>
  34. <td>{{$item['title']}}</td>
  35. <td>{{$item['controller']}}</td>
  36. <td>{{$item['action']}}</td>
  37. <td>{!!$item['ishidden']==1?'<span style="coloer:#cdcdcd;">隐藏</span>':'显示' !!}</td>
  38. <td>{!!$item['status']==1?'<span style="color:red;">禁用</span>':'<span style="color:green;">正常</span>'!!}</td>
  39. <td>
  40. <button class="layui-btn layui-btn-xs layui-btn-primary" onclick="child({{$item['mid']}})">下级菜单</button>
  41. <button class="layui-btn layui-btn-xs" onclick="edit({{$item['mid']}})">修改</button>
  42. <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="del({{$item['mid']}})">删除</button>
  43. </td>
  44. </tr>
  45. @endforeach
  46. </tbody>
  47. </table>
  48. </body>
  49. </html>
  50. <script type="text/javascript">
  51. layui.use(['layer'], function () {
  52. $ = layui.jquery;
  53. layer = layui.layer;
  54. });
  55. // 添加管理员
  56. function add() {
  57. var mid = $('input[name="mid"]').val();
  58. layer.open({
  59. type: 2,
  60. title: '添加菜单',
  61. shade: 0.3,
  62. area: ['500px', '450px'],
  63. content: '/admins/menus/add?pid=' + mid
  64. });
  65. }
  66. // 修改菜单
  67. function edit(mid) {
  68. layer.open({
  69. type: 2,
  70. title: '修改菜单',
  71. shade: 0.3,
  72. area: ['500px', '450px'],
  73. content: '/admins/menus/edit?mid=' + mid
  74. });
  75. }
  76. // 删除菜单
  77. function del(mid) {
  78. layer.confirm('确定要删除吗?', {
  79. icon: 3,
  80. btn: ['确定', '取消']
  81. }, function () {
  82. $.get('/admins/menus/del', {mid: mid}, function (res) {
  83. if (res.code > 0) {
  84. return layer.alert(res.msg, {icon: 2});
  85. }
  86. layer.msg(res.msg);
  87. setTimeout(function () {
  88. window.location.reload();
  89. }, 1000);
  90. }, 'json');
  91. });
  92. }
  93. // 下级菜单
  94. function child(mid) {
  95. window.location.href = '?mid=' + mid;
  96. }
  97. // 返回上一级
  98. function back(mid) {
  99. window.location.href = '/admins/menus/index?mid=' + mid;
  100. }
  101. </script>

2、创建 菜单列表 控制器

app/Http/Controllers/admins/Menus.php

  1. <?php
  2. namespace App\Http\Controllers\admins;
  3. use App\Http\Controllers\Controller;
  4. /**
  5. * 菜单 控制器
  6. * @package App\Http\Controllers\admins
  7. */
  8. class Menus extends Controller{
  9. }


3、菜单列表 控制器处理
app/Http/Controllers/admins/Menus.php

  1. use Illuminate\Http\Request;
  2. use Illuminate\Support\Facades\DB;
  3. use Illuminate\Support\Facades\Log;
  1. //菜单列表
  2. public function index(Request $req) {
  3. $mid = (int)$req->mid;//子列表查询有效
  4. Log::debug("菜单列表:mid:", [$mid]);
  5. $data['list'] = DB::table('admin_menu')->where('pid', $mid)->where('isdel', 0)->orderBy('mid', 'desc')->lists();
  6. $data['pmenu'] = DB::table('admin_menu')->where('mid', $mid)->item();
  7. return view('admins/menus/index', $data);
  8. }


4、菜单列表 注册路由

  1. use App\Http\Controllers\admins\Menus;
  2. //显示菜单列表
  3. Route::get('/admins/menus/index', [Menus::class, 'index']);



5、查看效果

二、添加与保存菜单 模块

1、创建 添加菜单 视图模板


resources/views/admins/menus/add.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>添加菜单</title>
  5. <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
  6. <script type="text/javascript" src="/static/layui/layui.js"></script>
  7. </head>
  8. <body style="padding: 10px;">
  9. <div class="layui-form">
  10. @csrf
  11. <input type="hidden" name="pid" value="{{$pid}}">
  12. <div class="layui-form-item">
  13. <label class="layui-form-label">菜单名称</label>
  14. <div class="layui-input-block">
  15. <input type="text" name="title" class="layui-input">
  16. </div>
  17. </div>
  18. <div class="layui-form-item">
  19. <label class="layui-form-label">controller</label>
  20. <div class="layui-input-block">
  21. <input type="text" name="controller" class="layui-input">
  22. </div>
  23. </div>
  24. <div class="layui-form-item">
  25. <label class="layui-form-label">action</label>
  26. <div class="layui-input-block">
  27. <input type="text" name="action" class="layui-input">
  28. </div>
  29. </div>
  30. <div class="layui-form-item">
  31. <label class="layui-form-label">状态</label>
  32. <div class="layui-input-block">
  33. <input type="checkbox" name="ishidden" lay-skin="primary" title="隐藏">
  34. <input type="checkbox" name="status" lay-skin="primary" title="禁用">
  35. </div>
  36. </div>
  37. <div class="layui-input-block">
  38. <button class="layui-btn" onclick="save()">保存</button>
  39. </div>
  40. </div>
  41. </body>
  42. </html>
  43. <script type="text/javascript">
  44. layui.use(['form'], function () {
  45. $ = layui.jquery;
  46. form = layui.form;
  47. layer = layui.layer;
  48. });
  49. function save() {
  50. var pid = $('input[name="pid"]').val();
  51. var title = $('input[name="title"]').val();
  52. var controller = $('input[name="controller"]').val();
  53. var action = $('input[name="action"]').val();
  54. var ishidden = $('input[name="ishidden"]').is(':checked') ? 1 : 0;
  55. var status = $('input[name="status"]').is(':checked') ? 1 : 0;
  56. var _token = $('input[name="_token"]').val();
  57. if (title === '') {
  58. return layer.alert('请填写菜单名称', {icon: 2});
  59. }
  60. $.post('/admins/menus/save', {
  61. pid: pid,
  62. title: title,
  63. controller: controller,
  64. action: action,
  65. ishidden: ishidden,
  66. status: status,
  67. _token: _token
  68. }, function (res) {
  69. if (res.code > 0) {
  70. return layer.alert(res.msg, {icon: 2});
  71. }
  72. layer.msg(res.msg);
  73. setTimeout(function () {
  74. parent.window.location.reload();
  75. }, 1000);
  76. }, 'json');
  77. }
  78. </script>

2、添加与保存菜单 控制器处理
app/Http/Controllers/admins/Menus.php

  1. //添加菜单
  2. public function add(Request $req) {
  3. $data['pid'] = (int)$req->pid;
  4. return view('admins/menus/add', $data);
  5. }
  6. //保存菜单
  7. public function save(Request $req) {
  8. $data['pid'] = (int)$req->pid;
  9. $data['title'] = $req->title;
  10. $data['controller'] = $req->controller ? $req->controller : '';
  11. $data['action'] = $req->action ? $req->action : '';
  12. $data['ishidden'] = (int)$req->ishidden;
  13. $data['status'] = (int)$req->status;
  14. if ($data['title'] == '') {
  15. return json_encode(array('code' => 1, 'msg' => '菜单名称不能为空'));
  16. }
  17. DB::table('admin_menu')->insert($data);
  18. return json_encode(array('code' => 0, 'msg' => '保存成功'));
  19. }


3、添加与保存菜单 注册 路由
routes/web.php

  1. //添加菜单 处理
  2. Route::get('/admins/menus/add', [Menus::class, 'add']);
  3. //保存菜单 处理
  4. Route::post('/admins/menus/save', [Menus::class, 'save']);

4、查看效果

三、编辑与保存编辑菜单 模块

1、创建 编辑与保存编辑菜单 视图模板

resources/views/admins/menus/edit.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>编辑菜单</title>
  5. <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
  6. <script type="text/javascript" src="/static/layui/layui.js"></script>
  7. </head>
  8. <body style="padding: 10px;">
  9. <div class="layui-form">
  10. @csrf
  11. <input type="hidden" name="mid" value="{{$item['mid']}}">
  12. <div class="layui-form-item">
  13. <label class="layui-form-label">菜单名称</label>
  14. <div class="layui-input-block">
  15. <input type="text" name="title" class="layui-input" value="{{$item['title']}}">
  16. </div>
  17. </div>
  18. <div class="layui-form-item">
  19. <label class="layui-form-label">controller</label>
  20. <div class="layui-input-block">
  21. <input type="text" name="controller" class="layui-input" value="{{$item['controller']}}">
  22. </div>
  23. </div>
  24. <div class="layui-form-item">
  25. <label class="layui-form-label">action</label>
  26. <div class="layui-input-block">
  27. <input type="text" name="action" class="layui-input" value="{{$item['action']}}">
  28. </div>
  29. </div>
  30. <div class="layui-form-item">
  31. <label class="layui-form-label">状态</label>
  32. <div class="layui-input-block">
  33. <input type="checkbox" name="ishidden" lay-skin="primary" title="隐藏" {{$item['ishidden']==1?'checked':''}}>
  34. <input type="checkbox" name="status" lay-skin="primary" title="禁用" {{$item['status']==1?'checked':''}}>
  35. </div>
  36. </div>
  37. <div class="layui-input-block">
  38. <button class="layui-btn" onclick="save()">保存</button>
  39. </div>
  40. </div>
  41. </body>
  42. </html>
  43. <script type="text/javascript">
  44. layui.use(['form'], function () {
  45. $ = layui.jquery;
  46. form = layui.form;
  47. layer = layui.layer;
  48. });
  49. //保存
  50. function save() {
  51. var mid = $('input[name="mid"]').val();
  52. var title = $('input[name="title"]').val();
  53. var controller = $('input[name="controller"]').val();
  54. var action = $('input[name="action"]').val();
  55. var ishidden = $('input[name="ishidden"]').is(':checked') ? 1 : 0;
  56. var status = $('input[name="status"]').is(':checked') ? 1 : 0;
  57. var _token = $('input[name="_token"]').val();
  58. if (title == '') {
  59. return layer.alert('请填写菜单名称', {icon: 2});
  60. }
  61. $.post('/admins/menus/save_edit', {
  62. mid: mid,
  63. title: title,
  64. controller: controller,
  65. action: action,
  66. ishidden: ishidden,
  67. status: status,
  68. _token: _token
  69. }, function (res) {
  70. if (res.code > 0) {
  71. return layer.alert(res.msg, {icon: 2});
  72. }
  73. layer.msg(res.msg);
  74. setTimeout(function () {
  75. parent.window.location.reload();
  76. }, 1000);
  77. }, 'json');
  78. }
  79. </script>

2、编辑与保存编辑菜单 控制器处理
app/Http/Controllers/admins/Menus.php

  1. //编辑菜单
  2. public function edit(Request $req) {
  3. $mid = (int)$req->mid;
  4. $data['item'] = DB::table('admin_menu')->where('mid', $mid)->item();
  5. return view('admins/menus/edit', $data);
  6. }
  7. // 保存修改
  8. public function save_edit(Request $req) {
  9. $mid = (int)$req->mid;
  10. $data['title'] = $req->title;
  11. $data['controller'] = $req->controller ? $req->controller : '';
  12. $data['action'] = $req->action ? $req->action : '';
  13. $data['ishidden'] = (int)$req->ishidden;
  14. $data['status'] = (int)$req->status;
  15. if ($data['title'] == '') {
  16. return json_encode(array('code' => 1, 'msg' => '菜单名称不能为空'));
  17. }
  18. DB::table('admin_menu')->where('mid', $mid)->update($data);
  19. return json_encode(array('code' => 0, 'msg' => '保存成功'));
  20. }


3、编辑与保存编辑菜单 注册路由
routes/web.php

  1. //编辑菜单 窗口
  2. Route::get('/admins/menus/edit',[Menus::class, 'edit']);
  3. //保存编辑菜单 处理
  4. Route::post('/admins/menus/save_edit',[Menus::class, 'save_edit']);


4、查看效果

四、删除菜单 模块

1、删除菜单 控制器处理
app/Http/Controllers/admins/Menus.php

  1. // 删除菜单
  2. public function del(Request $req) {
  3. $mid = (int)$req->mid;
  4. DB::table('admin_menu')->where('mid', $mid)->update(['isdel' => 1]);
  5. return json_encode(array('code' => 0, 'msg' => '删除成功'));
  6. }

2、删除菜单 注册路由

  1. //删除菜单 处理
  2. Route::get('/admins/menus/del',[Menus::class, 'del']);


3、查看效果

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议