laravel+layui分页

范福
2023-12-01

前端部分

        
        <div class="layui-card-body">
            <form class="layui-form">
                <table class="layui-table" id="demo" lay-filter="test">
                    <colgroup>
                        <col width="200">
                    </colgroup>
                    <thead>
                    <tr>
                        <th>创建时间</th>
                    </tr>
                    </thead>
                    <tbody>
                        <?php foreach($optimals as $optimal): ?>
                    <tr>
                        <td>{{$optimal['created_at']}}</td>     
                        <td><div class="layui-btn layui-btn-sm layui-btn-danger"   onclick="">取消</div></td>
                    </tr>
                     <?php endforeach;?>
                    </tbody>
                </table>
                //分页
                <div id="test1"style="float: right"></div>
            </form>
        </div>
        
        
//js部分   layui分页
        $(function () {
            layui.use(['layer', 'form','laypage','table'], function(exports){
                var layer = layui.layer, form = layui.form, laypage = layui.laypage;
                var count =  "{{$total}}";   //总数
                var limit =  "{{$limit}}";  //每页显示的条数
                var current_page = "{{$current_page}}"; //当前的页码
                var url = "";
                laypage.render({
                    elem: 'test1',
                    count: count,
                    limit:limit,
                    curr:current_page,
                    //上一页、下一页 页数
                    jump: function(obj, first){
                        console.log(obj.curr); //得到当前页码,向服务端请求对应页的数据。
                        console.log(obj.limit); //得到每页显示的条数
                        if (url.indexOf("?") >= 0) {
                            url = url.split("?")[0] + "?page=" + obj.curr;//切割字符串的方
                        } else {
                            url = url + "?page=" + obj.curr;
                        }
                        if (!first) {
                     		//必须写在first下 否则会陷入死循环
                     		//跳转的url
                            location.href = url;
                        }
                    }
                });
            });
        });

php部分

 public function best_pro(Request $request){
        if ($request->method() === 'POST') {
            return;
        }
        $limit = $request->input('limit',2); // 每页条数
        $current_page = $request->input('page',1);//当前页
        $skip = ($current_page-1) < 0 ? 0:($current_page-1)*$limit;
        $optimal = Optimal::select('');
        $total = $optimal->count(); //总数据
        $optimals = $optimal->skip($skip)->limit($limit)->get();
        $total = Optimal::count(); //总数据
        return view('',compact('optimals','current_page','limit' ,'total'));
    }
 类似资料: