abstract:1.控制器類<?phpnamespace app\admin\controller;use app\admin\controller\Common;use app\admin\model\SortModel;use think\facade\Request;use think\facade\Session;class Sort extends Common{ pub
1.控制器類
<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\SortModel;
use think\facade\Request;
use think\facade\Session;
class Sort extends Common
{
public function index()
{
// 實(shí)例化模型
$sort = new SortModel();
// 查詢數(shù)據(jù)并按照id的順序每頁(yè)八條數(shù)據(jù)
$sorts = $sort->order('id', 'desc')->paginate(8);
//將數(shù)據(jù)賦值給模板
$this->view->sorts = $sorts;
// 渲染分類列表
return $this->fetch();
}
public function DoAdd()
{
//獲取提交過(guò)來(lái)的數(shù)據(jù)
$data = Request::param();
// 獲取添加數(shù)據(jù)
$data['time'] = time();
// 獲取發(fā)布管理員
$data['username'] = Session::get('username');
//實(shí)例化模型
$sort = new SortModel();
// 存儲(chǔ)并驗(yàn)證
if ($sort->save($data)) {
return ['res' => 1, 'msg' => '添加成功'];
} else {
return ['res' => 1, 'msg' => '添加失敗'];
}
}
public function edit()
{
// 獲取需要修改的分類id
$sortId = Request::param('id');
// 使用分類的id查詢對(duì)應(yīng)的數(shù)據(jù)
$sort = SortModel::get($sortId);
// 將數(shù)據(jù)賦值給模板
$this->view->sort = $sort;
// 渲染編輯界面
return $this->fetch();
}
public function DoEdit()
{
//獲取提交數(shù)據(jù)
$data = Request::param();
//實(shí)例化模型
$sort = new SortModel();
//修改更新操作
$info = $sort->save([
'title' => $data['title'],
'time' => time(),
'username' => Session::get('username')
], ['id' => $data['id']]);
//驗(yàn)證修改結(jié)果
if ($info) {
//返回對(duì)應(yīng)值
return ['res' => 1, 'msg' => '修改成功!'];
} else {
return ['res' => 0, 'msg' => '修改失敗!'];
}
}
public function del()
{
// 獲取需要?jiǎng)h除的分類id
$sortId = Request::param('id');
// 實(shí)例化模型
$sort = new SortModel();
// 刪除并驗(yàn)證
if ($sort->destroy($sortId)) {
return ['res'=>1,'msg'=>'刪除成功!'];
}
}
}
2.視圖類
(1)編輯頁(yè)edit.html
<!doctype html>
<html lang="en">
{include file="/public/header"}
<body>
<div class="x-body">
<form class="layui-form">
<div class="layui-form-item">
<label for="title" class="layui-form-label">
<span class="x-red">*</span>分類名
</label>
<div class="layui-input-inline">
<input type="text" id="title" name="title" value="{$sort.title}" required="" lay-verify="required"
autocomplete="off" class="layui-input">
<input type="hidden" value="{$sort.id}" id="id">
</div>
<div class="layui-form-mid layui-word-aux">
<span class="x-red">*</span>將會(huì)成為您唯一的分類名
</div>
</div>
<div class="layui-form-item">
<label for="L_repass" class="layui-form-label">
</label>
<button class="layui-btn" lay-filter="add" lay-submit="">
修改
</button>
</div>
</form>
</div>
<script src="/static/admin//lib/layui/layui.js"></script>
<script>
layui.use(['form','layer'], function(){
$ = layui.jquery;
var form = layui.form
,layer = layui.layer;
//自定義驗(yàn)證規(guī)則
form.verify({
nikename: function(value){
if(value.length < 5){
return '昵稱至少得5個(gè)字符啊';
}
}
});
//監(jiān)聽(tīng)提交
form.on('submit(add)', function(data){
console.log(data);
//發(fā)異步,把數(shù)據(jù)提交給php
$.post('{:url("DoEdit")}',{
'title':$('#title').val(),
'id':$('#id').val()
},function (data) {
if(data.res == 1){
layer.alert(data.msg, {icon: 6},function () {
// 獲得frame索引
var index = parent.layer.getFrameIndex(window.name);
//關(guān)閉當(dāng)前frame
parent.layer.close(index);
});
}else{
layer.alert(data.msg, {icon: 6},function () {
// 獲得frame索引
var index = parent.layer.getFrameIndex(window.name);
//關(guān)閉當(dāng)前frame
parent.layer.close(index);
});
}
})
return false;
});
});
</script>
</body>
</html>
(2)詳情頁(yè)index.html
<!doctype html>
<html lang="en">
{include file="/public/header"}
<body>
<div class="x-nav">
<span class="layui-breadcrumb">
<a href="">首頁(yè)</a>
<a href="">演示</a>
<a>
<cite>導(dǎo)航元素</cite></a>
</span>
<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
<i class="layui-icon" style="line-height:30px">?</i></a>
</div>
<div class="x-body">
<div class="layui-row">
<form class="layui-form layui-col-md12 x-so layui-form-pane">
<input class="layui-input" placeholder="分類名" id="title" name="title">
<button class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon"></i>添加</button>
</form>
</div>
<table class="layui-table layui-form">
<thead>
<tr>
<th width="70">ID</th>
<th width="200">分類名</th>
<th width="200">管理員</th>
<th width="200" >創(chuàng)建時(shí)間</th>
<th width="200">操作</th>
</thead>
<tbody>
{volist name="sorts" id="sort"}
<tr>
<td>{$sort.id}</td>
<td>{$sort.title}</td>
<td>{$sort.username}</td>
<td>{$sort.time|date="Y-m-d"}</td>
<td class="td-manage">
<button class="layui-btn layui-btn layui-btn-xs" onclick="x_admin_show('編輯','{:url(\'edit\')}?id={$sort.id}')"><i class="layui-icon"></i>編輯</button>
<button class="layui-btn-danger layui-btn layui-btn-xs" onclick="member_del(this,'{$sort.id}')" href="javascript:;" ><i class="layui-icon"></i>刪除</button>
</td>
</tr>
{/volist}
</tbody>
</table>
<div class="page">
<div>
{$sorts|raw}
</div>
</div>
</div>
<style type="text/css">
</style>
<script>
layui.use(['form'], function(){
form = layui.form;
form.on('submit(sreach)', function(data){
console.log(data);
$.post("{:url('DoAdd')}",{
'title':$('#title').val()
},function (data) {
if (data.reg == 1){
layer.msg(data.msg,{icon:1,time:1000});
}else{
layer.msg(data.msg,{icon:1,time:1000});
}
})
return false;
})
});
/*用戶-刪除*/
function member_del(obj,id){
layer.confirm('確認(rèn)要?jiǎng)h除嗎?',function(index){
//發(fā)異步刪除數(shù)據(jù)
$.get('{:url(\'Sort/del\')}','id='+id,function (data) {
if (data.res == 1){
$(obj).parents("tr").remove();
layer.msg(data.msg,{icon:1,time:1000});
}
})
});
}
</script>
</body>
</html>
3.模板類
<?php
namespace app\admin\model;
use \think\Model;
class SortModel extends Model
{
protected $table = 'sort';
protected $pk = 'id';
}
4.運(yùn)行結(jié)果
Correcting teacher:天蓬老師Correction time:2019-05-27 09:15:37
Teacher's summary:你的后臺(tái)是否使用了內(nèi)聯(lián)框架, 出現(xiàn)了二個(gè)代碼跟蹤器....