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');

页面效果

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值