这个结论源于layui分页启蒙所得;
table.render({
elem: '#test-table-reload'
,url: 'http://localhost:8080/orders/findAllOrdersForAdmin'
,headers: {'X-Authorization-With':window.sessionStorage.getItem('jwt')}
,cols: [
[
{checkbox: true, fixed: true}
,{field:'id', title: '订单号', width:180, sort: true, align:'center'}
,{field:'scenicName', title: '景点名称', width:130, align:'center'}
,{field:'userName', title: '姓名', width:120, align:'center'}
,{field:'phone', title: '手机', width:150, align:'center'}
,{field:'qty', title: '票数', width:100,sort: true, align:'center'}
,{field:'payment', title: '总价', width:100,sort: true, align:'center'}
,{field:'paytime', title: '下单时间', width:180,sort: true, align:'center',templet: '<div>{{createTime(d.paytime)}}</div>'}
,{field:'status', title: '状态',width:100, align:'center',
templet:function (d) {
if (d.status == 0){
return "拒绝";
}else if(d.status == 1) {
return "待发货";
}else if(d.status == 2) {
return "交易关闭";
}
}}
,{width:136, title:'操作', align:'center', toolbar: '#barlist'}
]
]
,page: true
, parseData: function (res) {
return {
"code": res.code,
"msg": res.msg,
"count": res.data.total,
"data": res.data.rows
};
}
,height: 470
});
分页的时候初始化页面的请求:
http://localhost:8080/orders/findAllOrdersForAdmin?page=1&limit=10
page=1&limit=10 是框架默认自带的,同时也是get请求。如果是post请求的话page=1&limit=10 就不在请求头,而在请求体payload,即formdata里面。不过两者用@RequestParam 都能获取前端传过来的值。 但是,之前直接在方法参数里面这么写
@RequestMapping("/findAllOrdersForAdmin")
@ResponseBody
public Result findAllOrdersForAdmin(int page, int limit, HttpSession session) {
return ordersService.findAll(page , size , session);
}
而可以使参数名(括号内的还是要对应)不对应,重点是不区分get和post请求:
@RequestMapping("/findAllOrdersForAdmin")
@ResponseBody
public Result findAllOrdersForAdmin( @RequestParam(value = "page" ,defaultValue = "1")int page, @RequestParam(value = "limit" ,defaultValue = "1")int size , HttpSession session) {
return ordersService.findAll(page , size , session);
}
最后,layui使用
parseData: function (res) {
return {
"code": res.code,
"msg": res.msg,
"count": res.data.total,
"data": res.data.rows
};
}
函数奏效的版本2.4以上听说,但是有时候有的2.6也不行,建议官网找。
|