Browse Source

Add pagination controls to listing page

pm2
Luke Childs 8 years ago
parent
commit
6e9eada1d1
  1. 19
      lib/nunjuck-filters.js
  2. 3
      viewModels/listing.js
  3. 10
      views/listing.html

19
lib/nunjuck-filters.js

@ -1,5 +1,6 @@
const prettyBytes = require('pretty-bytes');
const moment = require('moment');
const querystring = require('querystring');
const filters = {
bandwidth: node => `${prettyBytes(node.advertised_bandwidth)}/s`,
@ -30,6 +31,24 @@ const filters = {
readableUptime += !uptime.d && uptime.m ? ` ${uptime.m}m` : '';
return readableUptime.trim();
},
pagination: (req, direction) => {
// Clone query string
const query = Object.assign({}, req.query);
// Set page as 1 by default
query.p = query.p ? query.p : 1;
// Update page
if(direction == 'next') {
query.p++;
} else if(direction == 'prev') {
query.p--;
}
// Encode query string
return `/?${querystring.encode(query)}`;
}
};

3
viewModels/listing.js

@ -20,7 +20,8 @@ module.exports = (req, res) => {
tor.listNodes(query)
.then(nodes => res.render('listing.html', {
title: title,
nodes: nodes
nodes: nodes,
numOfNodes: query.limit
}))
.catch(error => res.render('listing.html', {
error: error

10
views/listing.html

@ -30,6 +30,16 @@
{% endfor %}
</tbody>
</table>
{% if req.query.s %}
<section class="pagination">
{% if req.query.p > 1 %}
<a href="{{ req | pagination('prev') }}">&lt; Prev</a>
{% endif %}
{% if nodes.length == numOfNodes %}
<a href="{{ req | pagination('next') }}">Next &gt;</a>
{% endif %}
</section>
{% endif %}
{% endif %}
{% endblock %}

Loading…
Cancel
Save