博客列表 >【记账后台管理系统】用户列表、用户日志列表渲染、记账分类动态列表渲染等增删改查功能开发

【记账后台管理系统】用户列表、用户日志列表渲染、记账分类动态列表渲染等增删改查功能开发

 一纸荒凉* Armani
 一纸荒凉* Armani原创
2021年06月04日 19:24:341352浏览

记账后台管理系统实战开发


参考微信小程序墨子记账

后台页面采用layui_admin实现效果

制作记账数据库

  • 主键:代表唯一的
  • 自动递增:新增加数据时,必须有变化,就要自动增加
  • 无符号:不能为负数
  • 用户表 :uid、img(头像)、name(昵称)、status(状态)
  • 分类表 :ID、name(分类名称)、img(未选中图标)、img_s(选中后图标)、status(状态)
  • 记账表 :id、uid(用户)、type(类型)、money(金额)、cat(分类)、date(时间)、remarks(备注)

用户表 jz_user

字段名 类型 备注
uid int (10) 用户id
openid varchar(100) 微信唯一id
nickname varchar(200) 昵称
avatar varchar(300) 头像
province varchar(50) 省份
city varchar(50) 城市
sex tinyint 性别 1男 2女 0未知
status tinyint 状态 1开启 0关闭
money float 月预算
add_time int 注册时间
last_time int 最后登陆时间

账目明细表 jz_list

字段名 类型 备注
id int (10) 记账ID
uid int (10) 用户ID
cid int (10) 分类ID
date date 账目日期
type tinyint 类型 1支出 2收入
money float 金额
add_time int 记账时间
remarks varchar 备注

账目分类表

字段名 类型 备注
id int (10) 分类ID
name varchar 分类名
type tinyint 类型 1支出 2收入
img varchar 分类图
img_s varchar 分类选中图
status tinyint 状态 1开启 0关闭
sort int 排序

在后台新建记账系统导航子菜单分类管理和用户管理

其中内部代码中输入我们的控制器名称/操作方法

点击系统配置\部门管理开启权限

代码中app\bews\controller\Jizhang.php 控制器

  1. <?php
  2. namespace app\bews\controller;
  3. use app\bews\controller\Base;
  4. use think\facade\Db;
  5. use think\facade\View;
  6. use think\facade\Request;
  7. class Jizhang extends Base{
  8. // 渲染分类管理页面
  9. public function cat_list(){
  10. return View::fetch();
  11. }
  12. // 渲染用户管理页面
  13. public function user_list(){
  14. return View::fetch();
  15. }
  16. }
  17. ?>

app\bews\view\Jizhang\cat_list.php 分类列表视图

  1. {include file="public/header" /}
  2. <div style="text-align: center;color:gray;">
  3. <h1>账目分类</h1>
  4. </div>
  5. {include file="public/tail" /}

app\bews\view\Jizhang\user_list.php 用户列表视图

  1. {include file="public/header" /}
  2. <div style="text-align: center;color:gray;">
  3. <h1>记账用户</h1>
  4. </div>
  5. {include file="public/tail" /}


渲染用户管理页面列表

Jizhang.php 控制器

  1. <?php
  2. namespace app\bews\controller;
  3. use app\bews\controller\Base;
  4. use think\facade\Db;
  5. use think\facade\View;
  6. use think\facade\Request;
  7. class Jizhang extends Base{
  8. // 渲染分类管理页面
  9. public function cat_list(){
  10. return View::fetch();
  11. }
  12. // 渲染用户管理页面
  13. public function user_list(){
  14. $select = Db::name('oyk_user')->select();
  15. print_r($select);
  16. View::assign([
  17. 'select'=>$select
  18. ]);
  19. return View::fetch();
  20. }
  21. }
  22. ?>

用户管理列表

页面视图 user_list.php

  1. {include file="public/header" /}
  2. <div class="header">
  3. <span>用户管理列表</span>
  4. <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" onclick="add()">
  5. <i class="layui-icon layui-icon-add-1"></i>添加
  6. </button>
  7. <div></div>
  8. </div>
  9. <table class="layui-table">
  10. <thead>
  11. <tr>
  12. <th>ID</th>
  13. <th>昵称</th>
  14. <th>头像</th>
  15. <th>地区</th>
  16. <th>性别</th>
  17. <th>状态</th>
  18. <th>注册时间</th>
  19. <th>操作</th>
  20. </tr>
  21. </thead>
  22. <tbody>
  23. {volist name="select" id='vo'}
  24. <tr>
  25. <td>{$vo['uid']}</td>
  26. <td>{$vo['nickname']}</td>
  27. <td>
  28. <img style="width:30px;height:30px;" onmouseover="show_img(this)" onmouseleave="hide_img()" src="{$vo['avatar']}">
  29. </td>
  30. <td>{$vo['province']}-{$vo['city']}</td>
  31. <td>
  32. {if($vo['sex'] == 1)}
  33. <span style="color:green;"></span>
  34. {elseif($vo['sex'] == 2)/}
  35. <span style="color:red;"></span>
  36. {else /}
  37. <span style="color:grey;">未知</span>
  38. {/if}
  39. </td>
  40. <td>
  41. {if($vo['status'] == 1)}
  42. <span style="color:green;">开启</span>
  43. {else/}
  44. <span style="color:red;">关闭</span>
  45. {/if}
  46. </td>
  47. <td>{:date('Y-m-d H:i:s',$vo.add_time)}</td>
  48. <td>
  49. <button type="button" class="layui-btn layui-btn-primary layui-btn-xs" onclick="edit({$vo.uid})">
  50. <i class="layui-icon layui-icon-edit"></i>编辑
  51. </button>
  52. <button type="button" class="layui-btn layui-btn-primary layui-btn-xs" onclick="del({$vo['uid']})">
  53. <i class="layui-icon layui-icon-delete"></i>删除
  54. </button>
  55. </td>
  56. </tr>
  57. {/volist}
  58. </tbody>
  59. </table>
  60. {include file="public/tail" /}

添加用户

Jizhang.php 控制器 user_add()方法

  1. public function user_add(){
  2. if(Request::isPost()){
  3. $data = Request::post();
  4. $data['add_time'] = time();
  5. if(empty(Request::post('nickname'))){
  6. echo json_encode(['code'=>1,'msg'=>'昵称不能为空']);
  7. die;
  8. }
  9. $res = Db::name('oyk_user')->insert($data);
  10. if(empty($res)){
  11. echo json_encode(['code'=>1,'msg'=>'添加失败,请重试……']);
  12. }else{
  13. echo json_encode(['code'=>0,'msg'=>'添加成功!']);
  14. }
  15. }else{
  16. return View::fetch();
  17. }
  18. }

user_list.php 页面点击添加按钮事件

  1. <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" onclick="add()">
  2. <i class="layui-icon layui-icon-add-1"></i>添加
  3. </button>
  4. <script>
  5. layui.use(['layer'],function(){
  6. layer = layui.layer;
  7. $ = layui.jquery;
  8. });
  9. // 添加
  10. function add(){
  11. layer.open({
  12. type: 2,
  13. title: '添加',
  14. shade: 0.3,
  15. maxmin: true,
  16. area: ['450px','550px'],
  17. content: "{$config['admin_route']}Jizhang/user_add"
  18. });
  19. }
  20. </script>

Jizhang/user_add.php视图

  1. {include file="public/header" /}
  2. <form class="layui-form">
  3. <div class="layui-form-item">
  4. <label class="layui-form-label">openid</label>
  5. <div class="layui-input-block">
  6. <input type="text" class="layui-input" id="openid" name="openid" placeholder="请输入微信openid">
  7. </div>
  8. </div>
  9. <div class="layui-form-item">
  10. <label class="layui-form-label">昵称</label>
  11. <div class="layui-input-block">
  12. <input class="layui-input" id="nickname" name="nickname" placeholder="请输入昵称">
  13. </div>
  14. </div>
  15. <div class="layui-form-item">
  16. <label class="layui-form-label">头像</label>
  17. <div class="layui-input-inline">
  18. <button type="button" class="layui-btn layui-btn-fluid" id="up_avatar">上传图片</button>
  19. </div>
  20. <div class="layui-form-mid layui-word-aux">支持png,jpg,gif</div>
  21. </div>
  22. <div class="layui-form-item">
  23. <label class="layui-form-label">头像预览</label>
  24. <div class="layui-input-block">
  25. <div class="layui-upload-list" id="avatar_img" style="width:90%;float:left;"></div>
  26. </div>
  27. </div>
  28. <div class="layui-form-item">
  29. <label class="layui-form-label">省份</label>
  30. <div class="layui-input-block">
  31. <input type="text" class="layui-input" id="province" name="province" placeholder="请输入省份">
  32. </div>
  33. </div>
  34. <div class="layui-form-item">
  35. <label class="layui-form-label">城市</label>
  36. <div class="layui-input-block">
  37. <input type="text" class="layui-input" id="city" name="city" placeholder="请输入城市">
  38. </div>
  39. </div>
  40. <div class="layui-form-item">
  41. <label class="layui-form-label">性别</label>
  42. <div class="layui-input-block">
  43. <select id="sex" name="sex">
  44. <option value="1" selected>男</option>
  45. <option value="2">女</option>
  46. </select>
  47. </div>
  48. </div>
  49. <div class="layui-form-item">
  50. <label class="layui-form-label">状态</label>
  51. <div class="layui-input-block">
  52. <select id="status" name="status">
  53. <option value="1" selected>开启</option>
  54. <option value="2">关闭</option>
  55. </select>
  56. </div>
  57. </div>
  58. </form>
  59. <div class="layui-form-item" style="margin-top:10px;">
  60. <div class="layui-input-block">
  61. <button type="button" class="layui-btn" onclick="save()">保存</button>
  62. </div>
  63. </div>
  64. <script type="text/javascript">
  65. // 图片上传
  66. layui.use(['layer','form','upload','laydate'],function(){
  67. layer = layui.layer;
  68. $ = layui.jquery;
  69. upload = layui.upload;
  70. upload.render({
  71. elem: "#up_avatar",
  72. url: "{$config['admin_route']}index/upload_img",
  73. multiple: true,
  74. done: function(res,title){
  75. if(res.code > 0){
  76. return layer.msg("上传失败,"+res.msg);
  77. }else{
  78. $("#avatar_img").append('<div class="upload_pic_li" style="position:relative; width:100px; float:left; margin:0 10px 10px 0"><img style="width:100px;height:100px;" src="'+ res.url +'" class="layui-upload-img" onmouseover="show_img(this)" onmouseleave="hide_img()"><input type="hidden" name="avatar" value="'+res.data+'"/><a class="close-img" href="javascript:;" onclick="deleteImage(\''+res.data+'\',this);return false;"></a></div>');
  79. }
  80. }
  81. });
  82. });
  83. // 保存
  84. function save(){
  85. $.post("{$config['admin_route']}Jizhang/user_add",$('form').serialize(),function(res){
  86. if(res.code>0){
  87. layer.msg(res.msg,{'icon':2});
  88. }else{
  89. layer.msg(res.msg,{'icon':1});
  90. setTimeout(function(){parent.window.location.reload();},2000);
  91. }
  92. },'json');
  93. }
  94. </script>
  95. {include file="public/tail" /}

