backoffice: allow user to limit number of rows per page (#14443)

This commit is contained in:
Frédéric Péters 2017-01-07 16:42:21 +01:00
parent c46bc299e5
commit 88f04a3d01
6 changed files with 49 additions and 19 deletions

View File

@ -355,20 +355,26 @@ def test_backoffice_listing_pagination(pub):
assert resp.body.count('data-link') == 5
assert '<div id="page-links">' in resp.body
resp = resp.click('2', index=-1) # second page
resp = resp.click(re.compile('^2$')) # second page
assert resp.body.count('data-link') == 5
assert resp.form['offset'].value == '5'
resp = resp.click('3', index=-1) # third page
resp = resp.click(re.compile('^3$')) # third page
assert resp.body.count('data-link') == 5
assert resp.form['offset'].value == '10'
resp = resp.click('4', index=-1) # fourth page
resp = resp.click(re.compile('^4$')) # fourth page
assert resp.body.count('data-link') == 2
assert resp.form['offset'].value == '15'
with pytest.raises(IndexError): # no fifth page
resp = resp.click('5')
resp = resp.click(re.compile('^5$'))
resp = resp.click(re.compile('^10$')) # per page: 10
assert resp.body.count('data-link') == 10
resp = resp.click(re.compile('^20$')) # per page: 20
assert resp.body.count('data-link') == 17
# try an overbound offset
resp = app.get('/backoffice/management/form-title/?limit=5&offset=30')

View File

@ -360,7 +360,7 @@ class UsersViewDirectory(Directory):
r += htmltext('</tbody>')
r += htmltext('</table>')
if (offset > 0) or (total_count > limit > 0):
if get_publisher().is_using_postgresql():
r += pagination_links(offset, limit, total_count)
r += htmltext('</div>')

View File

@ -110,7 +110,7 @@ class FormDefUI(object):
r += htmltext('</tbody>')
r += htmltext('</table>')
if partial_display:
if get_publisher().is_using_postgresql():
# add links to paginate
r += pagination_links(offset, limit, total_count)

View File

@ -20,8 +20,6 @@ from quixote.html import htmltext, TemplateIO
from quixote import get_request, get_response
def pagination_links(offset, limit, total_count):
if total_count <= limit:
return ''
get_response().add_javascript(['jquery.js', 'wcs.listing.js'])
# pagination
r = TemplateIO(html=True)
@ -35,8 +33,8 @@ def pagination_links(offset, limit, total_count):
query['limit'] = limit
if 'ajax' in query:
del query['ajax']
r += htmltext('<a class="previous-page" data-offset="%s" href="?%s"><!--%s--></a>') % (
query['offset'], urllib.urlencode(query, doseq=1), _('Previous Page'))
r += htmltext('<a class="previous-page" data-limit="%s" data-offset="%s" href="?%s"><!--%s--></a>') % (
limit, query['offset'], urllib.urlencode(query, doseq=1), _('Previous Page'))
else:
r += htmltext('<span class="previous-page"><!--%s--></span>') % _('Previous Page')
@ -66,8 +64,9 @@ def pagination_links(offset, limit, total_count):
klass = 'current'
else:
klass = ''
r += htmltext(' <a class="%s" data-offset="%s" href="?%s">%s</a> ') % (
r += htmltext(' <a class="%s" data-limit="%s" data-offset="%s" href="?%s">%s</a> ') % (
klass,
limit,
query['offset'],
urllib.urlencode(query, doseq=1),
page_number)
@ -79,11 +78,27 @@ def pagination_links(offset, limit, total_count):
query['limit'] = limit
if 'ajax' in query:
del query['ajax']
r += htmltext('<a class="next-page" data-offset="%s" href="?%s"><!--%s--></a>') % (
query['offset'], urllib.urlencode(query, doseq=1), _('Next Page'))
r += htmltext('<a class="next-page" data-limit="%s" data-offset="%s" href="?%s"><!--%s--></a>') % (
limit, query['offset'], urllib.urlencode(query, doseq=1), _('Next Page'))
else:
r += htmltext('<span class="next-page"><!--%s--></span>') % _('Next Page')
r += htmltext(' <span class="displayed-range">(%s-%s/%s)</span> ') % (
offset + 1,
min((offset + limit, total_count)),
total_count)
r += htmltext(' <span class="page-limit"><span class="per-page-label">%s</span>') % _('Per page: ')
for page_size in (10, 20, 50, 100):
query['limit'] = page_size
query['offset'] = '0'
if page_size == limit:
r += htmltext('<span>%s</span>') % page_size
else:
r += htmltext('<a data-limit="%s" data-offset="0"" href="?%s">%s</a>') % (
page_size, urllib.urlencode(query, doseq=1), page_size)
if page_size > total_count:
break
r += htmltext('</div>')
return r.getvalue()

View File

@ -824,12 +824,6 @@ div.bo-block div#page-links {
content: ">";
}
div#page-links span.pages {
padding-left: 1ex;
margin-left: 1ex;
}
div#page-links a {
padding: 1ex 1.5ex;
border: none;
@ -842,6 +836,20 @@ div#page-links span.pages a.current {
border-radius: 2px;
}
div#page-links span.displayed-range {
color: #666;
margin: 0 1em;
}
div#page-links span.page-limit span.per-page-label {
color: #666;
}
div#page-links span.page-limit > span,
div#page-links span.page-limit > a {
padding: 1ex 1.5ex;
}
ul#fields-filter {
overflow: auto;
max-height: 350px;

View File

@ -1,6 +1,7 @@
function prepare_page_links() {
$('#page-links a').click(function() {
$('form#listing-settings input[name="offset"]').val($(this).data('offset'));
$('form#listing-settings input[name="limit"]').val($(this).data('limit'));
refresh_table();
return false;
});