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() {
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;
if (home.current)
home.current.load();
@ -113,7 +128,7 @@ $.extend(HomePage.prototype,{
},
updatePager: function(links) {
$.each(['prev','next'], function (i, elt) {
var btn = $('#'+elt);
var btn = $('.'+elt);
if (links[elt]) {
btn.parent().removeClass('disabled');
} else {

View File

@ -5,19 +5,39 @@ var HashMap = require('hashmap').HashMap;
/* All authors */
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 authors = new Array();
req.paginate = new paginate(req);
req.db.each(query, req.paginate.perpage + 1, req.paginate.offset, 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);
});
var qparams = new Array();
var query = 'SELECT authors.id as id,name,count(*) as count'+
' FROM authors,books_authors_link'+
' WHERE authors.id = books_authors_link.author';
var initial = req.query.initial;
console.log(initial);
if (initial) {
query+=' AND ';
if (initial == '0') {
query+=' (substr(authors.sort,1,1) < ? OR substr(authors.sort,1,1) > ?)';
qparams.push('A');
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 */

View File

@ -5,38 +5,63 @@ var HashMap = require('hashmap').HashMap;
/* All books */
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 books = new HashMap();
req.paginate = new paginate(req);
req.db.each(query, req.paginate.perpage + 1, req.paginate.offset, function (err, book) {
if (books.count() < req.paginate.perpage) {
books.set(''+book.id, book);
} else
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());
}
});
});
});
var qparams = new Array();
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';
var initial = req.query.initial;
if (initial) {
query+=' WHERE';
if (initial == '0') {
query+=' substr(books.sort,1,1) < ? OR substr(books.sort,1,1) > ?';
qparams.push('A');
qparams.push('Z');
} else {
query+=' UPPER(books.sort) LIKE ?';
qparams.push(req.query.initial.toUpperCase()+'%');
}
}
query+= ' ORDER BY books.sort LIMIT ? OFFSET ?';
var books = new HashMap();
req.paginate = new paginate(req);
qparams.push(req.paginate.perpage + 1);
qparams.push(req.paginate.offset);
req.db.each(query, qparams,
function (err, book) {
if (books.count() < req.paginate.perpage) {
books.set(''+book.id, book);
} else
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 */

View File

@ -11,13 +11,23 @@ block content
&nbsp;
a#serie.btn.btn-primary.btn-lg(role="button") Series
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
li.previous.disabled: a#prev Précédent
each p in [10,20,50,100]
li: button.btn.btn-link.perpage(type="button",value=p)= p
li.next.disabled: a#next Suivant
li.previous.disabled: a.prev Précédent
li.next.disabled: a.next Suivant
table#items.table.table-striped
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/bootstrap.min.js")
script(src="js/purl.js")