ThinkPHP5分页paginate之ajax异步请求

aaa.html视图

<!DOCTYPE HTML>
<html>
<head>
	<meta charset="utf-8">
    <title>{$name}</title>
    <link href="__STATIC__/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div>{$name}</div>
<div>
    <table border="1">
        <thead>
            <tr>
                <th  style="width: 60px">ID</th>
                <th  style="width: 160px">名称</th>
            </tr>
        </thead>
        <tbody  id="content">
        </tbody>
    </table>
    <div id="page"></div>
</div>

</body>
<script src="__STATIC__/js/jquery/1.9.1/jquery.js"></script>
<script>
    window.THINK_ROOT = '{:\\think\\Request::instance()->root()}';
    function Tp5AjaxPage(page) {
        $.ajax({
            url:THINK_ROOT+'/index/bbb',
            type:'post',
            dataType:'json',
            data: {page:page},
            success:function(res){
                console.log(res);
                var list = res.data;
                if(list.length !== 0){
                    $("#content").empty();
                    for(var key in list) {
                        $("#content").append('<tr>' +
                            '<td>'+list[key].id+'</td>' +
                            '<td>'+list[key].name+'</td>' +
                            '</tr>'
                        );
                    }
                    $("#page").html(res.page_html);
                }
            }
        });
    }
    //初始化
    Tp5AjaxPage(1);
</script>
</html>

Index.php控制器

class Index extends Controller
{

    public function aaa(){
        $this->view->assign('name','thinkphp5分页paginate之ajax请求');
        return $this->view->fetch();
    }
    public function bbb(){
        $page = 1;
        if(request()->param('page')){
            $page = request()->param('page');
        }
        $config = [
            'page' => $page,
            'var_page' => 'page',
            'path'=>'javascript:Tp5AjaxPage([PAGE]);',
        ];
        $listRows = 1;
        $res = Db::name('sys_role')->paginate($listRows,false,$config);
        $list = $res->toArray();
        $list['page_html'] = $res->render();
        return json($list);
    }
}

sys_role.sql数据来源

-- ----------------------------
-- Table structure for sys_role
-- ----------------------------
DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role` (
  `id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
  `pid` smallint(6) unsigned NOT NULL DEFAULT '0' COMMENT '父级id',
  `name` varchar(20) NOT NULL DEFAULT '' COMMENT '名称',
  `remark` varchar(255) NOT NULL DEFAULT '' COMMENT '备注',
  `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态',
  `isdelete` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `create_time` int(11) unsigned NOT NULL DEFAULT '0',
  `update_time` int(11) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `parentId` (`pid`) USING BTREE,
  KEY `status` (`status`) USING BTREE,
  KEY `isdelete` (`isdelete`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='RBAC权限模块';

-- ----------------------------
-- Records of sys_role
-- ----------------------------
INSERT INTO `sys_role` VALUES ('1', '0', '开发者', ' ', '1', '0', '1208784792', '1501434409');
INSERT INTO `sys_role` VALUES ('2', '0', '超级管理员', ' ', '1', '0', '1215496283', '1521102200');
INSERT INTO `sys_role` VALUES ('3', '0', '普通管理员', '', '1', '0', '1215496283', '1521102195');

页面效果

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页