Permalink
Browse files

pagination + infinite scroll + infinite scroll fix

  • Loading branch information...
1 parent 3a9c00e commit a7427ae4984c80fff05e193d21c1d0515b7b663a david committed Mar 3, 2012
@@ -8,6 +8,13 @@
box-content search-page group-name-{{ group.name }}
{% endblock %}
+{% block site_js %}
+ <script src="{{ url('jsi18n') }}"></script>
+ {{ js('common') }}
+ {{ js('search') }}
+ {% include "includes/webtrends_desktop.html" %}
+{% endblock %}
+
{% block main_content %}
{% trans name=group.name %}
<h1>Users in Group "{{ name }}"</h1>
@@ -31,8 +38,25 @@
<div class="clear"></div>
<div class="blue-pastels">
- {% for profile in profiles %}
- {{ search_result(profile) }}
+ <form method="GET" id="search-form" action="#">
+ <input type="hidden" name="limit" id="limit" value="{{ limit }}">
+ </form>
+ {% for person in people %}
+ {{ search_result(person) }}
{% endfor %}
+ {% if show_pagination %}
+ <div data-pages={{ num_pages }} class="pagination">
+ {% for page in people.paginator.page_range %}
+ {% if page == people.number %}
+ <span>{{ page }}</span>
+ {% else %}
+ <a href="#">{{ page }}</a>
+ {% endif %}
+ {% endfor %}
+ </div>
+ <div id="final-result">
+ <span>Showing all {{ people.paginator.count }} results.</span>
+ </div>
+ {% endif %}
</div>
{% endblock main_content %}
View
@@ -11,7 +11,7 @@
from funfactory.urlresolvers import reverse
from .models import Group
-from phonebook.forms import PAGINATION_LIMIT
+from phonebook import forms
from phonebook.views import vouch_required
log = commonware.log.getLogger('m.groups')
@@ -20,7 +20,7 @@
@login_required
def index(request):
"""Lists all public groups (in use) on Mozillians."""
- paginator = Paginator(Group.objects.all(), PAGINATION_LIMIT)
+ paginator = Paginator(Group.objects.all(), forms.PAGINATION_LIMIT)
page = request.GET.get('page')
try:
@@ -56,19 +56,52 @@ def search(request):
@vouch_required
def show(request, id, url=None):
- """List all users with this group."""
+ """ List all users with this group."""
+ if not url:
+ redirect(reverse('group', args=[group.id, group.url]))
+
+
+ form = forms.SearchForm(request.GET)
group = get_object_or_404(Group, id=id)
- # Redirect to the full URL if it wasn't supplied
- if not url:
- redirect(reverse('group', args=[group.id, group.url]))
+ limit = forms.PAGINATION_LIMIT
+ if form.is_valid():
+ limit = form.cleaned_data['limit']
+
+ page = request.GET.get('page', 1)
in_group = (request.user.get_profile()
.groups.filter(id=group.id).count())
profiles = group.userprofile_set.all()
- data = dict(group=group, in_group=in_group, profiles=profiles)
- return render(request, 'groups/show.html', data)
+ paginator = Paginator(profiles, limit)
+
+ people = []
+ try:
+ people = paginator.page(page)
+ except PageNotAnInteger:
+ people = paginator.page(1)
+ except EmptyPage:
+ people = paginator.page(paginator.num_pages)
+
+ show_pagination = False
+ num_pages = 0
+ if paginator.count > forms.PAGINATION_LIMIT:
+ show_pagination = True
+ num_pages = len(people.paginator.page_range)
+
+ d = dict(people=people,
+ group=group,
+ in_group=in_group,
+ form=form,
+ limit=limit,
+ show_pagination=show_pagination,
+ num_pages=num_pages)
+
+ if request.is_ajax():
+ return render(request, 'search_ajax.html', d)
+
+ return render(request, 'groups/show.html', d)
@require_POST
View
@@ -149,7 +149,7 @@ def search(request):
num_pages=num_pages)
if request.is_ajax():
- return render(request, 'phonebook/search_ajax.html', d)
+ return render(request, 'search_ajax.html', d)
return render(request, 'phonebook/search.html', d)
View
@@ -22,18 +22,20 @@
return cease;
},
callback: function(i) {
- cease = (pages <= i);
- $.ajax({
- data:{'page': i + 1},
- dataType: 'html',
- success: function(data) {
- paginator.before($(data));
- }
- });
+ cease = (pages < i);
if (cease) {
// Show the user that we have stopped scrolling on purpose.
results.show()
+ } else {
+ $.ajax({
+ data:{'page': i + 1},
+ dataType: 'html',
+ success: function(data) {
+ paginator.before($(data));
+ }
+ });
}
+
}
});
});

0 comments on commit a7427ae

Please sign in to comment.