编辑用户

Jizhang.php 控制器

  1. public function user_edit(){
  2. if(Request::isPost()){
  3. $data = Request::post();
  4. $res = Db::name('oyk_user')->update($data);
  5. if(empty($res)){
  6. echo json_encode(['code'=>1,'msg'=>'修改失败,请重试……']);
  7. }else{
  8. echo json_encode(['code'=>0,'msg'=>'修改成功!']);
  9. }
  10. }else{
  11. $id = Request::get('uid');
  12. $res = Db::table('oyk_user')->where(['uid'=>$id])->select();
  13. View::assign([
  14. 'res'=> $res['0']
  15. ]);
  16. return View::fetch();
  17. }
  18. }

user_list.php 页面点击编辑按钮事件

  1. <button type="button" class="layui-btn layui-btn-primary layui-btn-xs" onclick="edit({$vo.uid})">
  2. <i class="layui-icon layui-icon-edit"></i>编辑
  3. </button>
  4. <script>
  5. // 编辑
  6. function edit(uid){
  7. layer.open({
  8. type: 2,
  9. title: '编辑',
  10. shade: 0.3,
  11. area: ['450px','550px'],
  12. content: "{$config['admin_route']}Jizhang/user_edit?uid="+uid
  13. });
  14. }
  15. </script>

Jizhang/user_edit.php视图

  1. {include file="public/header" /}
  2. <form class="layui-form">
  3. <div class="layui-form-item" hidden>
  4. <label class="layui-form-label">ID</label>
  5. <div class="layui-input-block">
  6. <input class="layui-input" id="uid" name="uid" value="{$res['uid']}" readonly>
  7. </div>
  8. </div>
  9. <div class="layui-form-item">
  10. <label class="layui-form-label">昵称</label>
  11. <div class="layui-input-block">
  12. <input class="layui-input" id="nickname" name="nickname" value="{$res['nickname']}" placeholder="请输入昵称">
  13. </div>
  14. </div>
  15. <div class="layui-form-item">
  16. <label class="layui-form-label">头像预览</label>
  17. <div class="layui-input-block">
  18. <div class="layui-upload-list" id="avatar_img" style="width:90%;float:left;">
  19. <div class="upload_pic_li" style="position:relative; width:100px; float:left; margin:0 10px 10px 0">
  20. <img style="width:100px;height:100px;" src="{$res['avatar']}" class="layui-upload-img" onmouseover="show_img(this)" onmouseleave="hide_img()">
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. <div class="layui-form-item">
  26. <label class="layui-form-label">省份</label>
  27. <div class="layui-input-block">
  28. <input type="text" class="layui-input" id="province" name="province" value="{$res['province']}" placeholder="请输入省份">
  29. </div>
  30. </div>
  31. <div class="layui-form-item">
  32. <label class="layui-form-label">城市</label>
  33. <div class="layui-input-block">
  34. <input type="text" class="layui-input" id="city" name="city" value="{$res['city']}" placeholder="请输入城市">
  35. </div>
  36. </div>
  37. <div class="layui-form-item">
  38. <label class="layui-form-label">性别</label>
  39. <div class="layui-input-block">
  40. <select id="sex" name="sex">
  41. <option value="1" {if $res['sex']==1}selected{/if} >男</option>
  42. <option value="2" {if $res['sex']==2}selected{/if} >女</option>
  43. </select>
  44. </div>
  45. </div>
  46. <div class="layui-form-item">
  47. <label class="layui-form-label">状态</label>
  48. <div class="layui-input-block">
  49. <select id="status" name="status">
  50. <option value="1" {if $res['status']==1}selected{/if} >开启</option>
  51. <option value="2" {if $res['status']==2}selected{/if} >关闭</option>
  52. </select>
  53. </div>
  54. </div>
  55. </form>
  56. <div class="layui-form-item" style="margin-top:10px;">
  57. <div class="layui-input-block">
  58. <button type="button" class="layui-btn" onclick="save()">保存</button>
  59. </div>
  60. </div>
  61. <script type="text/javascript">
  62. // 图片上传
  63. layui.use(['layer','form'],function(){
  64. layer = layui.layer;
  65. $ = layui.jquery;
  66. });
  67. // 保存
  68. function save(){
  69. $.post("{$config['admin_route']}Jizhang/user_edit",$('form').serialize(),function(res){
  70. if(res.code>0){
  71. layer.msg(res.msg,{'icon':2});
  72. }else{
  73. layer.msg(res.msg,{'icon':1});
  74. setTimeout(function(){parent.window.location.reload();},2000);
  75. }
  76. },'json');
  77. }
  78. </script>
  79. {include file="public/tail" /}

删除用户

Jizhang.php 控制器

  1. public function user_del(){
  2. if(Request::isPost()){
  3. $data = Request::post('uid');
  4. $res = Db::name('oyk_user')->delete($data);
  5. if(empty($res)){
  6. echo json_encode(['code'=>1,'msg'=>'删除失败,请重试……']);
  7. }else{
  8. echo json_encode(['code'=>0,'msg'=>'删除成功!']);
  9. }
  10. }
  11. }

