abstract:view\news\index.html代碼{include file="/public/head"} <body> <div class="x-nav"> <spa
view\news\index.html代碼
{include file="/public/head"} <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"> <xblock> <button class="layui-btn" onclick="x_admin_show('添加用戶','{:url(\'News/add\')}')"><i class="layui-icon"></i>添加</button> <span class="x-right" style="line-height:40px">共有數(shù)據(jù):{$newsTotal} 條</span> </xblock> <table class="layui-table"> <thead> <tr> <th>新聞ID</th> <th>新聞標(biāo)題</th> <th>新聞圖片</th> <th>新聞簡(jiǎn)介</th> <th>發(fā)布管理員</th> <th>發(fā)布日期</th> <th >操作</th> </tr> </thead> <tbody> {volist name="new" id="news"} <tr> <td>{$news.id}</td> <td>{$news.title}</td> <td><img src=" width="20"></td> <td>{$news.desc}</td> <td>{$news.username}</td> <td>{$news.time|date="Y-m-d"}</td> <td class="td-manage"> <a title="查看" onclick="x_admin_show('編輯','{:url(\'News/edit\')}?id={$news.id}')" href="javascript:;"> <i class="layui-icon"></i> </a> <a title="刪除" onclick="member_del(this,'{$news.id}')" href="javascript:;"> <i class="layui-icon"></i> </a> </td> </tr> {/volist} </tbody> </table> <div class="page"> <div> {$new|raw} </div> </div> </div> <script> layui.use('laydate', function(){ var laydate = layui.laydate; //執(zhí)行一個(gè)laydate實(shí)例 laydate.render({ elem: '#start' //指定元素 }); //執(zhí)行一個(gè)laydate實(shí)例 laydate.render({ elem: '#end' //指定元素 }); }); /*用戶-刪除*/ function member_del(obj,id){ layer.confirm('確認(rèn)要?jiǎng)h除嗎?',function(index){ //發(fā)異步刪除數(shù)據(jù) $.get('{:url(\'del\')}','id='+id,function(data){ if(data.res == 1){ $(obj).parents("tr").remove(); layer.msg(data.msg,{icon:1,time:1000}); } }); }); } </script> </body> </html>
view\news\edit.html代碼
{include file="/public/head"} <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>新聞標(biāo)題 </label> <div class="layui-input-inline"> <input type="text" id="title" name="title" value="{$new.title}" required="" lay-verify="title" autocomplete="off" class="layui-input"> <input type="hidden" id="id" name="id" value="{$new.id}"> </div> </div> <div class="layui-form-item"> <label for="desc" class="layui-form-label"> <span class="x-red">*</span>簡(jiǎn)介 </label> <div class="layui-input-inline"> <textarea placeholder="請(qǐng)輸入內(nèi)容" id="desc" name="desc" class="layui-textarea">{$new.desc}</textarea> </div> </div> <div class="layui-form-item layui-form-text"> <label for="desc" class="layui-form-label"> 內(nèi)容 </label> <div class="layui-input-block" id="editor"> {:htmlspecialchars_decode($new.content)} </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 type="text/javascript" src="__ADMIN__/js/wangEditor.js"></script> <script> layui.use(['form','layer'], function(){ $ = layui.jquery; var form = layui.form ,layer = layui.layer; //配置wangEditor富文本編輯器 ////將要用到的對(duì)象添加到全局 var E = window.wangEditor //生成editor對(duì)象 var editor = new E('#editor') //設(shè)置圖片上傳的控件名稱:類(lèi)似于input的name屬性,供接口獲取圖片信息使用 editor.customConfig.uploadFileName = 'img' //設(shè)置服務(wù)上的圖片上傳處理接口腳本 editor.customConfig.uploadImgServer = '{:url(\'upload\')}' //創(chuàng)建出富文件編輯器 editor.create() //自定義驗(yàn)證規(guī)則 form.verify({ title: function(value){ if(value.length < 5){ return '新聞標(biāo)題至少得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(), 'desc':$('#desc').val(), 'content':editor.txt.html() },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>
controller\News.php代碼
<?php namespace app\admin\controller; use app\admin\controller\Common; use app\admin\model\NewsModel; use think\facade\Request; use think\facade\Session; class News extends Common { public function index() { //取得新聞總數(shù) $newsTotal = NewsModel::count(); //實(shí)例化模型 $news = new NewsModel(); //按id降序排列,并且每一頁(yè)為8條數(shù)據(jù) $new = $news->order('id','desc')->paginate(8); //將數(shù)據(jù)賦值給模板 $this->view->newsTotal = $newsTotal; $this->view->new = $new; //渲染新聞列表 return $this->fetch(); } public function add() { //渲染新聞添加頁(yè)面 return $this->fetch(); } public function upload() { //獲取上傳的圖片信息 $file = Request::file('img'); //驗(yàn)證圖片信息并移動(dòng)到指定目錄 if($info = $file->validate(['ext'=>'jpg,jpeg,png,gif'])->move('upload')){ //返回上傳成功信息 return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]); }else{ //返回錯(cuò)誤信息 return $file->getError(); } } public function DoAdd() { //獲取前臺(tái)提交過(guò)來(lái)的數(shù)據(jù) $data = Request::param(); //加入發(fā)布的時(shí)間 $data['time'] = time(); //加入發(fā)布新聞的管理員名稱 $data['username'] = Session::get('username'); //將新聞標(biāo)題獨(dú)立出來(lái) $title = $data['title']; //將新聞標(biāo)題作為查詢條件來(lái)查詢數(shù)據(jù) $news = NewsModel::where('title',$title)->find(); //驗(yàn)證新聞標(biāo)題是否重復(fù) if($news == true){ //重復(fù)返回提示信息 return ['res'=>0,'msg'=>'新聞標(biāo)題重復(fù)!']; } //實(shí)例化模型 $new = new NewsModel(); //存儲(chǔ)驗(yàn)證 if($new->save($data)){ //返回發(fā)布成功的信息 return ['res'=>1,'msg'=>'新聞添加成功!']; }else{ //返回發(fā)布失敗的信息 return ['res'=>0,'msg'=>'新聞添加失?。?#39;]; } } public function edit() { //接受傳遞過(guò)來(lái)的新聞id $newId = Request::param('id'); //查詢新聞id對(duì)應(yīng)的新聞信息 $new = NewsModel::get($newId); //將數(shù)據(jù)賦值到模板 $this->view->new = $new; //渲染新聞編輯頁(yè)面 return $this->fetch(); } public function DoEdit() { //獲取提交的數(shù)據(jù) $data = Request::param(); //實(shí)例化模型 $new = new NewsModel; //進(jìn)行修改操作 $res = $new->save([ 'title'=>$data['title'], 'desc'=>$data['desc'], 'content'=>$data['content'], 'username'=>Session::get('username'), 'time'=>time() ],['id'=>$data['id']]); //驗(yàn)證修改結(jié)果 if($res){ //返回修改成功信息 return ['res'=>1,'msg'=>'新聞修改成功!']; }else{ //返回修改失敗信息 return ['res'=>0,'msg'=>'新聞修改失敗!']; } } public function del() { //獲取需要?jiǎng)h除的新聞id $newId = Request::param('id'); //實(shí)例化模型 $new = new NewsModel(); //刪除并驗(yàn)證 if($new->destroy($newId)){ //返回刪除成功提示 return ['res'=>1,'msg'=>'新聞刪除成功!']; }else{ //返回刪除失敗信息 return ['res'=>0,'msg'=>'新聞刪除失??!']; } } } ?>
Correcting teacher:天蓬老師Correction time:2018-12-21 12:59:42
Teacher's summary:更新與刪除操作, 是典型的寫(xiě)操作, 寫(xiě)操作,必須是基于查詢的,當(dāng)然新增除外,因?yàn)樾略?并不會(huì)修改表中原有數(shù)據(jù)