lavarel5.2中下拉菜单类型的搜索

来源:互联网 发布:spss编程抽样 编辑:程序博客网 时间:2024/06/14 19:46

1:视图层中

上面:

<select name="partment" id="partment" style="width:100px;">
                 <option value="">请选择</option>   //切记这个请选择option标签里value值一定为空的,不然分页和搜索不能同时实现。
                <?php foreach ($partment as $key => $value) {?>
                   <option value="{{$value->p_id}}" class="option">{{$value->partment}}</option>
                <?php }?>
                </select>
        <button type="submit" onclick="page(<?php echo $page?>)"class="btn btn-success" id="" name=""><i class="Hui-iconfont">&#xe665;</i> 搜部门</button>    
<script>

// 搜索分页
    function page(page){
        var search=document.getElementsByName('partment')[0].value;//获取选中下拉菜单值
        //alert(search);
        var ajax=new XMLHttpRequest();
        ajax.open('get','{{url('ad_list')}}?page='+page+'&search='+search);
        ajax.send();
        ajax.onreadystatechange=function(){
            if(ajax.readyState==4&ajax.status==200){
                document.getElementById('div1').innerHTML=ajax.responseText;
            }

        }
    }

</script>

2:控制器中查询方法;

public function ad_list(){
            $partment = DB::table('mr_partment')->get();

        //echo 66666;die;
        //分页查询开始
        $search = isset($_GET['search'])?$_GET['search']:"";//判断是否接过搜索值
       if($search)
       {
           //$arr = DB::select("select * from mr_work where title like '%$search%'");//带搜索条件查询
           $arr=DB::table('mr_work')//主表
                 ->join('mr_partment', 'mr_work.p_id', '=', 'mr_partment.p_id')
                 ->select('mr_work.*', 'mr_partment.partment')
                 ->where ('mr_work.p_id','like','%'.$search.'%')
                 ->get();//搜索多表查询

       }else{
           //$arr = DB::table('mr_work')->get();//不带搜索条件查询
           $arr=DB::table('mr_work')//主表
                 ->join('mr_partment', 'mr_work.p_id', '=', 'mr_partment.p_id')
                 ->select('mr_work.*', 'mr_partment.partment')
                 ->get();//搜索多表查询
       }
       //总条数
       $count = count($arr);
       //每页条数
       $mys = 3 ;
       //计算总页数
       $zys = ceil($count/$mys);
       //获取当前页
       $page = isset($_GET['page'])?$_GET['page']:1;
       //偏移量
       $pyl = ($page-1)*$mys;
       if($search)
       {
           //$arr = DB::select("select * from mr_work where title like '%$search%' limit $pyl,$mys");//带搜索分页条件查询
            $arr=DB::table('mr_work')//主表
                 ->join('mr_partment', 'mr_work.p_id', '=', 'mr_partment.p_id')
                 ->select('mr_work.*', 'mr_partment.partment')
                 ->where ('mr_work.p_id','like','%'.$search.'%')
                 ->skip($pyl)
                 ->take($mys)
                 ->get();//搜索分页多表查询
       }else{
           //$arr = DB::table('mr_work')->skip($pyl)->take($mys)->get();//带分页条件查询
            $arr=DB::table('mr_work')//主表
                 ->join('mr_partment', 'mr_work.p_id', '=', 'mr_partment.p_id')
                 ->select('mr_work.*', 'mr_partment.partment')
                 ->skip($pyl)
                 ->take($mys)
                 ->get();//搜索分页多表查询
       }
       //上一页
       $last = $page-1<1?1:$page-1;
       //下一页
       $next = $page+1>$zys?$zys:$page+1;
       // print_R($next);die;
       return view('Ad/ad_list',['partment'=>$partment,'result'=>$arr,'search'=>$search,'last'=>$last,'next'=>$next,'sum_page'=>$zys,'page'=>$page,'count'=>$count]);
       //分页查询结束
       
    }

0 0
原创粉丝点击