Browse Source

pagination ok

master
lefteris 4 years ago
parent
commit
17ce108a36
  1. 58
      swarmlab-app/src/run/app.js

58
swarmlab-app/src/run/app.js

@ -134,15 +134,8 @@ app.get("/test2", cors(corsOptions), (req, res) => {
var sorttmp1 = sort.split("|");
var sortname = sorttmp1[0];
var sortorder = sorttmp1[1];
console.log("TEST LOG");
console.error(
"Sort: " +
sort +
"\n Sort Name: " +
sortname +
"\n Sort Order: " +
sortorder
);
var url = "mongodb://mongo:27017/";
var jsonfinal = [];
MongoClient.connect(url, function (err, db) {
@ -187,8 +180,55 @@ app.get("/test2", cors(corsOptions), (req, res) => {
}
});
}
//Pagination
var total = jsonfinal.length;
var perpage = per_page;
var lastpage = total / perpage;
if (lastpage <= 1) {
lastpage = 1;
} else {
lastpage++;
}
lastpage = Math.trunc(lastpage);
var next = page + 1;
if (next >= lastpage) {
next = lastpage;
}
var prev = page - 1;
if (prev == 0) {
prev = 1;
}
var from = (page - 1) * perpage + 1;
var to = perpage * page;
var mypage = new Object();
var links = `
{
"pagination": {
"total": ${total},
"per_page": ${perpage},
"current_page": ${page},
"last_page": ${lastpage},
"next_page_url": "?page=${next}",
"prev_page_url": "?page=${prev}",
"from": ${from},
"to": ${to},
"frommongo": ${from},
"tomongo": ${to}
}
}
`;
mypage.links = JSON.parse(links);
from--;
mypage.data = jsonfinal.slice(from, to);
var RES = new Object();
RES.code = req.query["action"];
RES.token = req.query["token"];
RES.error = false;
RES.error_msg = "ok";
RES.data = mypage;
// console.log("Sending Data: " + jsonfinal);
res.json(jsonfinal);
res.json(RES.data);
db.close();
});
});

Loading…
Cancel
Save