LayUI Table组件 + Laravel 实现前端分页显示

首先上效果图:

是不是很熟悉 对,没错就是layui的Table组件 右上角三个 按钮分别是 显示部分字段,导出成CSV,和打印 是LayUI自带的 我们暂且不管他 ,LayUI最多支持 每页显示90条,如果要显示90条以上 就得去修改LayUI.js里面的源码了,扯回原题,底下的分页什么的 都是Table组件自带的 不用我们自己写 我们需要做的是按照他的需求传值(JSON)就可以了 

表格代码如下:

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

    dicTable =  table.render({
        elem: '#test'
        ,url:'http://localhost/laravel55/public/GetAllUser'
        ,parseData: function(res) { //res 即为原始返回的数据
            return {
                "code":'0', //解析接口状态
                "msg": res.message, //解析提示文本
                "count":res.total, //解析数据长度
                "data": res.data //解析数据列表
            }
        }
        ,toolbar: '#toolbarDemo'
        ,title: '后台用户管理'
        ,totalRow: true
        ,id:'idTest'
        ,cols: [[
            {type: 'radio', fixed: 'left'}
            ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
            ,{field:'UserName', title:'用户名', width:150, edit: 'text'}
            ,{field:'Phone', title:'手机', width:150, edit: 'text'}
            ,{field:'RootID', title:'权限', width:80,  edit: 'text' }
            ,{field:'State', title:'状态', width:80,
                sort: true}
        ]]
        ,page: true
    });

 后台返回JSON如下:

  1. current_page: 1
  2. data: [{UserName: "test1", id: 2, Phone: "13989573453", RootID: 0, State: "0"},…]
    1. 0: {UserName: "test2", id: 2, Phone: "13989573453", RootID: 0, State: "0"}
    2. 1: {UserName: "test3", id: 7, Phone: "15658219150", RootID: 1, State: "0"}
    3. 2: {UserName: "test5", id: 9, Phone: "15658219150", RootID: 1, State: "0"}
    4. 3: {UserName: "test6", id: 10, Phone: "15658219150", RootID: 1, State: "1"}
    5. 4: {UserName: "test7", id: 11, Phone: "15658219150", RootID: 1, State: "0"}
    6. 5: {UserName: "test8", id: 12, Phone: "15658219150", RootID: 1, State: "0"}
    7. 6: {UserName: "test9", id: 13, Phone: "15658219150", RootID: 1, State: "0"}
    8. 7: {UserName: "test10", id: 14, Phone: "15658219150", RootID: 1, State: "1"}
    9. 8: {UserName: "test12", id: 16, Phone: "15658219150", RootID: 1, State: "0"}
    10. 9: {UserName: "test13", id: 17, Phone: "15658219150", RootID: 1, State: "0"}
  3. first_page_url: "http://localhost/laravel55/public/GetAllUser?page=1"
  4. from: 1
  5. last_page: 3
  6. last_page_url: "http://localhost/laravel55/public/GetAllUser?page=3"
  7. next_page_url: "http://localhost/laravel55/public/GetAllUser?page=2"
  8. path: "http://localhost/laravel55/public/GetAllUser"
  9. per_page: "10"
  10. prev_page_url: null
  11. to: 10
  12. total: 21

PHP(Laravel)代码如下:

首先在路由器配置

Route::middleware(['userToken'])->match(['get'],'/getAllFudai','Admin\IndexController@getAllUser');//配置API

然后控制器代码:

public function GetAllUser(Request $request){//获取所有用户
   $limit=$request->get('limit');//前端Layui 传过来分页标准
    $phone=$request->get('phone');//前端传过来的手机号;
    $name=$request->get('name');//前端传过来的用户名
    if($phone!=''&& $name==''){
        $result=PcMode::select('UserName','id','Phone','RootID','State')->where(['Phone'=>$phone])->paginate($limit);
    }else if($phone!=''&& $name!=''){
      $result=PcMode::select('UserName','id','Phone','RootID','State')->where(["UserName"=>$name],["Phone"=>$phone])->paginate($limit);
    }else if($phone==''&& $name!=''){
        $result=PcMode::select('UserName','id','Phone','RootID','State')->where(['UserName'=>$name])->paginate($limit);
    }else{
        $result=PcMode::select('UserName','id','Phone','RootID','State')->paginate($limit);
    }
    return $result;
}

前端代码中 count 是记录总数(前端计算分页用) ,data是指定区间的记录数据,后端 limit 是前端设置的每页数据条数 ps:前端是GET请求,还有我写的请求 带中间件验证,要用的 自己修改,有什么不懂的 移步LayUI官方开发文档 或者给本人发消息

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bug克星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值