天天看点

thinkphp5 ajax无刷新分页

thinkphp5 ajax 无刷新分页效果的实现

1.在做paginate分页的时候,修改a链接

$res = db("xxx")->where("1=1")->paginate(1,false,['path'=>'javascript:AjaxPage([PAGE]);']);
           

2.js写ajaxPage方法去调用ajax

//无刷新分页
            function AjaxPage(page){
                $.get(
                    '/index.php/admin/Student/getPage',
					{ page:page },
		function (data) {
                        var html = "";
                        $(data.data.data).each(function (i,d) {

                             html += "<tr>\n" +
                                 "<th>资助人</th>\n" +
                                 "<th>学生姓名</th>\n" +
                                 "<th>性别</th>\n" +
                                 "<th>出生日期</th>\n" +
                                 "<th>家庭人数</th>\n" +
                                 "<th>家庭年收入</th>\n" +
                                 "<th>望助金额(学期)</th>\n" +
                                 "<th>操作</th>\n" +
                                 "</tr>";
                             html += "<tr>\n" +
                                 "<td>"+ d.u_id +"</td>\n" +
                                 "<td>"+ d.name +"</td>\n" +
                                 "<td>"+ d.sex +"</td>\n" +
                                 "<td>"+ d.birthday.substring(0,10)+"</td>\n" +
                                 "<td>"+ d.family_num +"</td>\n" +
                                 "<td>"+ d.family_income +"</td>\n" +
                                 "<td>" + d.money + "</td>\n" +
                                 "<td>\n" +
                                 "<div class=\"button-group\">\n" +
                                 "<a class=\"button border-main\" href=\"javascript:vaid(0);\" οnclick=\"Delivery_stop(this,"+ d.id +")\"><span class=\"icon-plus\"></span> 上传汇款记录</a>\n" +
                                 "<a class=\"button border-green\" href=\"/index.php/admin/Student/student_eye/id/"+ d.id +"\"><span class=\"icon-eye\"></span> 查看</a>\n" +
                                 "<a class=\"button border-main\" href=\"/index.php/admin/Student/student_list_add/id/"+ d.id +"\"><span class=\"icon-edit\"></span> 编辑</a>\n" +
                                 "<a class=\"button border-red\" href=\"/index.php/admin/Student/student_del/id/"+ d.id +"\" οnclick=\"return del()\"><span class=\"icon-trash-o\"></span> 删除</a>\n" +
                                 "</div>\n" +
                                 "</td>\n" +
                                 "</tr>";
                        });
                        html +=  "<tr>\n" +
                            "<td colspan=\"10\"><div class=\"pagelist\"> "+ data.ajaxPage +" </div></td>\n" +
                            "</tr>";
                    $('.ajaxPage').html(html);
                })
            }
           

3.ajax访问的控制器代码

/**
     * ajax 无刷新分页
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function getPage()
    {
        if(input("page")){
            $data['data'] = db("xxx")->where("1=1")->paginate(1,false,['page'=>input("page"),'path'=>'javascript:AjaxPage([PAGE]);']);
            $data['ajaxPage'] =  $data['data']->render();
            return $data;
        }

    }
           

继续阅读