enhanced pagination (initial)

This commit is contained in:
Meutel 2014-06-27 18:40:59 +02:00
parent f286457117
commit 37d25a0bc1
4 changed files with 120 additions and 50 deletions

View File

@ -81,6 +81,21 @@ var HomePage = function() {
}); });
$(".perpage").click(function() { $(".perpage").click(function() {
home.pagination.perpage = $(this).attr("value"); home.pagination.perpage = $(this).attr("value");
$('.perpage').removeClass('active');
$(this).addClass('active');
home.pagination.page = 0;
if (home.current)
home.current.load();
});
$(".initial").click(function() {
if ($(this).hasClass('active')) {
home.pagination.initial = null;
$('.initial').removeClass('active');
} else {
home.pagination.initial = $(this).attr("value");
$('.initial').removeClass('active');
$(this).addClass('active');
}
home.pagination.page = 0; home.pagination.page = 0;
if (home.current) if (home.current)
home.current.load(); home.current.load();
@ -113,7 +128,7 @@ $.extend(HomePage.prototype,{
}, },
updatePager: function(links) { updatePager: function(links) {
$.each(['prev','next'], function (i, elt) { $.each(['prev','next'], function (i, elt) {
var btn = $('#'+elt); var btn = $('.'+elt);
if (links[elt]) { if (links[elt]) {
btn.parent().removeClass('disabled'); btn.parent().removeClass('disabled');
} else { } else {

View File

@ -5,19 +5,39 @@ var HashMap = require('hashmap').HashMap;
/* All authors */ /* All authors */
router.get('/', function(req, res) { router.get('/', function(req, res) {
var query = 'SELECT authors.id as id,name,count(*) as count FROM authors,books_authors_link WHERE authors.id = books_authors_link.author GROUP BY books_authors_link.author ORDER BY sort LIMIT ? OFFSET ?'; var qparams = new Array();
var authors = new Array(); var query = 'SELECT authors.id as id,name,count(*) as count'+
req.paginate = new paginate(req); ' FROM authors,books_authors_link'+
req.db.each(query, req.paginate.perpage + 1, req.paginate.offset, function (err, row) { ' WHERE authors.id = books_authors_link.author';
if (authors.length < req.paginate.perpage) var initial = req.query.initial;
authors.push(row); console.log(initial);
else if (initial) {
req.paginate.hasNext = true; query+=' AND ';
}, function(err) { if (initial == '0') {
if (err) console.log(err); query+=' (substr(authors.sort,1,1) < ? OR substr(authors.sort,1,1) > ?)';
res.links(req.paginate.links()); qparams.push('A');
res.json(authors); qparams.push('Z');
}); } else {
query+=' UPPER(authors.sort) LIKE ?';
qparams.push(req.query.initial.toUpperCase()+'%');
}
}
query+=' GROUP BY books_authors_link.author'+
' ORDER BY sort LIMIT ? OFFSET ?';
req.paginate = new paginate(req);
qparams.push(req.paginate.perpage + 1);
qparams.push(req.paginate.offset);
var authors = new Array();
req.db.each(query, qparams, function (err, row) {
if (authors.length < req.paginate.perpage)
authors.push(row);
else
req.paginate.hasNext = true;
}, function(err) {
if (err) console.log(err);
res.links(req.paginate.links());
res.json(authors);
});
}); });
/* Single author */ /* Single author */

View File

@ -5,38 +5,63 @@ var HashMap = require('hashmap').HashMap;
/* All books */ /* All books */
router.get('/', function(req, res) { router.get('/', function(req, res) {
var query = 'SELECT books.id as id,title,series_index,name as series_name,series.id AS series_id FROM books LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book LEFT OUTER JOIN series ON series.id = books_series_link.series ORDER BY books.sort LIMIT ? OFFSET ?'; var qparams = new Array();
var books = new HashMap(); var query = 'SELECT books.id as id,title,series_index,name as series_name,series.id AS series_id'+
req.paginate = new paginate(req); ' FROM books' +
req.db.each(query, req.paginate.perpage + 1, req.paginate.offset, function (err, book) { ' LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book' +
if (books.count() < req.paginate.perpage) { ' LEFT OUTER JOIN series ON series.id = books_series_link.series';
books.set(''+book.id, book); var initial = req.query.initial;
} else if (initial) {
req.paginate.hasNext = true; /* more than perpage */ query+=' WHERE';
}, function(err) { if (initial == '0') {
if (err) console.log(err); query+=' substr(books.sort,1,1) < ? OR substr(books.sort,1,1) > ?';
res.links(req.paginate.links()); qparams.push('A');
/* query books authors */ qparams.push('Z');
var qAuthors = 'SELECT books_authors_link.book as book,authors.id as id,name FROM authors, books_authors_link WHERE books_authors_link.author = authors.id AND books_authors_link.book IN ('; } else {
for (var i=0;i<books.count()-1;i++) qAuthors+='?,'; query+=' UPPER(books.sort) LIKE ?';
qAuthors+='?)'; qparams.push(req.query.initial.toUpperCase()+'%');
req.db.each(qAuthors, books.keys(), function(err, author) { }
var book = books.get(''+author.book); }
if (!book.authors) book.authors = new Array(); query+= ' ORDER BY books.sort LIMIT ? OFFSET ?';
book.authors.push(author); var books = new HashMap();
}, function(err) { req.paginate = new paginate(req);
if (err) console.log(err); qparams.push(req.paginate.perpage + 1);
res.format({ qparams.push(req.paginate.offset);
html: function(){ req.db.each(query, qparams,
//TODO load items in home page function (err, book) {
res.render('home', books.values()); if (books.count() < req.paginate.perpage) {
}, books.set(''+book.id, book);
json: function(){ } else
res.json(books.values()); req.paginate.hasNext = true; /* more than perpage */
} },
}); function(err) {
}); if (err) console.log(err);
}); res.links(req.paginate.links());
/* query books authors */
var qAuthors = 'SELECT books_authors_link.book as book,authors.id as id,name FROM authors, books_authors_link WHERE books_authors_link.author = authors.id AND books_authors_link.book IN (';
for (var i=0;i<books.count()-1;i++) qAuthors+='?,';
qAuthors+='?)';
req.db.each(qAuthors, books.keys(),
function(err, author) {
var book = books.get(''+author.book);
if (!book.authors) book.authors = new Array();
book.authors.push(author);
},
function(err) {
if (err) console.log(err);
res.format({
html: function(){
//TODO load items in home page
res.render('home', books.values());
},
json: function(){
res.json(books.values());
}
});
}
);
}
);
}); });
/* Single book */ /* Single book */

View File

@ -11,13 +11,23 @@ block content
&nbsp; &nbsp;
a#serie.btn.btn-primary.btn-lg(role="button") Series a#serie.btn.btn-primary.btn-lg(role="button") Series
div.container-fluid div.container-fluid
div.btn-group
- var initials = '0ABCDEFGHIPQRSTUVWXYZ';
- for (var i=0;i<initials.length;i++) {
- var c = initials.charAt(i);
button.btn.btn-default.initial(type="button",value=c)= c
- }
ul.pager ul.pager
li.previous.disabled: a#prev Précédent li.previous.disabled: a.prev Précédent
each p in [10,20,50,100] li.next.disabled: a.next Suivant
li: button.btn.btn-link.perpage(type="button",value=p)= p
li.next.disabled: a#next Suivant
table#items.table.table-striped table#items.table.table-striped
tr: td.disabled Vide tr: td.disabled Vide
ul.pager
li.previous.disabled: a.prev Précédent
li.next.disabled: a.next Suivant
div.btn-group.center-block
each p in [10,20,50,100]
button.btn.btn-default.perpage(type="button",value=p)= p
script(src="js/jquery.min.js") script(src="js/jquery.min.js")
script(src="js/bootstrap.min.js") script(src="js/bootstrap.min.js")
script(src="js/purl.js") script(src="js/purl.js")