产品修改以及删除操作的实现

原创 2018-12-23 00:42:40 366
摘要:view\product\index.html代码{include file="/public/head"}   <body>     <div class="x-nav">       <

view\product\index.html代码

{include file="/public/head"}
  <body>
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">演示</a>
        <a>
          <cite>导航元素</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(\'Product/add\')}')"><i class="layui-icon"></i>添加</button>
        <span class="x-right" style="line-height:40px">共有数据:{$productTotal} 条</span>
      </xblock>
      <table class="layui-table">
        <thead>
          <tr>
            <th>产品ID</th>
            <th>产品标题</th>
            <th>产品分类</th>
            <th>产品图片</th>
            <th>产品简介</th>
            <th>发布管理员</th>
            <th>发布日期</th>
            <th >操作</th>
            </tr>
        </thead>
        <tbody>
        {volist name="products" id="product"}
          <tr>
            <td>{$product.id}</td>
            <td>{$product.title}</td>
            <td>{:GetSortTitle($product.sort)}</td>
            <td><img src="{:GetProductPic($product.id)}" width="100"></td>
            <td>{$product.desc}</td>
            <td>{$product.username}</td>
            <td>{$product.time|date="Y-m-d"}</td>
            <td class="td-manage">
              <a title="查看"  onclick="x_admin_show('编辑','{:url(\'Product/edit\')}?id={$product.id}')" href="javascript:;">
                <i class="layui-icon">&#xe63c;</i>
              </a>
              <a title="删除" onclick="member_del(this,'{$product.id}')" href="javascript:;">
                <i class="layui-icon">&#xe640;</i>
              </a>
            </td>
          </tr>
          {/volist}
        </tbody>
      </table>
      <div class="page">
        <div>
          {$products|raw}
        </div>
      </div>
    </div>
    <script>
      layui.use('laydate', function(){
        var laydate = layui.laydate;
        
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });
        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });
      });

      /*用户-删除*/
      function member_del(obj,id){
          layer.confirm('确认要删除吗?',function(index){
              //发异步删除数据
              $.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\product\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>产品标题
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="title" name="title" value="{$product.title}" required="" lay-verify="title"
                  autocomplete="off" class="layui-input">
                  <input type="hidden" id="id" name="id" value="{$product.id}">
              </div>
          </div>
          <div class="layui-form-item">
              <label for="desc" class="layui-form-label">
                  <span class="x-red">*</span>简介
              </label>
              <div class="layui-input-inline">
                  <textarea placeholder="请输入内容" id="desc" name="desc" class="layui-textarea">{$product.desc}</textarea>
              </div>
          </div>
          <div class="layui-form-item layui-form-text">
            <label for="editor" class="layui-form-label">
                内容
            </label>
            <div class="layui-input-block" id="editor">
            {:htmlspecialchars_decode($product.content)}
            </div>
          </div>
          <div class="layui-form-item">
              <label for="once" class="layui-form-label">
                  <span class="x-red">*</span>一次价格
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="once" name="once" value="{$product.once}" required="" lay-verify="once"
                  autocomplete="off" class="layui-input">
              </div>
          </div>
          <div class="layui-form-item">
              <label for="over_night" class="layui-form-label">
                  <span class="x-red">*</span>一夜价格
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="over_night" name="over_night" value="{$product.over_night}" required="" lay-verify="over_night"
                  autocomplete="off" class="layui-input">
              </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富文本编辑器
        ////将要用到的对象添加到全局
        var E = window.wangEditor
        //生成editor对象
        var editor = new E('#editor')
        //设置图片上传的控件名称:类似于input的name属性,供接口获取图片信息使用
        editor.customConfig.uploadFileName = 'img'
        //设置服务上的图片上传处理接口脚本
        editor.customConfig.uploadImgServer = '{:url(\'upload\')}'
        //创建出富文件编辑器
        editor.create()
        
          //自定义验证规则
          form.verify({
            title: function(value){
              if(value.length < 2){
                return '产品标题至少得2个字符啊';
              }
            }
          });
          //监听提交
          form.on('submit(add)', function(data){
            console.log(data);
            //发异步,把数据提交给php
            $.post('{:url(\'DoEdit\')}',{
                'title':$('#title').val(),
                'id':$('#id').val(),
                'desc':$('#desc').val(),                
                'content':editor.txt.html(),
                'once':$('#once').val(),
                'over_night':$('#over_night').val()
            },function(data){
                if(data.res == 1){
                    layer.alert(data.msg, {icon: 6},function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                    });                  
                }else{
                        layer.alert(data.msg, {icon: 6},function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                    });   
                }
            });
            return false;
          });          
        });
    </script>
  </body>
