首先上效果图:
是不是很熟悉 对,没错就是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如下:
- current_page: 1
- data: [{UserName: "test1", id: 2, Phone: "13989573453", RootID: 0, State: "0"},…]
- 0: {UserName: "test2", id: 2, Phone: "13989573453", RootID: 0, State: "0"}
- 1: {UserName: "test3", id: 7, Phone: "15658219150", RootID: 1, State: "0"}
- 2: {UserName: "test5", id: 9, Phone: "15658219150", RootID: 1, State: "0"}
- 3: {UserName: "test6", id: 10, Phone: "15658219150", RootID: 1, State: "1"}
- 4: {UserName: "test7", id: 11, Phone: "15658219150", RootID: 1, State: "0"}
- 5: {UserName: "test8", id: 12, Phone: "15658219150", RootID: 1, State: "0"}
- 6: {UserName: "test9", id: 13, Phone: "15658219150", RootID: 1, State: "0"}
- 7: {UserName: "test10", id: 14, Phone: "15658219150", RootID: 1, State: "1"}
- 8: {UserName: "test12", id: 16, Phone: "15658219150", RootID: 1, State: "0"}
- 9: {UserName: "test13", id: 17, Phone: "15658219150", RootID: 1, State: "0"}
- first_page_url: "http://localhost/laravel55/public/GetAllUser?page=1"
- from: 1
- last_page: 3
- last_page_url: "http://localhost/laravel55/public/GetAllUser?page=3"
- next_page_url: "http://localhost/laravel55/public/GetAllUser?page=2"
- path: "http://localhost/laravel55/public/GetAllUser"
- per_page: "10"
- prev_page_url: null
- to: 10
- 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官方开发文档 或者给本人发消息