<?php namespace App\Http\Controllers; use App\Demo1; use DB; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller as BaseController; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Support\Facades\Input; class Demo1Controller extends Controller { /** * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View * 列表展示 */ public function index(){ if(Input::get('username')!='' ){ $search=Input::get('username'); $show = DB::table('demo1')->where('username','like',"%$search%")->paginate(5); }else{ $search=''; $show = DB::table('demo1')->paginate(5); } return view('Demo1/index',['show'=>$show,'search'=>$search]); } /** * 删除 */ public function del(){ $id=Input::get('id'); $del=new Demo1(); $arr=$del->getDel($id); if($arr){ echo "<script>alert('删除成功');location.href='index'</script>"; }else{ echo "<script>alert('删除失败');location.href='index'</script>"; } } /** * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|void * 批量删除 */ public function dels(){ $ids = Input::get("ids"); $str = explode(",",$ids); foreach($str as $v){ DB::table('demo1')->where('id',"=","$v")->delete(); } echo "<script>alert('删除成功');location.href='index'</script>"; } public function up(){ $aaa=Input::get("bbb"); $id=Input::get("id"); $up=new Demo1(); $up->getup($id,$aaa); } }
<?php namespace App; use DB; use Illuminate\Foundation\Auth\User as Authenticatable; class Demo1 extends Authenticatable { /** * @param $where * @param $search * @return mixed * 列表展示 */ public static function getDemo($where,$search){ // foreach($where as $key => $value){ // $where[$key]['username'] = str_replace($search,"<font color='red'>$search</font>",$value['username']); // } // print_r($where); return DB::table('demo1')->where($where)->paginate(5); } /** * @param $id * @return mixed * 单删 */ public static function getDel($id){ return DB::table('demo1')->where('id',$id)->delete(); } /** * @param $id * @param $aaa * @return mixed * 修改 */ public static function getup($id,$aaa){ return DB::table("demo1")->where("id",$id)->update(['age'=>$aaa]); } }
<!doctype html> <html lang="en" id="box"> <head> <meta charset="UTF-8"> <title>列表展示</title> <style> ul{ margin-left: 150px; } li{ list-style: none;float:left;align-content: center; margin: 5px; } </style> </head> <body> <form action="index" method="get"> <input type="text" name="username" value="{{ $search or '' }}"> <input type="submit" value="搜索"> <input type="reset" value="取消"> </form> <table border="1"> <tr> <td><a href="#" οnclick="checkAll()">全选</a></td> <td>ID</td> <td>姓名</td> <td>密码</td> <td>年龄</td> <td>性别</td> <td>身高</td> <td>描述</td> <td>操作</td> </tr> @foreach ($show as $shows) <tr id="{{$shows->id}}"> <td><input type="checkbox" name="checkpre" value="{{$shows->id}}"></td> <td>{{$shows->id}}</td> <td><span class="aaa"><?php $pos = strrpos($shows->username,$search); if($pos==0 || $pos){ $pos=true; }else{ $pos=false; } if($search!='' && $pos){ $shows->username=str_ireplace($search,"<span style='color: red'>".$search."</span>",$shows->username) ; echo "<span style='cursor: pointer'> $shows->username </span>"; }else{ echo "<span style='cursor: pointer'>$shows->username </span>"; } ?></span></td> <td>{{$shows->pwd}}</td> <td><span class="bbb">{{$shows->age}}</span></td> <td>{{$shows->sex}}</td> <td>{{$shows->height}}</td> <td>{{$shows->desc}}</td> <td><a href="del?id={{$shows->id}}">删除</a></td> </tr> @endforeach </table> <tr> <td><a href="#" οnclick="check_fx()">反选</a></td> <td><a href="#" οnclick="check_qx()">取消</a></td> <td><a href="#" οnclick="delAll()">批量删除</a></td> </tr> {!! $show->appends($search)->links() !!} </body> </html> <script language="JavaScript" src="{{ URL::asset('/') }}jq/jq.js"></script> <script> //全选 function checkAll() { var checkpre=document.getElementsByName("checkpre"); for(var i=0;i<checkpre.length;i++){ checkpre[i].checked='checked'; } } //反选 function check_fx() { var checkpre=document.getElementsByName("checkpre"); for(var i=0;i<checkpre.length;i++){ checkpre[i].checked=!checkpre[i].checked; } } //取消 function check_qx() { var checkpre=document.getElementsByName("checkpre"); for(var i=0;i<checkpre.length;i++){ checkpre[i].checked=false; } } //批删 function delAll() { var checkpre=document.getElementsByName("checkpre"); var ids=""; for(var i=0;i<checkpre.length;i++){ if(checkpre[i].checked){ ids+=checkpre[i].value+','; } } //干掉逗号 ids=ids.substr(0,ids.lastIndexOf(',')); $.ajax({ type:"get", url:"dels", data:{ids:ids}, success: function(data){ $("#box").html(data); } }) } //即点即改修改文本框 $(document).on("click",".bbb",function () { //或取要修改的值 var bbb=$(this).html(); //赋值 nsme值 value值 $(this).parent().html("<input type='text' name='bbb' value='"+bbb+"'>"); //光标落入 $("input").focus(); //失去焦点 保存值 $(document).on("blur","input[name='bbb']",function () { var obj=$(this); //获取id值 var id=$(this).parents("tr").attr("id"); //获取aaa新写的值 var bbb=$(this).val(); $.ajax({ type:"get", url:"up", data:{ id:id, bbb:bbb }, success: function(data){ // alert(data) obj.parent().html("<span class='bbb'>"+bbb+"</span>"); } }) }) }) </script>