user_list.php 页面点击删除按钮事件

  1. <button type="button" class="layui-btn layui-btn-primary layui-btn-xs" onclick="del({$vo['uid']})">
  2. <i class="layui-icon layui-icon-delete"></i>删除
  3. </button>
  4. <script>
  5. // 删除
  6. function del(uid){
  7. layer.confirm('确定要删除吗?', {
  8. icon:3,
  9. btn: ['确定','取消']
  10. }, function(){
  11. $.post("{$config['admin_route']}Jizhang/user_del",{'uid':uid},function(res){
  12. if(res.code>0){
  13. layer.alert(res.msg,{icon:2});
  14. }else{
  15. layer.msg(res.msg,{icon:1});
  16. setTimeout(function(){window.location.reload();},2000);
  17. }
  18. },'json');
  19. });
  20. }
  21. </script>

账目列表

Jizhang.php 控制器

  1. public function jz_list(){
  2. $uid = (int)input('get.uid');
  3. $find = Db::table('oyk_user')->where('uid',$uid)->find();
  4. $value = trim(input('value'));
  5. if(!empty($value)){
  6. $where[] = ['date','=',$value];
  7. }
  8. $where[] = ['uid','=',$uid];
  9. $lists = Db::table('oyk_jz j')
  10. ->join(['oyk_cat'=>'c'],'j.cid=c.id')
  11. ->where($where)
  12. ->order('j.date DESC,j.add_time DESC')
  13. ->paginate([
  14. 'list_rows'=> $this->config['admin_page'],
  15. 'query' => Request::param()
  16. ]);
  17. View::assign([
  18. 'value' => $value,
  19. 'find' => $find,
  20. 'lists' => $lists
  21. ]);
  22. return View::fetch();
  23. }

user_list.php 页面点击账目列表按钮事件

  1. <button type="button" class="layui-btn layui-btn-primary layui-btn-xs" onclick="jz({$vo.uid})">
  2. <i class="layui-icon layui-icon-form"></i>账目列表
  3. </button>
  4. <script>
  5. // 账目列表
  6. function jz(uid){
  7. window.location.href="{$config['admin_route']}Jizhang/jz_list?uid="+uid;
  8. }
  9. </script>

Jizhang/jz_list.php视图

  1. {include file="public/header" /}
  2. <div class="header">
  3. <a href="{$config['admin_route']}Jizhang/user_list"><span style="background:#999;">用户列表</span></a>
  4. <span>{$find['nickname']} - 账单</span>
  5. <div></div>
  6. </div>
  7. <!-- 搜索框 -->
  8. <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist" style="height:100%;margin-top:20px;">
  9. <div class="layui-form-item">
  10. <div class="layui-input-inline">
  11. <input type="date" id="value" name="value" placeholder="清选择日期,进行筛选" class="layui-input" value="{$value}" />
  12. </div>
  13. <button class="layui-btn layui-btn-sm" data-type="reload" id="search">
  14. <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>搜索
  15. </button>
  16. </div>
  17. </div>
  18. <!-- 搜索框 -->
  19. <table class="layui-table">
  20. <thead>
  21. <tr>
  22. <th>日期</th>
  23. <th>分类</th>
  24. <th>金额</th>
  25. <th>记账时间</th>
  26. <th>备注</th>
  27. </tr>
  28. </thead>
  29. <tbody>
  30. {volist name="lists" id='vo'}
  31. <tr>
  32. <td>{$vo['date']}</td>
  33. <td>{$vo['name']}</td>
  34. <td>
  35. {if($vo['type'] == 1)}
  36. <span style="color:red;">-</span>
  37. {else/}
  38. <span style="color:green;">-</span>
  39. {/if}
  40. {$vo['money']}
  41. </td>
  42. <td>{:date('Y-m-d',$vo['add_time'])}</td>
  43. <td>{$vo['remarks']}</td>
  44. </tr>
  45. {/volist}
  46. </tbody>
  47. </table>
  48. <div>{$lists|raw}</div>
  49. <script type="text/javascript">
  50. layui.use(['layer'],function(){
  51. layer = layui.layer;
  52. $ = layui.jquery;
  53. $('#search').on('click',function(){
  54. var value = $("#value").val();
  55. location.href = "{$config['admin_route']}Jizhang/jz_list?value="+value+"&uid="+{$find['uid']};
  56. });
  57. });
  58. </script>
  59. {include file="public/tail" /}

分类管理列表

分类管理列表动态表格渲染

