目录
创建员工信息管理模块,实现用户信息增删改查,按原代码风格编写。
1.likeshop后台管理创建模块分级(二级规则创建要先创建相关控制器)
2.数据库随便新建一个数据表ls_user_info用来记录相关数据
员工信息显示
3.在admin控制器管理目录下UserPhoto控制器里写lists相关代码。
public function lists(){
$get = $this->request->get();
if(isset($get['name'])){
$gjz =$get['name'];
$lists = Db('user_info')->order('id','desc')->where( "name",'like',"%{$gjz}%" )->page($get['page'],$get['limit'])->select();
$this->_success('',['list'=>$lists]);
}
if($this->request->isAjax()){
$lists = Db('user_info')->order('id','desc')->page($get['page'],$get['limit'])->select();
$this->_success('',['list'=>$lists]);
}
return $this->fetch();
}
4.在admin管理视图目录下创建user_photo文件夹,这里写员工信息前端显示页相关代码。该lists页面为内容显示前端页,按原编码风格用系统封装layui书写显示页面。
{layout name="layout2"/}
<style>
.layui-form-label {
padding: 9px 0;
}
</style>
<div class="luyui-fluid">
<div class="layui-card">
<div class="layui-card-body">
<div class="layui-collapse like-layui-collapse" lay-accordion="" style="border:1px dashed #c4c4c4">
<div class="layui-colla-item">
<h2 class="layui-colla-title like-layui-colla-title" style="background-color: #fff">操作提示</h2>
<div class="layui-colla-content layui-show">
<p>员工信息管理库</p>
</div>
</div>
</div>
</div>
</div>
<div class="layui-card">
<div class="layui-card-body">
<div class="layui-form layui-card-header layuiadmin-card-header-auto">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">查员工身份</label>
<div class="layui-input-block">
<input type="text" name="name" id="keyword" placeholder="请输入员工姓名" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-inline">
<button class="layui-btn layui-btn-sm layuiadmin-btn-user_photo {$view_theme_color}" lay-submit lay-filter="user_photo-search">查询</button>
</div>
<div class="layui-inline">
<button class="layui-btn layui-btn-sm layui-btn-primary layuiadmin-btn-user_photo " lay-submit lay-filter="user_photo-clear-search">清空查询</button>
</div>
</div>
</div>
</div>
</div>
<div class="layui-tab layui-tab-card" lay-filter="tab-all">
<div class="layui-card">
<div class="layui-card-body">
<!-- 表结构在下一行 通过遍历得到完整表-->
<div style="padding-bottom: 10px;" class="add">
<button class="layui-btn layui-btn-sm layuiadmin-btn-user_photo {$view_theme_color}" data-type="add">增加员工信息</button>
</div>
<table id="user_photo-lists" lay-filter="user_photo-lists"></table>
<script type="text/html" id="image">
<img src="{{d.user_img}}" style="height:auto;width: auto" class="image-show">
</script>
<script type="text/html" id="user_photo-operation">
<a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
</script>
</div>
</div>
</div>
</div>
<style>
.layui-table-cell {
height: auto;
}
</style>
<script>
layui.config({
version: "{$front_version}",
base: '/static/plug/layui-admin/dist/layuiadmin/' //静态资源所在路径
}).extend({
index: 'lib/index' //主入口模块
}).use(['index', 'table', 'like', 'form'], function() {
var $ = layui.$,
form = layui.form,
table = layui.table, //表格
like = layui.like,
element = layui.element; //元素操作
//图片放大
$(document).on('click', '.image-show', function() {
var src = $(this).attr('src');
like.showImg(src, 600);
});
//监听搜索
form.on('submit(user_photo-search)', function(data) {
var field = data.field;
//执行重载
table.reload('user_photo-lists', {
where: field,
page: {
curr: 1
}
});
});
//清空查询
form.on('submit(user_photo-clear-search)', function() {
$('#keyword').val(' ');
//刷新列表
table.reload('user_photo-lists', {
where: [],
page: {
curr: 1
}
});
});
$('.layui-btn.layuiadmin-btn-user_photo').on('click', function() {
var type = $(this).data('type');
active[type] ? active[type].call(this) : '';
});
layui.define(['table', 'form'], function(exports) {
var $ = layui.$,
table = layui.table,
form = layui.form;
//管理员管理 //执行一个 table 实例
table.render({
id: 'user_photo-lists',
elem: '#user_photo-lists',
page: true, //开启分页
url: '{:url("user_photo/lists")}', //模拟(数据)接口
cols: [
[{
field: 'name',
title: '员工姓名',
width: 150
}, {
field: 'age',
width: 100,
title: '员工年龄'
}, {
field: 'user_img',
width: 300,
title: '员工头像',
toolbar: '#image'
}, {
field: 'position',
width: 265,
title: '员工岗位'
}, {
fixed: 'right',
title: '管理',
width: 200,
align: 'center',
toolbar: '#user_photo-operation'
}]
],
// page: true,//开启分页
text: {
none: '暂无数据!'
},
parseData: function(res) { //将原始数据解析成 table 组件所规定的数据
console.log(res);
return {
"code": res.code,
"msg": res.msg,
"count": res.data.count, //解析数据长度
"data": res.data.list, //解析数据列表
};
},
done: function(res, curr, count) {
// 解决操作栏因为内容过多换行问题
$(".layui-table-main tr").each(function(index, val) {
$($(".layui-table-fixed-l .layui-table-body tbody tr")[index]).height($(val).height());
$($(".layui-table-fixed-r .layui-table-body tbody tr")[index]).height($(val).height());
});
}
});
});
//事件
var active = {
add: function() {
var index = layer.open({
type: 2,
title: '完善个人信息',
content: '{:url("user_photo/add")}',
area: ['90%', '90%'],
btn: ['保存', '取消'],
maxmin: true,
yes: function(index, layero) {
var iframeWindow = window['layui-layer-iframe' + index],
submitID = 'add-user_photo-submit',
submit = layero.find('iframe').contents().find('#' + submitID);
//监听提交
iframeWindow.layui.form.on('submit(' + submitID + ')', function(data) {
var field = data.field;
// console.log(field);
like.ajax({
url: '{:url("user_photo/add")}',
data: field,
type: "post",
success: function(res) {
if (res.code == 1) {
layui.layer.msg(res.msg, {
offset: '15px',
icon: 1,
time: 1000
});
layer.close(index); //关闭弹层
table.reload('user_photo-lists'); //数据刷新
}
}
});
});
submit.trigger('click');
}
});
}
}
table.on('tool(user_photo-lists)', function(obj) {
var id = obj.data.id;
if (obj.event === 'edit') {
var index = layer.open({
type: 2,
title: '修改个人信息',
content: '{:url("user_photo/edit")}?id=' + id,
area: ['90%', '90%'],
btn: ['保存', '取消'],
maxmin: true,
yes: function(index, layero) {
var iframeWindow = window['layui-layer-iframe' + index],
submit = layero.find('iframe').contents().find('#edit-user_photo-submit');
//监听提交
iframeWindow.layui.form.on('submit(edit-user_photo-submit)', function(data) {
var field = data.field;
console.log(field);
like.ajax({
url: '{:url("user_photo/edit")}',
data: field,
type: "post",
success: function(res) {
if (res.code == 1) {
layui.layer.msg(res.msg, {
offset: '15px',
icon: 1,
time: 1000
});
layer.close(index); //关闭弹层
table.reload('user_photo-lists'); //数据刷新
}
}
});
});
submit.trigger('click');
}
});
}
if (obj.event === 'del') {
var name = obj.data.name;
layer.confirm('确定删除员工<span style="color: red">' + name + '</span>' + '的身份信息吗?', function(index) {
like.ajax({
url: '{:url("user_photo/del")}',
data: {
id: id
},
type: "post",
success: function(res) {
console.log("id=" + id);
if (res.code == 1) {
layui.layer.msg(res.msg, {
offset: '15px',
icon: 1,
time: 1000
});
layer.close(index); //关闭弹层
table.reload('user_photo-lists'); //数据刷新
}
}
});
layer.close(index);
})
}
})
});
</script>
大概效果预览:
员工信息修改
1.后端相关代码
public function edit($id){
if($this->request->isAjax()){
$data = $this->request->post();
$data['user_img'] = DIRECTORY_SEPARATOR.$data['user_img']; //此处DIRECTORY_SEPARATOR为tp框架内斜杠的意思(可修改,和tp5.0的DS一个意思)
$res = Db('user_info')->where('id',$id)->update($data);
if($res){
$this->_success('修改成功','');
}
}
$data = $this->request->post();
$res = Db('user_info')->where('id',$id)->find();
$this->assign('detail',$res);
return $this->fetch();
}
2.前端layui相关代码
2.1lists页面相关代码
<script type="text/html" id="user_photo-operation">
<a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit">编辑</a></script>
if (obj.event === 'edit') {
var index = layer.open({
type: 2,
title: '修改个人信息',
content: '{:url("user_photo/edit")}?id=' + id,
area: ['90%', '90%'],
btn: ['保存', '取消'],
maxmin: true,
yes: function(index, layero) {
var iframeWindow = window['layui-layer-iframe' + index],
submit = layero.find('iframe').contents().find('#edit-user_photo-submit');
//监听提交
iframeWindow.layui.form.on('submit(edit-user_photo-submit)', function(data) {
var field = data.field;
console.log(field);
like.ajax({
url: '{:url("user_photo/edit")}',
data: field,
type: "post",
success: function(res) {
if (res.code == 1) {
layui.layer.msg(res.msg, {
offset: '15px',
icon: 1,
time: 1000
});
layer.close(index); //关闭弹层
table.reload('user_photo-lists'); //数据刷新
}
}
});
});
submit.trigger('click');
}
});
}
2.2在lists同级视图目录下写修改页面代码:
{layout name="layout2" /}
<style>
.layui-form-label {
color: #6a6f6c;
width: 100px;
}
.layui-input-block {
margin-left: 130px;
}
.tips {
color: red;
}
.goods-li {
float: left;
opacity: 1;
position: relative;
}
.goods-img {
width: 80px;
height: 80px;
padding: 4px;
}
.goods-img-del-x {
position: absolute;
z-index: 100;
top: -4px;
right: -2px;
width: 20px;
height: 20px;
font-size: 16px;
line-height: 16px;
color: #fff;
text-align: center;
cursor: pointer;
background: hsla(0, 0%, 60%, .6);
border-radius: 10px;
}
</style>
<div class="layui-form" lay-filter="layuiadmin-form-user_photo" id="layuiadmin-form-user_level" style="padding: 20px 30px 0 0;">
<input type="hidden" name="id" value="{$detail.id}">
<div class="layui-form-item">
<label class="layui-form-label"><span class="tips">*</span>个人名称:</label>
<div class="layui-input-inline">
<input type="text" name="name" value="{$detail.name}" lay-verify="required" lay-verType="tips" placeholder="请输入用户姓名" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><span class="tips">*</span>个人年龄:</label>
<div class="layui-input-inline">
<input name="age" value="{$detail.age}" lay-verify="required" lay-verType="tips" placeholder="请输入年龄" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item ">
<label class="layui-form-label"><span class="tips">*</span>个人头像:</label>
<div class="layui-inline">
<div class="" style="height:80px;line-height:80px">
<input name="user_img" type="hidden" value="{$detail.user_img}"> {if !empty($detail.user_img)}
<div class="goods-img-add" style="display: none"></div>
<div class="goods-li">
<img class="goods-img" src="{$detail.user_img}">
<a class="goods-img-del-x" style="display: none">x</a>
</div>
{else}
<div class="goods-img-add"></div>
{/if}
</div>
<div class=" layui-form-mid layui-word-aux" style="white-space: nowrap">建议尺寸:100*100像素,jpg,jpeg,png图片类型</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><span class="tips">*</span>个人介绍:</label>
<div class="layui-input-block">
<textarea name="position" placeholder="请输入等级说明" class="layui-textarea">{$detail.position}</textarea>
</div>
</div>
<div class="layui-form-item layui-hide">
<input type="button" lay-submit lay-filter="edit-user_photo-submit" id="edit-user_photo-submit" value="确认">
</div>
</div>
<style>
.layui-form-label {
width: 100px;
}
.layui-input-block {
margin-left: 130px;
}
</style>
<link rel="stylesheet" href="/static/plug/formSelects/formSelects-v4.css" />
<script src="/static/plug/formSelects/formSelects-v4.js" type="text/javascript" charset="utf-8"></script>
<script>
layui.config({
version: "{$front_version}",
base: '/static/plug/layui-admin/dist/layuiadmin/' //静态资源所在路径
}).extend({
index: 'lib/index' //主入口模块
}).use(['index', 'form', 'like'], function() {
var $ = layui.$,
form = layui.form,
like = layui.like,
formSelects = layui.formSelects,
select_id = 1;
//上传图片
like.imageUpload('.goods-img-add', function(uri, element) {
if (uri.length > 1) {
layer.msg('最多最能选中1张图片');
return;
}
var html = '<div class="goods-li">\n' +
'<img class="goods-img" ' +
'src="' + uri[0] + '">' +
'<a class="goods-img-del-x" style="display: none">x</a>' +
'</div>';
element.prev().val(like.getUrlFileName(uri[0], '{$storageUrl}'));
element.parent().append(html);
element.css('display', 'none');
}, true);
//删除图片
$(document).on('click', '.goods-img-del-x', function() {
$(this).parent().siblings('input').val('');
$(this).parent().prev().css('display', 'block');
$(this).parent().remove();
});
//显示图片
$(document).on('click', '.goods-img', function() {
var image = $(this).attr('src');
like.showImg(image, 600);
});
// 删除按钮的显示与隐藏
$(document).on('mouseover', '.goods-img', function() {
$(this).next().show();
});
$(document).on('mouseout', '.goods-img', function() {
$(this).next().hide();
});
$(document).on('mouseover', '.goods-img-del-x', function() {
$(this).show();
});
$(document).on('mouseout', '.goods-img-del-x', function() {
$(this).hide();
});
//三级联动
formSelects.value('privilege', select_id);
})
</script>
效果预览:
员工信息添加
1.后端代码
public function add(){
if($this->request->isAjax()){
$data = $this->request->post();
$data['user_img'] = DIRECTORY_SEPARATOR.$data['user_img'];
$res = Db('user_info')->insert($data);
if($res){
$this->_success('添加成功','');
}
}
return $this->fetch();
}
2.前端代码:
2.1.lists里的相关代码:
<div style="padding-bottom: 10px;" class="add">
<button class="layui-btn layui-btn-sm layuiadmin-btn-user_photo {$view_theme_color}" data-type="add">增加员工信息</button>
</div>
//事件
var active = {
add: function() {
var index = layer.open({
type: 2,
title: '完善个人信息',
content: '{:url("user_photo/add")}',
area: ['90%', '90%'],
btn: ['保存', '取消'],
maxmin: true,
yes: function(index, layero) {
var iframeWindow = window['layui-layer-iframe' + index],
submitID = 'add-user_photo-submit',
submit = layero.find('iframe').contents().find('#' + submitID);
//监听提交
iframeWindow.layui.form.on('submit(' + submitID + ')', function(data) {
var field = data.field;
// console.log(field);
like.ajax({
url: '{:url("user_photo/add")}',
data: field,
type: "post",
success: function(res) {
if (res.code == 1) {
layui.layer.msg(res.msg, {
offset: '15px',
icon: 1,
time: 1000
});
layer.close(index); //关闭弹层
table.reload('user_photo-lists'); //数据刷新
}
}
});
});
submit.trigger('click');
}
});
}
}
2.2.在lists同级视图目录下健添加页面前端代码:
{layout name="layout2" /}
<style>
.layui-form-label {
color: #6a6f6c;
width: 100px;
}
.layui-input-block {
margin-left: 130px;
}
.tips {
color: red;
}
.goods-li {
float: left;
opacity: 1;
position: relative;
}
.goods-img {
width: 80px;
height: 80px;
padding: 4px;
}
.goods-img-del-x {
position: absolute;
z-index: 100;
top: -4px;
right: -2px;
width: 20px;
height: 20px;
font-size: 16px;
line-height: 16px;
color: #fff;
text-align: center;
cursor: pointer;
background: hsla(0, 0%, 60%, .6);
border-radius: 10px;
}
</style>
<div class="layui-form" lay-filter="layuiadmin-form-user_level" id="layuiadmin-form-user_level" style="padding: 20px 30px 0 0;">
<div class="layui-form-item">
<label class="layui-form-label"><span class="tips">*</span>员工名称:</label>
<div class="layui-input-inline">
<input type="text" name="name" lay-verify="required" lay-verType="tips" placeholder="请输入员工名称" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><span class="tips">*</span>员工年龄:</label>
<div class="layui-input-inline">
<input type="number" name="age" lay-verify="required" lay-verType="tips" placeholder="请输入员工年龄" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><span class="tips">*</span>员工头像:</label>
<div class="layui-input-inline">
<div class="" style="height:80px;line-height:80px">
<input name="user_img" type="hidden" value="">
<div class="goods-img-add"></div>
</div>
<div class=" layui-form-mid layui-word-aux" style="white-space: nowrap">建议尺寸:100*100像素,jpg,jpeg,png图片类型</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">员工岗位:</label>
<div class="layui-input-block">
<textarea name="position" placeholder="请填入岗位" class="layui-textarea"></textarea>
</div>
</div>
<div class="layui-form-item layui-hide">
<input type="button" lay-submit lay-filter="add-user_photo-submit" id="add-user_photo-submit" value="确认">
</div>
</div>
<style>
.layui-form-label {
width: 100px;
}
.layui-input-block {
margin-left: 130px;
}
</style>
<link rel="stylesheet" href="/static/plug/formSelects/formSelects-v4.css" />
<script src="/static/plug/formSelects/formSelects-v4.js" type="text/javascript" charset="utf-8"></script>
<script>
layui.config({
version: "{$front_version}",
base: '/static/plug/layui-admin/dist/layuiadmin/' //静态资源所在路径
}).extend({
index: 'lib/index' //主入口模块
}).use(['index', 'form', 'like'], function() {
var $ = layui.$,
form = layui.form,
like = layui.like,
formSelects = layui.formSelects;
//上传图片
like.imageUpload('.goods-img-add', function(uri, element) {
if (uri.length > 1) {
layer.msg('最多最能选中1张图片');
return;
}
var html = '<div class="goods-li">\n' +
'<img class="goods-img" ' +
'src="' + uri[0] + '">' +
'<a class="goods-img-del-x" style="display: none">x</a>' +
'</div>';
element.prev().val(like.getUrlFileName(uri[0], '{$storageUrl}'));
element.parent().append(html);
element.css('display', 'none');
}, true);
//删除图片
$(document).on('click', '.goods-img-del-x', function() {
$(this).parent().siblings('input').val('');
$(this).parent().prev().css('display', 'block');
$(this).parent().remove();
});
//显示图片
$(document).on('click', '.goods-img', function() {
var image = $(this).attr('src');
like.showImg(image, 600);
});
// 删除按钮的显示与隐藏
$(document).on('mouseover', '.goods-img', function() {
$(this).next().show();
});
$(document).on('mouseout', '.goods-img', function() {
$(this).next().hide();
});
$(document).on('mouseover', '.goods-img-del-x', function() {
$(this).show();
});
$(document).on('mouseout', '.goods-img-del-x', function() {
$(this).hide();
});
//三级联动
formSelects.value('privilege', '');
})
</script>
效果预览:
实现员工搜索
1.后端代码(就在控制器lists里,细心的小伙伴想必已经发现了):
$get = $this->request->get();
if(isset($get['name'])){
$gjz =$get['name'];
$lists = Db('user_info')->order('id','desc')->where( "name",'like',"%{$gjz}%" )->page($get['page'],$get['limit'])->select();
$this->_success('',['list'=>$lists]);
}
2.前端代码(使用了layui的表格重载,在前面的lists前端代码里):
<div class="layui-card">
<div class="layui-card-body">
<div class="layui-form layui-card-header layuiadmin-card-header-auto">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">查员工身份</label>
<div class="layui-input-block">
<input type="text" name="name" id="keyword" placeholder="请输入员工姓名" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-inline">
<button class="layui-btn layui-btn-sm layuiadmin-btn-user_photo {$view_theme_color}" lay-submit lay-filter="user_photo-search">查询</button>
</div>
<div class="layui-inline">
<button class="layui-btn layui-btn-sm layui-btn-primary layuiadmin-btn-user_photo " lay-submit lay-filter="user_photo-clear-search">清空查询</button>
</div>
</div>
</div>
</div>
</div>
3.前端代码2,表格重载(在前面lists前端代码里):
//监听搜索
form.on('submit(user_photo-search)', function(data) {
var field = data.field;
//执行重载
table.reload('user_photo-lists', { //此处重新加载页面并将搜索关键字返回后端
where: field, //field携带了修改关键字{name:'猪'}交给控制器lists继续查询输出
page: {
curr: 1
}
});
});
//清空查询
form.on('submit(user_photo-clear-search)', function() {
$('#keyword').val(' ');
//刷新列表
table.reload('user_photo-lists', {
where: [],
page: {
curr: 1
}
});
});
实现效果:
员工信息删除:
1.后端代码:
public function del(){
if($this->request->isAjax()){
$data = $this->request->post();
$res = Db('user_info')->delete($data['id']);
if($res){
$this->_success('删除成功','');
}
}
}
2.前端代码(在list显示页代码中可找到):
<script type="text/html" id="user_photo-operation">
<a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
</script>
if (obj.event === 'del') {
var name = obj.data.name;
layer.confirm('确定删除员工<span style="color: red">' + name + '</span>' + '的身份信息吗?', function(index) {
like.ajax({
url: '{:url("user_photo/del")}',
data: {
id: id
},
type: "post",
success: function(res) {
console.log("id=" + id);
if (res.code == 1) {
layui.layer.msg(res.msg, {
offset: '15px',
icon: 1,
time: 1000
});
layer.close(index); //关闭弹层
table.reload('user_photo-lists'); //数据刷新
}
}
});
layer.close(index);
})
}
记录我的实习生活