视图代码:

<!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);
} }

最新文章

  1. 用Redis实现Session功能
  2. Objective-C中的语法糖
  3. R12.2.6 installation failed with - Unable to rename database
  4. JavaScript学习笔记- 自定义滚动条插件
  5. 使用Redis来实现LBS的应用
  6. 怎么在eclipse里调试WebDriver的源代码(转)
  7. 第三百六十天 how can I 坚持
  8. java cglib动态代理原理及样例
  9. ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解
  10. nginx在linux下的目录结构
  11. Vowel Counting
  12. NSAttributedString in Swift
  13. AngularJS [ 快速入门教程 ]
  14. sqlite数据库的char,varchar,text,nchar,nvarchar,ntext的区别
  15. hadoop执行 报错
  16. Nginx加载ngx_pagespeed模块,加快网站打开的速度
  17. Android CPU耗电量测试
  18. MVC的多页面后台管理系统
  19. 基于9款CSS3鼠标悬停相册预览特效
  20. ansible--02

热门文章

  1. Python重载比较运算符
  2. iOS 七大手势之轻拍,长按,旋转手势识别器方法-赵小波
  3. MySQL 日志管理及备份与恢复
  4. python基础语法_9-1闭包 装饰器补充
  5. 如何在Kubernetes 里添加自定义的 API 对象(一)
  6. 大地坐标BLH转平面坐标xyh(高斯投影坐标正算) Java版
  7. 274-基于XC7V690T的3U VPX信号处理板
  8. opencv笔记-SimpleBlobDetector
  9. 静态分离 &amp; rewrit 重写 &amp; HTTPS
  10. 深入MySQL(三):MySQL的索引的应用