Added pagination by date to blocklist page

This commit is contained in:
Mario Colque 2014-01-09 18:36:20 -03:00
parent aab6121dd0
commit 279a357f0e
3 changed files with 47 additions and 15 deletions

View File

@ -48,28 +48,50 @@ exports.last_blocks = function(req, res) {
* List of blocks by date * List of blocks by date
*/ */
exports.list = function(req, res) { 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) { if (req.query.blockDate) {
var gte = Math.round((new Date(req.query.blockDate)).getTime() / 1000); dateStr = req.query.blockDate;
var lte = gte + 86400; } else {
dateStr = formatTimestamp(new Date());
findParam = { time: {
'$gte': gte,
'$lte': lte
}};
} }
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 Block
.find(findParam) .find({
.limit(5) time: {
'$gte': gte,
'$lte': lte
}
})
.exec(function(err, blocks) { .exec(function(err, blocks) {
if (err) { if (err) {
res.render('error', { res.render('error', {
status: 500 status: 500
}); });
} else { } else {
res.jsonp(blocks); res.jsonp({
blocks: blocks,
pagination: {
next: next,
prev: prev,
current: dateStr
}
});
} }
}); });
}; };

View File

@ -4,10 +4,11 @@ angular.module('mystery.blocks').controller('BlocksController', ['$scope', '$rou
$scope.global = Global; $scope.global = Global;
$scope.list = function() { $scope.list = function() {
Blocks.query({ Blocks.get({
blockDate: $routeParams.blockDate blockDate: $routeParams.blockDate
}, function(blocks) { }, function(res) {
$scope.blocks = blocks; $scope.blocks = res.blocks;
$scope.pagination = res.pagination;
}); });
}; };

View File

@ -1,6 +1,15 @@
<section data-ng-controller="BlocksController" data-ng-init="list()"> <section data-ng-controller="BlocksController" data-ng-init="list()">
<div class="page-header"> <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}}">&laquo; {{pagination.prev}}</a></li>
<li class="disabled"><a href="#">{{pagination.current}}</a></li>
<li><a href="#!/blocks-date/{{pagination.next}}">{{pagination.next}} &raquo;</a></li>
</ul>
</div> </div>
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>