abstract:添加和编辑页面<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name=&quo
添加和编辑页面
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>添加菜单</title> <script type="text/javascript" src="/static/layui/layui.js"></script> <link rel="stylesheet" href="/static/layui/css/layui.css"> </head> <body> <div style="margin-top: 15px;"> <form action="" class="layui-form"> <input type="hidden" name="pid" value="{$pid}"> <input type="hidden" name="menu_id" value="{$menu_id}"> <div class="layui-form-item"> <label for="ord" class="layui-form-label">排序</label> <div class="layui-input-inline"> <input type="text" name="ord" class="layui-input" id="ord" value="{$item.ord}"> </div> </div> {if $parent_menu} <div class="layui-form-item"> <label class="layui-form-label">上级菜单</label> <div class="layui-input-inline"> <input type="text" class="layui-input" value="{$parent_menu.title}" readonly> </div> </div> {/if} <div class="layui-form-item"> <label for="title" class="layui-form-label">菜单名称</label> <div class="layui-input-inline"> <input type="text" name="title" class="layui-input" id="title" value="{$item.title}"> </div> </div> <div class="layui-form-item"> <label for="controller" class="layui-form-label">控制器</label> <div class="layui-input-inline"> <input type="text" name="controller" class="layui-input" id="controller" value="{$item.controller}"> </div> </div> <div class="layui-form-item"> <label for="method" class="layui-form-label">方法</label> <div class="layui-input-inline"> <input type="text" name="method" class="layui-input" id="method" value="{$item.method}"> </div> </div> <div class="layui-form-item"> <label for="status" class="layui-form-label">状态</label> <div class="layui-input-inline"> <input type="checkbox" name="status" lay-skin="primary" id="status" title="禁用" value="1" {$item.status==1?'checked':''}> <input type="checkbox" name="ishidden" lay-skin="primary" id="ishidden" title="隐藏" value="1" {$item.ishidden==1?'checked':''}> </div> </div> </form> <div class="layui-form-item"> <div class="layui-input-block"> <button class="layui-btn" onclick="save()">保存</button> </div> </div> </div> <script type="text/javascript"> layui.use(['layer','form'],function () { var form = layui.form; layer = layui.layer; $ = layui.jquery; }) function save(){ var title = $.trim($('#title').val()); var controller = $.trim($('#controller').val()); var method = $.trim($('#method').val()); if(title == ''){ layer.alert('菜单名不能为空',{icon:2}); return; } if(controller == ''){ layer.alert('控制器名不能为空',{icon:2}); return; } if(method == ''){ layer.alert('方法名不能为空',{icon:2}); return; } $.post("{:url('doAdd')}",$("form").serialize(),function (res) { if(res.code>0){ layer.alert(res.msg); }else{ layer.msg(res.msg); setTimeout(function(){ parent.window.location.reload(); },1000) } }) } </script> </body> </html>
控制器
<?php namespace app\admins\controller; use app\admins\controller\Base; class Menu extends Base { public function index() { $pid = (int)input('get.pid'); $data['pid'] = $pid; $lists = $this->db->table('menus')->order('ord asc')->where($data)->lists(); if($pid>0){ $parent = $this->db->table('menus')->where(['menu_id'=>$pid])->item(); $backid = $parent['pid']; $this->assign('backid',$backid); } $this->assign('pid',$pid); $this->assign('lists',$lists); return $this->fetch(); } public function add() { $pid = (int)input('get.pid'); $menu_id = (int)input('get.menu_id'); $parent_menu = $this->db->table('menus')->where(['menu_id'=>$pid])->item(); $item = $this->db->table('menus')->where(['menu_id'=>$menu_id])->item(); $this->assign('parent_menu',$parent_menu); $this->assign('pid',$pid); $this->assign('item',$item); $this->assign('menu_id',$menu_id); return $this->fetch(); } public function doAdd() { $menu_id = (int)input('post.menu_id'); $data['pid'] = (int)input('post.pid'); $data['ord'] = (int)input('post.ord'); $data['title'] = trim(input('post.title')); $data['controller'] = trim(input('post.controller')); $data['method'] = trim(input('post.method')); $data['status'] = (int)input('post.status'); $data['ishidden'] = (int)input('post.ishidden'); if($data['title'] == ''){ return ['code'=>1,'msg'=>'菜单名称不能为空']; } if($data['controller'] == ''){ return ['code'=>1,'msg'=>'控制器名称不能为空']; } if($data['method'] == ''){ return ['code'=>1,'msg'=>'方法名称不能为空']; } $res = true; if($menu_id){ $this->db->table('menus')->where(['menu_id'=>$menu_id])->update($data); }else{ $res = $this->db->table('menus')->insert($data); } if($res){ return ['code'=>0,'msg'=>'保存成功']; }else{ return ['code'=>1,'msg'=>'保存失败']; } } public function delete() { $menu_id = (int)input('get.menu_id'); if($this->db->table('menus')->delete($menu_id)){ return ['code'=>0,'msg'=>'删除成功']; }else{ return ['code'=>1,'msg'=>'删除失败']; } } }
Correcting teacher:韦小宝Correction time:2019-03-01 14:38:03
Teacher's summary:写的很不错 没有毛病 代码整体写的也很规范