</html>

controller\Product.php代码

<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\ProductModel;
use app\admin\model\SortModel;
use think\facade\Request;
use think\facade\Session;
class Product extends Common
{
 public function index(){
  //取得产品总数
        $productTotal = ProductModel::count();
  //实例化模型
  $product = new ProductModel();
  //查询数据并按id降序排列,每一页为8条数据
  $products = $product->order('id','desc')->paginate(8);
  //将数据赋值给模板
  $this->view->productTotal=$productTotal;
  $this->view->products=$products;
  //渲染产品列表
  return $this->fetch();
 }
 public function add(){
  //实例化并查询分类
  $sorts = SortModel::order('id','desc')->all();
  //数据赋值到模板
  $this->view->sorts=$sorts;
  //渲染产品添加界面
  return $this->fetch();
 }
 public function upload(){
  //获取上传图片信息
  $file = Request::file('img');
  //验证图片信息并移动到指定目录
  if($info = $file->validate(['ext'=>'jpeg,jpg,png,gif'])->move('upload')){
   //返回上传成功信息
   return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]);
  }else{
   //返回错误信息
   return $file->getError();
  }
 }
 public function DoAdd(){
  //获取前台提交过来的数据
  $data = Request::param();
  $title = $data['title'];
  //将产品标题作为查询条件来查询数据
  $info = ProductModel::where('title',$title)->find();
  //验证产品标题是否重复
  if($info == true){
   //重复返回提示信息
   return ['res'=>0,'msg'=>'产品标题重复!'];
  }
  //加入发布的时间
  $data['time'] = time();
  //加入发布产品的管理员名称
  $data['username'] = Session::get('username');
  //实例化模型
  $product = new ProductModel();
  //进行添加并验证
  if($product->save($data)){
   //返回发布成功的信息
   return ['res'=>1,'msg'=>'产品发布成功!'];
  }else{
   //返回发布失败的信息
   return ['res'=>0,'msg'=>'产品发布失败!'];
  }
 }
 public function edit(){
  //接受传递过来的产品id
  $proId = Request::param('id');
  //查询产品id对应的产品信息
  $product = ProductModel::get($proId);
  //将数据赋值到模板
  $this->view->product = $product;
  //渲染产品修改界面
  return $this->fetch();
 }
 public function DoEdit(){
  //获取提交过来的数据
  $data= Request::param();
  //加入发布的时间
  $data['time'] = time();
  //加入发布产品的管理员名称
  $data['username'] = Session::get('username');
  //进行修改操作  
  $product = new ProductModel();
  //验证修改结果
  $info = $product->save([
    'title'=>$data['title'],
    'desc'=>$data['desc'],
    'content'=>$data['content'],
    'once'=>$data['once'],
    'over_night'=>$data['over_night'],
    'username'=>$data['username'],
    'time'=>$data['time']
   ],['id'=>$data['id']]);
  if($info){
   //返回修改成功信息
   return ['res'=>1,'msg'=>'产品更新成功!'];
  }else{
   //返回修改失败信息
   return ['res'=>0,'msg'=>'产品更新失败!'];
  }
 }
 public function del()
 {
  //获取需要删除的产品id
  $proId = Request::param('id');
  //实例化模型
  $product = new ProductModel();
  //删除并验证
  if($product->destroy($proId)){
   //返回删除成功提示
   return ['res'=>1,'msg'=>'产品删除成功!'];
  }else{
   //返回删除失败信息
   return ['res'=>0,'msg'=>'产品删除失败!'];
  }
 }
}

?>


批改老师:天蓬老师批改时间:2018-12-23 10:24:52
老师总结:作业写的很好,注释很清晰明确,继续努力,加油

发布手记

热门词条