页面视图 cat_list.php

  1. {include file="public/header" /}
  2. <div class="header">
  3. <span>分类列表动态渲染</span>
  4. <div></div>
  5. </div>
  6. <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist" style="height:100%;">
  7. <div class="layui-form-item" id="search"></div>
  8. </div>
  9. <script type="text/html" id="toolbar">
  10. <div class="layui-btn-container">
  11. <div class="layui-btn-group">
  12. <button type="button" class="layui-btn layui-btn-primary" lay-event="add">
  13. <i class="layui-icon layui-icon-add-1"></i>添加
  14. </button>
  15. <button type="button" class="layui-btn layui-btn-primary" lay-event="edit">
  16. <i class="layui-icon layui-icon-edit"></i>修改
  17. </button>
  18. <button type="button" class="layui-btn layui-btn-primary" lay-event="del">
  19. <i class="layui-icon layui-icon-delete"></i>删除
  20. </button>
  21. </div>
  22. </div>
  23. </script>
  24. <table class="layui-hide" id="datalist" lay-filter="datalist"></table>
  25. <script type="text/javascript">
  26. layui.use(['layer','table'],function(){
  27. table = layui.table;
  28. layer = layui.layer;
  29. $ = layui.jquery;
  30. get_data();
  31. table.on('row(datalist)',function(obj){
  32. obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
  33. obj.tr.find("div.layui-unselect.layui-form-radio")[0].click();
  34. });
  35. table.on('toolbar(datalist)',function(obj){
  36. var checkStatus = table.checkStatus(obj.config.id);
  37. var data = checkStatus.data; //获取选中行数据
  38. var datas = obj.data; //获得当前行数据
  39. var layEvent = obj.event; //获得 lay-event 对应的值(也可以是表头的 event 参数对应的值)
  40. var tr = obj.tr; //获得当前行 tr 的 DOM 对象(如果有的话)
  41. switch(obj.event){
  42. case 'add':
  43. layer.open({
  44. title: '添加分类',
  45. type: 2,
  46. content: "{$config['admin_route']}Jizhang/cat_add",
  47. maxmin: true,
  48. area: ['560px','80%'],
  49. btn: ['确定','关闭'],
  50. yes: function(index,layero){
  51. // 方法一
  52. var form = layero.find('iframe')[0].contentWindow;
  53. // 方法二
  54. var body = layer.getChildFrame('body', index);
  55. var img = '';
  56. body.find("input[name='img']").each(function(){
  57. img = $(this).val();
  58. })
  59. var img_s = '';
  60. body.find("input[name='img_s']").each(function(){
  61. img_s = $(this).val();
  62. })
  63. var dataj = {
  64. name : form.uname.value,
  65. type : form.type.value,
  66. img : img,
  67. img_s : img_s,
  68. status : form.test_status.value,
  69. sort : form.test_sort.value
  70. };
  71. $.post('{$config["admin_route"]}Jizhang/cat_add',dataj,function(res){
  72. if(res.code>0){
  73. layer.msg(res.msg,{'icon':2});
  74. }else{
  75. layer.msg(res.msg,{'icon':1});
  76. get_data();
  77. layer.close(index);
  78. }
  79. },'json');
  80. }
  81. });
  82. break;
  83. case 'edit':
  84. if(!data[0]){
  85. layer.msg('请选择一条数据',{'icon':2});
  86. return false;
  87. }
  88. layer.full(layer.open({
  89. title: '修改分类',
  90. type: 2,
  91. content: "{$config['admin_route']}Jizhang/cat_edit?id="+data[0].id,
  92. maxmin: true,
  93. area: ['560px','80%'],
  94. btn: ['确定','关闭'],
  95. yes: function(index,layero){
  96. // 方法一
  97. var form = layero.find('iframe')[0].contentWindow;
  98. // 方法二
  99. var body = layer.getChildFrame('body', index);
  100. var img = '';
  101. body.find("input[name='img']").each(function(){
  102. img = $(this).val();
  103. })
  104. var img_s = '';
  105. body.find("input[name='img_s']").each(function(){
  106. img_s = $(this).val();
  107. })
  108. var dataj = {
  109. id: form.cat_id.value,
  110. name : form.uname.value,
  111. type : form.type.value,
  112. img : img,
  113. img_s : img_s,
  114. status : form.test_status.value,
  115. sort : form.test_sort.value
  116. };
  117. $.post('{$config["admin_route"]}Jizhang/cat_edit',dataj,function(res){
  118. if(res.code>0){
  119. layer.msg(res.msg,{'icon':2});
  120. }else{
  121. layer.msg(res.msg,{'icon':1});
  122. get_data();
  123. layer.close(index);
  124. }
  125. },'json');
  126. }
  127. }))
  128. break;
  129. case 'del':
  130. if(!data[0]){
  131. layer.msg('请选择一条数据',{'icon':2});
  132. return false;
  133. }
  134. layer.confirm('确定要删除吗?',{
  135. icon:3,
  136. btn: ['确定','取消']
  137. },function(){
  138. $.post('{$config["admin_route"]}Jizhang/cat_del',{'id':data[0].id},function(res){
  139. if(res.code>0){
  140. layer.alert(res.msg,{icon:2});
  141. }else{
  142. layer.msg(res.msg);
  143. setTimeout(function(){window.location.reload();},1000);
  144. }
  145. },'json');
  146. });
  147. break;
  148. };
  149. });
  150. function get_data(){
  151. table.render({
  152. elem: '#datalist',
  153. method : 'post'
  154. ,url:"{$config['admin_route']}Jizhang/cat_list"
  155. ,toolbar: '#toolbar'
  156. ,defaultToolbar: ['filter', 'print', 'exports', {
  157. title: '提示' //标题
  158. ,layEvent: 'LAYTABLE_TIPS' //事件名,用于 toolbar 事件中使用
  159. ,icon: 'layui-icon-tips' //图标类名
  160. }]
  161. ,title: '分类列表'
  162. ,page: true
  163. ,cols: [[
  164. {type:'radio'}
  165. ,{field:'sort', width:100, title:'排序'}
  166. ,{field:'id', width:100, title:'ID'}
  167. ,{field:'name', width:260, title: '分类名'}
  168. ,{field:'type', width:100, title: '类型',templet:function(res){
  169. if(res.type == 1){
  170. return '<span style="color:green;">支出</span>';
  171. }else{
  172. return '<span style="color:grey;">收入</span>';
  173. }
  174. }}
  175. ,{field:'img', width:260, title: '默认图片',templet:function(res){
  176. return '<img style="width:30px;height:30px;" onmouseover="show_img(this)" onmouseleave="hide_img()" src="'+res.img+'"';
  177. }}
  178. ,{field:'img_s', width:260, title: '选中图片',templet:function(res){
  179. return '<img style="width:30px;height:30px;" onmouseover="show_img(this)" onmouseleave="hide_img()" src="'+res.img_s+'"';
  180. }}
  181. ,{field:'status', width:160, title: '状态',templet:function(res){
  182. if(res.status == 1){
  183. return '<span style="color:green;">开启</span>';
  184. }else{
  185. return '<span style="color:grey;">关闭</span>';
  186. }
  187. }}
  188. ]]
  189. ,data:[]
  190. ,limit :10
  191. ,limits:[10,15,20]
  192. ,height: 'full-100'
  193. });
  194. }
  195. });
  196. </script>
  197. {include file="public/tail" /}

