Merge pull request #12 from colkito/feature/blocklist-page-pagination
Added pagination by date to blocklist page
This commit is contained in:
commit
38e93bb341
|
@ -48,28 +48,50 @@ exports.last_blocks = function(req, res) {
|
|||
* List of blocks by date
|
||||
*/
|
||||
exports.list = function(req, res) {
|
||||
var findParam = {};
|
||||
//helper to convert timestamps to yyyy-mm-dd format
|
||||
var formatTimestamp = function (date) {
|
||||
var yyyy = date.getUTCFullYear().toString();
|
||||
var mm = (date.getUTCMonth() + 1).toString(); // getMonth() is zero-based
|
||||
var dd = date.getUTCDate().toString();
|
||||
|
||||
return yyyy + '-' + (mm[1] ? mm : '0' + mm[0]) + '-' + (dd[1] ? dd : '0' + dd[0]); //padding
|
||||
};
|
||||
|
||||
var dateStr;
|
||||
if (req.query.blockDate) {
|
||||
var gte = Math.round((new Date(req.query.blockDate)).getTime() / 1000);
|
||||
var lte = gte + 86400;
|
||||
|
||||
findParam = { time: {
|
||||
'$gte': gte,
|
||||
'$lte': lte
|
||||
}};
|
||||
dateStr = req.query.blockDate;
|
||||
} else {
|
||||
dateStr = formatTimestamp(new Date());
|
||||
}
|
||||
|
||||
var gte = Math.round((new Date(dateStr)).getTime() / 1000);
|
||||
|
||||
//pagination
|
||||
var lte = gte + 86400;
|
||||
var prev = formatTimestamp(new Date((gte - 86400) * 1000));
|
||||
var next = formatTimestamp(new Date(lte * 1000));
|
||||
|
||||
Block
|
||||
.find(findParam)
|
||||
.limit(5)
|
||||
.find({
|
||||
time: {
|
||||
'$gte': gte,
|
||||
'$lte': lte
|
||||
}
|
||||
})
|
||||
.exec(function(err, blocks) {
|
||||
if (err) {
|
||||
res.render('error', {
|
||||
status: 500
|
||||
});
|
||||
} else {
|
||||
res.jsonp(blocks);
|
||||
res.jsonp({
|
||||
blocks: blocks,
|
||||
pagination: {
|
||||
next: next,
|
||||
prev: prev,
|
||||
current: dateStr
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
|
@ -4,10 +4,11 @@ angular.module('mystery.blocks').controller('BlocksController', ['$scope', '$rou
|
|||
$scope.global = Global;
|
||||
|
||||
$scope.list = function() {
|
||||
Blocks.query({
|
||||
Blocks.get({
|
||||
blockDate: $routeParams.blockDate
|
||||
}, function(blocks) {
|
||||
$scope.blocks = blocks;
|
||||
}, function(res) {
|
||||
$scope.blocks = res.blocks;
|
||||
$scope.pagination = res.pagination;
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,15 @@
|
|||
<section data-ng-controller="BlocksController" data-ng-init="list()">
|
||||
<div class="page-header">
|
||||
<h1>Blocks by Date</h1>
|
||||
<h1>
|
||||
<span class="glyphicon glyphicon-calendar"></span>
|
||||
Blocks
|
||||
<small>by date</small>
|
||||
</h1>
|
||||
<ul class="pagination">
|
||||
<li><a href="#!/blocks-date/{{pagination.prev}}">« {{pagination.prev}}</a></li>
|
||||
<li class="disabled"><a href="#">{{pagination.current}}</a></li>
|
||||
<li><a href="#!/blocks-date/{{pagination.next}}">{{pagination.next}} »</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
|
|
Loading…
Reference in New Issue