文中的LayUIAdminPro版本为v2020.4.1。使用的软件是IDEA,你问我为什么不用vsCode?为了偷懒,就使用IDEA。
书接上篇,如果我们的项目中,查询数量需要另外调用的接口时,例如,我们项目中使用到find来查询数据,count方法来查询数量。那么,我们需要在table.render中再而外的发起一次请求来查询数量的总数。
下面是单独的查询数据。
table.render({
elem: '#LAY-stream-user-manage',
url: 'http://localhost:8001/api/user/find',
method: "post",
contentType: "application/json;charset=UTF-8",
dataType: "json",
where: {},
cols: [[
{type: 'checkbox', fixed: 'left'},
{field: 'id', title: 'User_id', templet: '#LAY-user-id'},
{field: 'nickname', title: 'User_name'},
{field: 'email', title: 'User_email'},
{field: 'loginTime', title: 'User_last_login_at', sort: true},
{field: 'enabled', title: 'User_status', templet: '#LAY-user-status'},
{title: 'Action', align: 'center', fixed: 'right', toolbar: '#table-content-list'}
]],
autoSort: false,
skin: "line",
page: {limit: 10, sort: 'id', desc: false, curr: 1, groups: 3},
limit: 10,
limits: [10, 15, 20, 25, 30],
text: {none: '查无数据'},
parseData: function (res) { //res 即为原始返回的数据
// 解析响应结果
return {
"code": 0 //解析接口状态
}
}
});
在parseData中,发起查询数量的请求。
table.render({
elem: '#LAY-stream-user-manage',
url: 'http://localhost:8001/api/user/find',
method: "post",
contentType: "application/json;charset=UTF-8",
dataType: "json",
where: {},
cols: [[
{type: 'checkbox', fixed: 'left'},
{field: 'id', title: 'User_id', templet: '#LAY-user-id'},
{field: 'nickname', title: 'User_name'},
{field: 'email', title: 'User_email'},
{field: 'loginTime', title: 'User_last_login_at', sort: true},
{field: 'enabled', title: 'User_status', templet: '#LAY-user-status'},
{title: 'Action', align: 'center', fixed: 'right', toolbar: '#table-content-list'}
]],
autoSort: false,
skin: "line",
page: {limit: 10, sort: 'id', desc: false, curr: 1, groups: 3},
limit: 10,
limits: [10, 15, 20, 25, 30],
text: {none: '查无数据'},
parseData: function (res) { //res 即为原始返回的数据
// 发送请求,得到总数
let total = 0;
admin.req({
contentType: "application/json;charset=utf-8",
dataType: "json",
method: "post",
async: false,
url: 'http://localhost:8001/api/user/count',
data: JSON.stringify({
'type': 'user' // 这里携带的参数需要和table.render中where的参数一致
}),
success: function (res) {
if (res.code == 200) {
total = res.data;
}
}
});
// 解析响应结果
return {
"code": 0, //解析接口状态
"msg": "", //解析提示文本
"count": total, //解析数据长度
"data": res.data //解析数据列表
}
}
});
从控制台可以看到,我们发起了两次请求,一次查数据,一次查总数。