使用数据表在 Sails 中进行服务器端分页
Server-side Pagination in Sails with DataTables
我目前正在使用DataTables的服务器端选项,我目前的问题是数据table中的页码只显示1。这里进一步解释是我的代码:
Javascript:
var oTable1 = $('#users-table').dataTable({
"bDestroy": true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/v1/users",
"bPaginate": true,
"aoColumns": [
{ "mData": "id" },
{ "mData": "username" },
{ "mData": "name" },
{ "mData": "acctype" },
{ "mData": "country" },
{ "mData": "createdAt" },
{ "mData": "button" }
]
});
/v1/users:
viewAll: function(req, res, next) {
var colS = "";
switch(req.query.iSortCol_0){
case '0': colS = 'id'; break;
case '1': colS = 'username'; break;
case '2': colS = 'firstname'; break;
case '3': colS = 'acctype'; break;
case '4': colS = 'country'; break;
case '5': colS = 'createdAt'; break;
default: break;
};
var options = {
sort: colS+' '+req.query.sSortDir_0,
or: [
{ acctype: { 'contains': req.query.sSearch }},
{ id: { 'contains': req.query.sSearch }},
{ username: { 'contains': req.query.sSearch }},
{ lastname: { 'contains': req.query.sSearch }},
{ firstname: { 'contains': req.query.sSearch }},
{ country: { 'contains': req.query.sSearch }},
{ createdAt: { 'contains': req.query.sSearch }}
],
limit: req.query.iDisplayLength
};
console.log(req.query);
console.log(req.query.sSearch);
//console.log(options);
var p = req.query.iDisplayStart+1;
//console.log(req.query.iDisplayStart);
Users.find(options).paginate({ page: p, limit: req.query.iDisplayLength }).exec(function(err, usr){
if(err){
res.send(500, { error: 'DB error' });
} else {
var retuser = [];
usr.forEach(function(user){
retuser.push({ id: user.id, username: user.username, name: user.firstname+" "+user.lastname, acctype: user.acctype, country: user.country, createdAt: user.createdAt, button: "<a class='blue' href='#view-user' data-toggle='modal' id='user-info'> <i class='icon-zoom-in bigger-130'></i></a>" });
});
Users.count().exec(function(err, result){
var json = {
aaData: retuser,
iTotalRecords: result,
iTotalDisplayRecords: usr.length
};
res.contentType('application/json');
res.json(json);
});
}
});
},
基本上发生的是:
当我在
中更改页面时
.paginate({ page: p, limit: req.query.iDisplayLength })
改为2,显示第2页应该显示的内容,如果改成1,则显示第1页应该显示的内容。当更改要显示的记录数时,显示第11页记录。所以我想我从控制器中检索数据的方式没有问题。
我以为 DataTables 会根据 iTotalDisplayRecords
和 iTotalRecords
自动添加页码,但事实证明不是。是不是我的代码有问题,或者我认为是错误的,我必须是在我的 DataTable 中添加页码的人?
我认为问题在于您在 iTotalDisplayRecords
中返回了错误的值:
"iTotalRecords": 11,
"iTotalDisplayRecords": 10
来自manual:
int iTotalRecords
Total records, before filtering (i.e. the total
number of records in the database)
int iTotalDisplayRecords
Total records, after filtering (i.e. the total number of records after
filtering has been applied - not just the number of records being
returned in this result set)
不进行过滤时,iTotalRecords
应该等于iTotalDisplayRecords
。
要更正此问题,您需要在应用过滤后计算 记录总数 并将该值用于 iTotalDisplayRecords
而不是 usr.length
.
我目前正在使用DataTables的服务器端选项,我目前的问题是数据table中的页码只显示1。这里进一步解释是我的代码:
Javascript:
var oTable1 = $('#users-table').dataTable({
"bDestroy": true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/v1/users",
"bPaginate": true,
"aoColumns": [
{ "mData": "id" },
{ "mData": "username" },
{ "mData": "name" },
{ "mData": "acctype" },
{ "mData": "country" },
{ "mData": "createdAt" },
{ "mData": "button" }
]
});
/v1/users:
viewAll: function(req, res, next) {
var colS = "";
switch(req.query.iSortCol_0){
case '0': colS = 'id'; break;
case '1': colS = 'username'; break;
case '2': colS = 'firstname'; break;
case '3': colS = 'acctype'; break;
case '4': colS = 'country'; break;
case '5': colS = 'createdAt'; break;
default: break;
};
var options = {
sort: colS+' '+req.query.sSortDir_0,
or: [
{ acctype: { 'contains': req.query.sSearch }},
{ id: { 'contains': req.query.sSearch }},
{ username: { 'contains': req.query.sSearch }},
{ lastname: { 'contains': req.query.sSearch }},
{ firstname: { 'contains': req.query.sSearch }},
{ country: { 'contains': req.query.sSearch }},
{ createdAt: { 'contains': req.query.sSearch }}
],
limit: req.query.iDisplayLength
};
console.log(req.query);
console.log(req.query.sSearch);
//console.log(options);
var p = req.query.iDisplayStart+1;
//console.log(req.query.iDisplayStart);
Users.find(options).paginate({ page: p, limit: req.query.iDisplayLength }).exec(function(err, usr){
if(err){
res.send(500, { error: 'DB error' });
} else {
var retuser = [];
usr.forEach(function(user){
retuser.push({ id: user.id, username: user.username, name: user.firstname+" "+user.lastname, acctype: user.acctype, country: user.country, createdAt: user.createdAt, button: "<a class='blue' href='#view-user' data-toggle='modal' id='user-info'> <i class='icon-zoom-in bigger-130'></i></a>" });
});
Users.count().exec(function(err, result){
var json = {
aaData: retuser,
iTotalRecords: result,
iTotalDisplayRecords: usr.length
};
res.contentType('application/json');
res.json(json);
});
}
});
},
基本上发生的是: 当我在
中更改页面时.paginate({ page: p, limit: req.query.iDisplayLength })
改为2,显示第2页应该显示的内容,如果改成1,则显示第1页应该显示的内容。当更改要显示的记录数时,显示第11页记录。所以我想我从控制器中检索数据的方式没有问题。
我以为 DataTables 会根据 iTotalDisplayRecords
和 iTotalRecords
自动添加页码,但事实证明不是。是不是我的代码有问题,或者我认为是错误的,我必须是在我的 DataTable 中添加页码的人?
我认为问题在于您在 iTotalDisplayRecords
中返回了错误的值:
"iTotalRecords": 11,
"iTotalDisplayRecords": 10
来自manual:
int
iTotalRecords
Total records, before filtering (i.e. the total number of records in the database)int
iTotalDisplayRecords
Total records, after filtering (i.e. the total number of records after filtering has been applied - not just the number of records being returned in this result set)
不进行过滤时,iTotalRecords
应该等于iTotalDisplayRecords
。
要更正此问题,您需要在应用过滤后计算 记录总数 并将该值用于 iTotalDisplayRecords
而不是 usr.length
.