1 <?php 2 3 namespace AppHttpControllersadmin; 4 5 use IlluminateHttpRequest as request; 6 use AppHttpControllersController; 7 use IlluminateSupportFacadesDB; 8 9 class UserController extends Controller 10 { 11 /** 12 * @return IlluminateContractsViewFactory|IlluminateViewView| hink esponseView 13 * 渲染數據展示頁面 14 */ 15 public function showlist(request $request) 16 { 17 //設置頁數,從第一頁開始 18 $page = $request->post('page') ? $request->post('page') : 1; 19 //每頁顯示條數 20 $size = 3; 21 //查詢數據總條數 22 $count = count(DB::select("select * from week_type")); 23 //查詢數據總頁數 24 $end = ceil($count / $size); 25 //計算偏移量 26 $offset = ($page-1)*$size; 27 //查詢所欲數據 28 $data = DB::select("select * from week_type limit $offset,$size"); 29 //傳送數據到後台 30 return view('admin.showlist',['data'=>$data,'end'=>$end,'count'=>$count]); 31 } 32 33 /** 34 * @param request $request 35 * ajax分頁 & 搜索後分頁 36 */ 37 public function ajaxshowlist(request $request) 38 { 39 //根據分類名稱搜索接收值 40 $search = $request->post('search'); 41 if (empty($search)){ 42 //設置頁數,從第一頁開始 43 $page = $request->post('page') ? $request->post('page') : 1; 44 //每頁顯示條數 45 $size = 3; 46 //查詢數據總條數 47 $count = count(DB::select("select * from week_type")); 48 //查詢數據總頁數 49 $end = ceil($count / $size); 50 //計算偏移量 51 $offset = ($page-1)*$size; 52 //查詢所欲數據 53 $data = DB::select("select * from week_type limit $offset,$size"); 54 //傳送數據到前台 55 return json_encode($data); 56 }else{ 57 //設置頁數,從第一頁開始 58 $page = $request->post('page') ? $request->post('page') : 1; 59 //每頁顯示條數 60 $size = 3; 61 //查詢數據總條數 62 $count = count(DB::select("select * from week_type")); 63 //查詢數據總頁數 64 $end = ceil($count / $size); 65 //計算偏移量 66 $offset = ($page-1)*$size; 67 //查詢所欲數據 68 $data = DB::select("select * from week_type where type_name like '%$search%' limit $offset,$size"); 69 //傳送數據到前台 70 return json_encode($data); 71 } 72 } 73 /** 74 * ajax 刪除 75 */ 76 public function ajax_del(request $request) 77 { 78 //接收id 79 $id = $request->get('id'); 80 //刪除語句 81 $data = DB::delete("delete from week_type where id='$id'"); 82 //刪除後查詢數據庫 83 if ($data){ 84 DB::select("select * from week_type"); 85 } 86 return 1; 87 } 88 }
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" 6 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title>展示頁面</title> 9 <link rel="stylesheet" href="{{asset('css/bootstrap.css')}}"> 10 </head> 11 <body> 12 <center><h1>數據展示頁面</h1></center> 13 <input type="text" name="search" id="search"><input type="button" class="page" value="搜索"> 14 <table class="table table-striped"> 15 <tr> 16 <td>編號</td> 17 <td>分類</td> 18 <td>分類名稱</td> 19 <td>狀態</td> 20 <td>操作</td> 21 </tr> 22 <tbody id="tb"> 23 @foreach($data as $key=>$val) 24 <tr> 25 <td>{{$val->id}}</td> 26 <td>{{$val->type}}</td> 27 <td>{{$val->type_name}}</td> 28 <td>{{$val->state}}</td> 29 <td><a href="#" id="{{$val->id}}" class="del">刪除</a></td> 30 </tr> 31 @endforeach 32 </tbody> 33 </table> 34 <input type="hidden" name="a_page" id="a_page" value="1"> 35 <input type="hidden" name="last_page" id="last_page" value="{{$end}}"> 36 <a href="javascript:void (0)" class="page">首頁</a> 37 <a href="javascript:void (0)" class="page">上一頁</a> 38 <a href="javascript:void (0)" class="page">下一頁</a> 39 <a href="javascript:void (0)" class="page">尾頁</a> 40 41 共{{$end}}頁 42 43 合計:{{$count}}條 44 </body> 45 </html> 46 <script src="../js/jquery-3.3.1.min.js"></script> 47 <script> 48 //ajax刪除 49 $(document).on("click",".del",function () { 50 var id = $(this).attr('id'); 51 $.ajax({ 52 url:"ajax_del", 53 type:"get", 54 dataType:"json", 55 data:{ 56 id:id, 57 }, 58 success:function (data) { 59 if (data==1){ 60 alert("刪除成功") 61 location.href = ""; 62 } else { 63 alert("刪除失敗") 64 } 65 } 66 }) 67 }) 68 //ajax分頁 & 搜索 69 $(document).on("click",".page",function () { 70 var search = $("#search").val(); 71 var a_val = $(this).text(); 72 var a_page = $("#a_page").val(); 73 var last_page = $("#last_page").val(); 74 if (a_val=="首頁"){ 75 var page = 1; 76 } else if (a_val=="上一頁"){ 77 var page = parseInt(a_page)-1 < 1 ? 1 : parseInt(a_page)-1; 78 } else if (a_val=="下一頁"){ 79 var page = parseInt(a_page)+1 > last_page ? last_page : parseInt(a_page)+1; 80 } else if (a_val=="尾頁"){ 81 var page = last_page; 82 }else { 83 var page = 1; 84 } 85 $.ajax({ 86 url:"ajaxshowlist", 87 type:"post", 88 dataType:"json", 89 data:{ 90 page:page, 91 search:search, 92 }, 93 success:function (data) { 94 var str = ""; 95 $.each(data,function (key,val) { 96 str+='<tr>' 97 str+='<td>'+val.id+'</td>' 98 str+='<td>'+val.type+'</td>' 99 str+='<td>'+val.type_name+'</td>' 100 str+='<td>'+val.state+'</td>' 101 str+='<td><a href="#" id="'+val.id+'" class="del">刪除</a></td>' 102 str+='</tr>' 103 }) 104 $("#tb").html(str); 105 $("#a_page").val(page); 106 } 107 }) 108 }) 109 </script>