Jizhang.php 控制器 cat_list()方法

  1. public function cat_list(){
  2. if(Request::isPost()){
  3. $lists = Db::table('oyk_cat')->order('sort DESC')->select();
  4. $count = Db::table('oyk_cat')->count();
  5. $page = (int)input('post.page',1);
  6. $limit = (int)input('post.limit',10);
  7. $lists = Db::table('oyk_cat')->order('sort DESC')->page($page,$limit)->select();
  8. $this->returnCode(0,$lists,$count);
  9. }else{
  10. return View::fetch();
  11. }
  12. }

动态分类添加

页面视图 cat_add.php

  1. {include file="public/header" /}
  2. <div class="header">
  3. <span>分类列表动态渲染</span>
  4. <div></div>
  5. </div>
  6. <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist" style="height:100%;">
  7. <div class="layui-form-item" id="search"></div>
  8. </div>
  9. <script type="text/html" id="toolbar">
  10. <div class="layui-btn-container">
  11. <div class="layui-btn-group">
  12. <button type="button" class="layui-btn layui-btn-primary" lay-event="add">
  13. <i class="layui-icon layui-icon-add-1"></i>添加
  14. </button>
  15. <button type="button" class="layui-btn layui-btn-primary" lay-event="edit">
  16. <i class="layui-icon layui-icon-edit"></i>修改
  17. </button>
  18. <button type="button" class="layui-btn layui-btn-primary" lay-event="del">
  19. <i class="layui-icon layui-icon-delete"></i>删除
  20. </button>
  21. </div>
  22. </div>
  23. </script>
  24. <table class="layui-hide" id="datalist" lay-filter="datalist"></table>
  25. <script type="text/javascript">
  26. layui.use(['layer','table'],function(){
  27. table = layui.table;
  28. layer = layui.layer;
  29. $ = layui.jquery;
  30. get_data();
  31. table.on('row(datalist)',function(obj){
  32. obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
  33. obj.tr.find("div.layui-unselect.layui-form-radio")[0].click();
  34. });
  35. table.on('toolbar(datalist)',function(obj){
  36. var checkStatus = table.checkStatus(obj.config.id);
  37. var data = checkStatus.data; //获取选中行数据
  38. var datas = obj.data; //获得当前行数据
  39. var layEvent = obj.event; //获得 lay-event 对应的值(也可以是表头的 event 参数对应的值)
  40. var tr = obj.tr; //获得当前行 tr 的 DOM 对象(如果有的话)
  41. switch(obj.event){
  42. case 'add':
  43. layer.open({
  44. title: '添加分类',
  45. type: 2,
  46. content: "{$config['admin_route']}Jizhang/cat_add",
  47. maxmin: true,
  48. area: ['560px','80%'],
  49. btn: ['确定','关闭'],
  50. yes: function(index,layero){
  51. // 方法一
  52. var form = layero.find('iframe')[0].contentWindow;
  53. // 方法二
  54. var body = layer.getChildFrame('body', index);
  55. var img = '';
  56. body.find("input[name='img']").each(function(){
  57. img = $(this).val();
  58. })
  59. var img_s = '';
  60. body.find("input[name='img_s']").each(function(){
  61. img_s = $(this).val();
  62. })
  63. // 注意此处获取的表单元素的值是ID的名称且name和status不能用
  64. var dataj = {
  65. name : form.uname.value,
  66. type : form.type.value,
  67. img : img,
  68. img_s : img_s,
  69. status : form.test_status.value,
  70. sort : form.test_sort.value
  71. };
  72. $.post('{$config["admin_route"]}Jizhang/cat_add',dataj,function(res){
  73. if(res.code>0){
  74. layer.msg(res.msg,{'icon':2});
  75. }else{
  76. layer.msg(res.msg,{'icon':1});
  77. get_data();
  78. layer.close(index);
  79. }
  80. },'json');
  81. }
  82. });
  83. break;
  84. case 'edit':
  85. if(!data[0]){
  86. layer.msg('请选择一条数据',{'icon':2});
  87. return false;
  88. }
  89. layer.full(layer.open({
  90. title: '修改',
  91. type: 2,
  92. content: "{$config['admin_route']}bewtest/test_edit?test_id="+data[0].test_id,
  93. maxmin: true,
  94. area: ['70%','70%'],
  95. btn: ['确定','关闭'],
  96. yes: function(index,layero){
  97. // 方法一
  98. var form = layero.find('iframe')[0].contentWindow;
  99. // 方法二
  100. var body = layer.getChildFrame('body', index);
  101. var test_img =[];
  102. body.find("input[name='test_img']").each(function(){
  103. test_img.push($(this).val());
  104. })
  105. var dataj = {
  106. test_id : data[0].test_id,
  107. test_input : form.test_input.value,
  108. test_reference : form.test_reference.value,
  109. test_time : form.test_time.value,
  110. test_data : form.test_data.value,
  111. test_datatime : form.test_datatime.value,
  112. test_img : test_img,
  113. test_rich : form.test_rich.value,
  114. test_rich_baidu : form.test_rich_baidu.value,
  115. test_url : form.test_url.value
  116. };
  117. $.post('{$config["admin_route"]}bewtest/test_edit',dataj,function(res){
  118. if(res.code>0){
  119. layer.msg(res.msg,{'icon':2});
  120. }else{
  121. layer.msg(res.msg,{'icon':1});
  122. get_data();
  123. layer.close(index);
  124. }
  125. },'json');
  126. }
  127. }))
  128. break;
  129. case 'del':
  130. if(!data[0]){
  131. layer.msg('请选择一条数据',{'icon':2});
  132. return false;
  133. }
  134. layer.confirm('确定要删除吗?',{
  135. icon:3,
  136. btn: ['确定','取消']
  137. },function(){
  138. $.post('{$config["admin_route"]}bewtest/test_del',{'test_id':data[0].test_id},function(res){
  139. if(res.code>0){
  140. layer.alert(res.msg,{icon:2});
  141. }else{
  142. layer.msg(res.msg);
  143. setTimeout(function(){window.location.reload();},1000);
  144. }
  145. },'json');
  146. });
  147. break;
  148. };
  149. });
  150. function get_data(){
  151. table.render({
  152. elem: '#datalist',
  153. method : 'post'
  154. ,url:"{$config['admin_route']}Jizhang/cat_list"
  155. ,toolbar: '#toolbar'
  156. ,defaultToolbar: ['filter', 'print', 'exports', {
  157. title: '提示' //标题
  158. ,layEvent: 'LAYTABLE_TIPS' //事件名,用于 toolbar 事件中使用
  159. ,icon: 'layui-icon-tips' //图标类名
  160. }]
  161. ,title: '分类列表'
  162. ,page: true
  163. ,cols: [[
  164. {type:'radio'}
  165. ,{field:'sort', width:100, title:'排序'}
  166. ,{field:'id', width:100, title:'ID'}
  167. ,{field:'name', width:260, title: '分类名'}
  168. ,{field:'type', width:100, title: '类型',templet:function(res){
  169. if(res.type == 1){
  170. return '<span style="color:green;">支出</span>';
  171. }else{
  172. return '<span style="color:grey;">收入</span>';
  173. }
  174. }}
  175. ,{field:'img', width:260, title: '默认图片',templet:function(res){
  176. return '<img style="width:30px;height:30px;" onmouseover="show_img(this)" onmouseleave="hide_img()" src="'+res.img+'"';
  177. }}
  178. ,{field:'img_s', width:260, title: '选中图片',templet:function(res){
  179. return '<img style="width:30px;height:30px;" onmouseover="show_img(this)" onmouseleave="hide_img()" src="'+res.img_s+'"';
  180. }}
  181. ,{field:'status', width:160, title: '状态',templet:function(res){
  182. if(res.status == 1){
  183. return '<span style="color:green;">开启</span>';
  184. }else{
  185. return '<span style="color:grey;">关闭</span>';
  186. }
  187. }}
  188. ]]
  189. ,data:[]
  190. ,limit :10
  191. ,limits:[10,15,20]
  192. ,height: 'full-100'
  193. });
  194. }
  195. });
  196. </script>
  197. {include file="public/tail" /}

