• laravel——ajax分页&删除&搜索


    一、视图代码

    /*搜索*/
    <form action="javascript:search_brand()" name="searchForm">
    <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
    <input type="text" name="brand_name" size="15" placeholder="请输入品牌名称查找" id="brand_name">
    <input type="button" value=" 搜索 " onclick="page()">
    </form>

    /*删除*/
    <input type="button" onclick="del({{$v->brand_id}})" value="删除">

    /*按钮*/
    <div id="turn-page">
    总计<span id="totalRecords"><?php echo $count?></span>个记录&nbsp;&nbsp;&nbsp;
    分为<span id="totalPages"><?php echo $page_num?></span>页&nbsp;&nbsp;&nbsp;
    当前第<span id="pageCurrent">1</span>页
    <span id="page-link">
    <a href="javascript:void(0)" onclick="page(1)">首页</a>
    <a href="javascript:void(0)" onclick="page({{$prev}})">上一页</a>
    <a href="javascript:void(0)" onclick="page({{$next}})">下一页</a>
    <a href="javascript:void(0)" onclick="page({{$page_num}})">尾页</a>
    </span>
    </div>
    <script src="{{URL::asset('js/jquery.js')}}"></script>
    <script>
    function page(page) {
    var brand_name = $('#brand_name').val();
    $.ajax({
    type:'get',
    url:'brand_list',
    data:{page:page,brand_name:brand_name},
    headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')},
    success:function (e) {
    $('body').html(e);
    }
    })
    }

    function del(brand_id) {
    var brand_name = $('#brand_name').val();
    $.ajax({
    type:'get',
    url:'brand_del',
    data:{brand_id:brand_id,brand_name:brand_name},
    headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')},
    success:function (msg) {
    $("body").html(msg);
    }
    })
    }
    </script>

      二、控制器代码

    public function brand_list()
        {
            $where = '1';
            $brand_name = Input::get('brand_name');
            if (!empty($brand_name)){
                $where = "brand_name like '%$brand_name%' ";
            }
            $count = count(DB::table('brand')->get());//总条数
            $limit = 4;//每页显示条数
            $page = Input::get('page');//当前页
            if(empty($page))
            {
                $page = 1;
            }
            $page_num = ceil($count/$limit);//总页数
            $py = ($page-1)*$limit;//偏移量
            $prev = ($page-1)>0?$page-1:1;//上一页
            $next = ($page+1)<$page_num?$page+1:$page_num;//下一页
            $data = DB::select("select * from brand where $where limit $py,$limit");
            return view('Brand/brand_list',['data'=>$data,'count'=>$count,'prev'=>$prev,'next'=>$next,'page_num'=>$page_num]);
        }
  • 相关阅读:
    窗口显示于parent控件上
    DELPHI SOCKET 通信编程要点小结
    dxBarManagerToDxNavBar方法
    DLL直接返回对象
    海量数据库的查询优化及分页算法方案
    excel怎么只打印某页?excel怎么只打印某几页
    HTTP请求错误400、401、402、403、404、405、406、407、412、414、500、501、502解析
    excel中如何设置只打印第一页
    Navicat Premium 常用功能讲解
    laravel查询构造器DB还是ORM,这两者有什么区别,各该用在什么场景中
  • 原文地址:https://www.cnblogs.com/wxy0126/p/10698425.html
Copyright © 2020-2023  润新知