laravel ajax分页,批删

后台接口

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值