后台接口
namespace App\Http\Controllers;
use App\Show;
use Illuminate\Http\Request;
class ShowController extends Controller
{
//
public function index(Request $request){
//判断是不是ajax 请求
if (request()->ajax()) {
$data = Show::paginate(5)->toArray();
return $data;
} else {
$data = Show::paginate(5)->toArray();
return view('show.index',compact('data'));
}
}
public function del(Request $request){
$ids =$request->id; //接过来传过来的id字符串
$data = explode(',', $ids);//字符串分割数组
Show::destroy($data); //使用函数destroy 批量删除 注意数据库字段的 是 id 否则删除失败
$data = Show::paginate(5);
return $data;
}
}
返回的数据
<!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">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<title>Document</title>
</head>
<body align="center">
<table align="center">
<thead>
<tr>
<td></td>
<td>id</td>
<td>名称</td>
<td>数量</td>
<td>价格</td>
</tr>
</thead>
<tbody id='tbody'>
@foreach ($data['data'] as $v)
<tr>
<td><input type="checkbox" name="box" value="{{ $v['id'] }}"></td>
<td>{{ $v['id']}}</td>
<td>{{ $v['cart_name']}}</td>
<td>{{ $v['num']}}</td>
<td>{{ $v['price']}}</td>
</tr>
@endforeach
</tbody>
</table>
<div id="fen">
<a path="{{$data['first_page_url']}}" class="page">首页</a>
<a path="{{$data['prev_page_url']}}" class="page" >上一页</a>
<a path="{{$data['next_page_url']}}" class="page" >下一页</a>
<a path="{{$data['last_page_url']}}" class="page">尾页</a>
</div>
<br>
<button id='pi'>批删</button>
</body>
</html>
<script type="text/javascript">
$(document).on('click','.page',function(){
var path = $(this).attr('path');
console.log(path);
$.get(path,function(res){
console.log(res);
str = "";
arr = "";
$.each(res.data,function(k,v){
str += "<tr>";
str += '<td><input type="checkbox" name="box" value=" ' + v.id + '"></td>';
str += "<td>"+ v.id + "</td>";
str += "<td>"+ v.cart_name + "</td>";
str += "<td>"+ v.num + "</td>";
str += "<td>"+ v.price + "</td>";
str += "</tr>";
})
arr += '<a path="'+res.first_page_url+'" class="page">首页</a>';
arr += '<a path="'+res.prev_page_url+'" class="page">上一页</a>';
arr += '<a path="'+res.next_page_url+'" class="page">下一页</a>';
arr += '<a path="'+res.last_page_url+'" class="page">尾页</a>';
$('#tbody').html(str);
$('#fen').html(arr);
})
})
$("#pi").on("click", function(){
var ids ="";
$("[name=box]:checked").each(function() {
ids += ',' + $(this).val();
});
ids = ids.substr(1);
var url = "{{route('show.del') }}"
$.get(url,{id:ids},function(res){
console.log(res);
str = "";
$.each(res.data,function(k,v){
str += "<tr>";
str += '<td><input type="checkbox" name="box" value=" ' + v.id + '"></td>';
str += "<td>"+ v.id + "</td>";
str += "<td>"+ v.cart_name + "</td>";
str += "<td>"+ v.num + "</td>";
str += "<td>"+ v.price + "</td>";
str += "</tr>";
})
$('#tbody').html(str);
});
});
</script>