视图代码:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css"> </head> <body> <form action="/homework/homework/index" method="get"> <input type="text" name="name" placeholder="请根据姓名进行搜索"> <input type="text" name="price" placeholder="请根据价格进行搜索"> <input type="submit" value="立即搜索"> </form> <input type="button" value="批量删除" onclick="allDel()"> <table class="table"> <tr> <td><input type="checkbox" id="allChecked"></td> <td>商品名称</td> <td>商品图片</td> <td>商品分类</td> <td>状态</td> <td>商品库存</td> <td>本店售价</td> <td>操作</td> </tr> {foreach $data as $k=>$v} <tr id="del"> <td><input type="checkbox"></td> <td>{$v.name}</td> <td><img src="{$v.img}" alt=""></td> <td>{$v.cate}</td> <td> {if $v.put==1} <span onclick="upper({$v.id})" class="upper{$v.id}" put="1">上架</span> {else} <span onclick="upper({$v.id})" class="upper{$v.id}" put="2">下架</span> {/if} </td> <td>{$v.reserve}</td> <td>{$v.price}</td> <td> <a href="{:url('homework/homework/edit',['id'=>$v.id])}">编辑</a> <a href="javascript:void(0)" onclick="del({$v.id})">删除</a> </td> </tr> {/foreach} </table> {$data->render()} </body> </html> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> function upper(id) { // 1表示的上架 2 表示的下架 var put = $('.upper'+id).attr('put');//1 var text = $('.upper'+id).text();//上架 //判断 if (put == 1 ) { put = 2; text = '下架'; } else { put = 1; text = '上架'; } $.ajax({ url: '/homework/homework/put', type: 'POST', data: { id: id, put: put, }, dataType: 'JSON', success:function (res){ console.log(res) if (res.code==200){ $('.upper'+id).text(text); $('.upper'+id).attr('put',put) } } }) } //批量删除 function allDel() { if (confirm('确定要删除吗?')) { //获取选中的多选框 var checks = $(':checkbox:checked'); $(checks).each(function (k, v) { $(v).parents('tr').remove(); }) } } //全选 $("#allChecked").click(function () { $(":checkbox").each(function (k, v) { v['checked'] = true }); }) function del(id) { $.ajax({ url: '/homework/homework/delete/id/' + id, type: 'GET', dataType: 'json', success: function (res) { if (res.code == 200) { alert('删除成功'); $('#del').remove(); } } }) } </script>
器代码:
<?php namespace app\homework\controller; use app\homework\model\HomeworkModel; use think\Controller; use think\Request; class Homework extends Controller { /** * 显示资源列表 * * @return \think\Response */ public function index() { //接受搜索关键字 $name = input('name'); $price=input('price'); $data = HomeworkModel::listInfo($name,$price); if (!empty($name)){ foreach ($data as $k=>$v){ $v['name']=str_replace($name,"<font color='red'> $name</font>",$v['name']); } } if (!empty($price)){ foreach ($data as $k=>$v){ $v['price']=str_replace($price,"<font color='red'> $price</font>",$v['price']); } } $this->assign('data', $data); return view(); } /** * 显示创建资源表单页. * * @return \think\Response */ public function create() { // return view(); } /** * 保存新建的资源 * * @param \think\Request $request * @return \think\Response */ public function save(Request $request) { // $params = $request->param(); $file = $request->file('img'); if ($file) { $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'); if ($info) { // 成功上传后 获取上传信息 // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg $filename = '/uploads/' . $info->getSaveName(); $image = \think\Image::open('.' . $filename); // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png $image->thumb(150, 150)->save('.' . $filename); $params['img'] = $filename; } else { // 上传失败获取错误信息 echo $file->getError(); } } $result = HomeworkModel::add($params); if (!$result) { $this->error('添加失败', '/homework/homework/save'); } $this->success('添加成功', '/homework/homework/index'); } /** * 显示指定的资源 * * @param int $id * @return \think\Response */ public function read($id) { // } /** * 显示编辑资源表单页. * * @param int $id * @return \think\Response */ public function edit($id) { // $data = HomeworkModel::showOneInfo($id); $this->assign('data', $data); return view(); } /** * 保存更新的资源 * * @param \think\Request $request * @param int $id * @return \think\Response */ public function update(Request $request, $id) { // } /** * 删除指定资源 * * @param int $id * @return \think\Response */ public function delete($id) { // $res = HomeworkModel::del($id); return json(['code' => 200, 'msg' => 'success', 'data' => $res]); } public function put(Request $request){ $post=request()->post(); $updata=[ 'put'=>$post['put'] ]; $id=$post['id']; //修改数据 $result=HomeworkModel::put($updata,$id); if ($result){ return json(['code'=>200,'meg'=>'success','data'=>$updata]); }else{ return json(['code'=>500,'msg'=>'error','data'=>'']); } } }
模型代码:
<?php namespace app\homework\model; use think\Model; class HomeworkModel extends Model { //连接表名 protected $table = 'goodsave'; //商品添加 public static function add($params) { return self::create($params, true); } //商品分页搜索 public static function listInfo($name, $price) { $config = [ 'query' => ['name' => $name, 'price' => $price], ]; $model = new self(); if (isset($name)) { $model = $model->where('name', 'like', "%$name%"); } if (isset($price)) { $model = $model->where('price', 'like', "%$price%"); } $data = $model->paginate(2, false, $config); return $data; } //编辑 public static function showOneInfo($id) { return self::find($id); } //删除 public static function del($id) { return self::destroy($id); } //修改状态put public static function put($updata, $id) { return self::update($updata, ['id' => $id], true); } }