包子

木森技术分享

路漫漫其修远兮,吾将上下而求索。

您现在的位置是:网站首页 > THINKPHP

thinkphp5.1和layui分页

2021-03-13 19:05:24242

<script>
    layui.use('table', function(){
        var table = layui.table;

        //方法级渲染
        table.render({
            elem: '#mytable'
            ,url: '{:url("access/access_data")}'
            ,cols: [[
                {checkbox: true, fixed: true}
                ,{field:'id', title: 'ID', width:80, sort: true, fixed: true}
                ,{field:'access_name', title: '用户名', width:80}
                ,{field:'username', title: '用户名', width:80}
                ,{field:'password', title: '密码', width:80}
                ,{field:'parent_cat_name', title: '主分类', width:80}
                ,{field:'cat_name', title: '分类', width:80}
                ,{field:'login_post', title: '地址',width:80}

            ]]
            ,id: 'testReload'
            ,page: {


                limits:[5,10,15]//每页条数的选择项


            }
            ,height: 800
        });

        var $ = layui.$, active = {
            reload: function(){
                var find = $('#find');

                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        key: {
                            id: find.val()
                        }
                    }
                });
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

  thinkphp

public  function  access_data(){
     //返回入口


     $limit=$this->request->param('limit');
     $page=$this->request->param('page');
     $begin=($page-1)*$limit;
     $list=Db::name('access')->select();
     $count=count($list);
     $list=Db::name('access')->field('id,access_name,username,password,cat_id,login_post,status')
         ->order(['sort','cat_id'=>'asc'])->limit($begin,$limit)->select();



     //获取每页显示的条数

     foreach($list as $k=>$v){
         $cat=Db::name('category')->where('id',$v['cat_id'])->find();
         $list[$k]['cat_name']=$cat['cat_name'];
         $list[$k]['parent_cat_name']=Db::name('category')->where('id',$cat['parent_id'])->value('cat_name');

     }


//dump($list);
    // dump($count);
   //  exit;

     $data['code']=0;
     $data['msg']="";
     $data['data']=$list;
     $data['count']=$count;
//有的需要json处理
   return $data;



 }