Jizhang.php 控制器 cat_add()方法

  1. public function cat_add(){
  2. if(Request::isPost()){
  3. $data = Request::post();
  4. if(empty($data['name'])){
  5. $this->returnCode('1','分类名称不能为空');
  6. }
  7. $insert = Db::table('oyk_cat')->insert($data);
  8. if(empty($insert)){
  9. $this->returnCode('91000001','添加失败,请重试……');
  10. }
  11. $this->returnCode(0);
  12. }else{
  13. return View::fetch();
  14. }
  15. }

动态分类修改

页面视图 cat_edit.php

  1. {include file="public/header" /}
  2. <form class="layui-form">
  3. <div class="layui-form-item" hidden >
  4. <label class="layui-form-label">ID</label>
  5. <div class="layui-input-block">
  6. <input type="text" class="layui-input" id="cat_id" value="{$find['id']}">
  7. </div>
  8. </div>
  9. <div class="layui-form-item">
  10. <label class="layui-form-label">分类名</label>
  11. <div class="layui-input-block">
  12. <input type="text" class="layui-input" id="uname" value="{$find['name']}" placeholder="请输入分类名称">
  13. </div>
  14. </div>
  15. <div class="layui-form-item">
  16. <label class="layui-form-label">类型</label>
  17. <div class="layui-input-block">
  18. <select id="type">
  19. <option value="1" {if $find['type']==1}selected{/if}>支出</option>
  20. <option value="2" {if $find['type']==2}selected{/if}>收入</option>
  21. </select>
  22. </div>
  23. </div>
  24. <div class="layui-form-item">
  25. <label class="layui-form-label">默认图片</label>
  26. <div class="layui-input-inline">
  27. <button type="button" class="layui-btn layui-btn-fluid" id="up_img">上传图片</button>
  28. </div>
  29. <div class="layui-form-mid layui-word-aux">支持png,jpg,gif</div>
  30. </div>
  31. <div class="layui-form-item">
  32. <label class="layui-form-label">图片预览</label>
  33. <div class="layui-input-block">
  34. <div class="layui-upload-list" id="test_img" style="width:90%;float:left;">
  35. {if(!empty($find['img']))}
  36. <div class="upload_pic_li" style="position:relative; width:100px; float:left; margin:0 10px 10px 0">
  37. <img style="width:100px;height:100px;" src="{$find['img']}" class="layui-upload-img" onmouseover="show_img(this)" onmouseleave="hide_img()">
  38. <input type="hidden" name="img" value="{$find['img']}"/>
  39. <a class="close-img" href="javascript:;" onclick="deleteImage('{$find[\'img\']}',this);return false;"></a>
  40. </div>
  41. {/if}
  42. </div>
  43. </div>
  44. <script type="text/javascript">
  45. layui.use(['layer','upload'],function(){
  46. layer = layui.layer;
  47. $ = layui.jquery;
  48. upload = layui.upload;
  49. upload.render({
  50. elem: "#up_img",
  51. url: "{$config['admin_route']}index/upload_img",
  52. multiple: true,
  53. done: function(res,title){
  54. if(res.code > 0){
  55. return layer.msg("上传失败,"+res.msg);
  56. }else{
  57. $("#test_img").append('<div class="upload_pic_li" style="position:relative; width:100px; float:left; margin:0 10px 10px 0"><img style="width:100px;height:100px;" src="'+ res.url +'" class="layui-upload-img" onmouseover="show_img(this)" onmouseleave="hide_img()"><input type="hidden" name="img" value="'+res.data+'"/><a class="close-img" href="javascript:;" onclick="deleteImage(\''+res.data+'\',this);return false;"></a></div>');
  58. }
  59. }
  60. });
  61. });
  62. </script>
  63. </div>
  64. <div class="layui-form-item">
  65. <label class="layui-form-label">选中图片</label>
  66. <div class="layui-input-inline">
  67. <button type="button" class="layui-btn layui-btn-fluid" id="up_img_s">上传图片</button>
  68. </div>
  69. <div class="layui-form-mid layui-word-aux">支持png,jpg,gif</div>
  70. </div>
  71. <div class="layui-form-item">
  72. <label class="layui-form-label">图片预览</label>
  73. <div class="layui-input-block">
  74. <div class="layui-upload-list" id="test_img_s" style="width:90%;float:left;">
  75. {if(!empty($find['img_s']))}
  76. <div class="upload_pic_li" style="position:relative; width:100px; float:left; margin:0 10px 10px 0">
  77. <img style="width:100px;height:100px;" src="{$find['img_s']}" class="layui-upload-img" onmouseover="show_img(this)" onmouseleave="hide_img()">
  78. <input type="hidden" name="img_s" value="{$find['img_s']}"/>
  79. <a class="close-img" href="javascript:;" onclick="deleteImage('{$find[\'img_s\']}',this);return false;"></a>
  80. </div>
  81. {/if}
  82. </div>
  83. </div>
  84. <script type="text/javascript">
  85. layui.use(['layer','upload'],function(){
  86. layer = layui.layer;
  87. $ = layui.jquery;
  88. upload = layui.upload;
  89. upload.render({
  90. elem: "#up_img_s",
  91. url: "{$config['admin_route']}index/upload_img",
  92. multiple: true,
  93. done: function(res,title){
  94. if(res.code > 0){
  95. return layer.msg("上传失败,"+res.msg);
  96. }else{
  97. $("#test_img_s").append('<div class="upload_pic_li" style="position:relative; width:100px; float:left; margin:0 10px 10px 0"><img style="width:100px;height:100px;" src="'+ res.url +'" class="layui-upload-img" onmouseover="show_img(this)" onmouseleave="hide_img()"><input type="hidden" name="img_s" value="'+res.data+'"/><a class="close-img" href="javascript:;" onclick="deleteImage(\''+res.data+'\',this);return false;"></a></div>');
  98. }
  99. }
  100. });
  101. });
  102. </script>
  103. </div>
  104. <div class="layui-form-item">
  105. <label class="layui-form-label">状态</label>
  106. <div class="layui-input-block">
  107. <select id="test_status">
  108. <option value="1" {if $find['status']==1}selected{/if}>开启</option>
  109. <option value="0" {if $find['status']==0}selected{/if}>关闭</option>
  110. </select>
  111. </div>
  112. </div>
  113. <div class="layui-form-item">
  114. <label class="layui-form-label">排序</label>
  115. <div class="layui-input-block">
  116. <input type="number" class="layui-input" value="{$find['sort']}" id="test_sort" placeholder="排序值越大越靠前">
  117. </div>
  118. </div>
  119. </form>
  120. {include file="public/tail" /}

Jizhang.php 控制器 cat_edit()方法

  1. public function cat_edit(){
  2. if(Request::isPost()){
  3. $data = Request::post();
  4. $id = input('post.id');
  5. $update = Db::table('oyk_cat')->where('id',$id)->update($data);
  6. if(empty($update)){
  7. $this->returnCode('91000002');
  8. }
  9. $this->returnCode(0);
  10. }else{
  11. $id = input('get.id');
  12. $find = Db::table('oyk_cat')->where('id',$id)->find();
  13. View::assign([
  14. 'find' => $find
  15. ]);
  16. return View::fetch();
  17. }
  18. }

动态分类删除

Jizhang.php 控制器 cat_del()方法

  1. public function cat_del(){
  2. $id = (int)input('post.id');
  3. $res = Db::table('oyk_cat')->where('id',$id)->delete();
  4. if(empty($res)){
  5. $this->returnCode('91000003');
  6. }
  7. $this->returnCode(0);
  8. }

由于表格是动态渲染的添加修改删除的方法的使用及数据请求都在cat_list页面中

注:所有操作均在Jizhang控制器中,其实页面表格渲染详见layUI官方使用手册

https://www.layui.com/

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