Added search and orderby features to people

This commit is contained in:
Tom Price
2014-11-04 04:35:03 +00:00
parent 0cfa36b602
commit a097a8c08c
4 changed files with 56 additions and 8 deletions

View File

@@ -1,8 +1,10 @@
{% extends request.is_ajax|yesno:"base_ajax.html,base.html" %} {% extends request.is_ajax|yesno:"base_ajax.html,base.html" %}
{% load widget_tweaks %} {% load widget_tweaks %}
{% load paginator from filters %} {% load paginator from filters %}
{% load url_replace from filters %}
{% load orderby from filters %}
{% block title %}People list{% endblock %} {% block title %}People{% endblock %}
{% block content %} {% block content %}
<div class="col-sm-10 col-sm-offset-1"> <div class="col-sm-10 col-sm-offset-1">
@@ -13,6 +15,13 @@
<div class="col-sm-2"> <div class="col-sm-2">
<a href="{% url 'person_add' %}" class="btn btn-default pull-right">New <span class="glyphicon glyphicon-plus"></span></a> <a href="{% url 'person_add' %}" class="btn btn-default pull-right">New <span class="glyphicon glyphicon-plus"></span></a>
</div> </div>
<div class="col-sm-3 col-sm-offset-9">
<form class="form form-horizontal col-sm-12">
<div class="form-group">
<input type="search" name="q" placeholder="Search" value="{{ request.GET.q }}" class="form-control" />
</div>
</form>
</div>
</div> </div>
<div class="pull-right">{% paginator %}</div> <div class="pull-right">{% paginator %}</div>
<div> <div>
@@ -20,7 +29,7 @@
<thead> <thead>
<tr> <tr>
<td></td> <td></td>
<td>Name</td> <td>Name<a href="?{% orderby request 'orderBy' 'name' %}"><span class="caret"></span></a></td>
<td>Email</td> <td>Email</td>
<td>Phone</td> <td>Phone</td>
<td>Notes</td> <td>Notes</td>

View File

@@ -40,6 +40,7 @@ def paginator(context, adjacent_pages=3):
if n > 0 and n <= paginator.num_pages] if n > 0 and n <= paginator.num_pages]
dict = { dict = {
'request': context['request'],
'is_paginated': paginator.num_pages > 0, 'is_paginated': paginator.num_pages > 0,
'page_obj': page, 'page_obj': page,
'paginator': paginator, 'paginator': paginator,
@@ -59,4 +60,28 @@ def paginator(context, adjacent_pages=3):
dict['previous'] = page.previous_page_number() dict['previous'] = page.previous_page_number()
return dict return dict
register.inclusion_tag('pagination.html', takes_context=True)(paginator) register.inclusion_tag('pagination.html', takes_context=True)(paginator)
@register.simple_tag
def url_replace(request, field, value):
dict_ = request.GET.copy()
dict_[field] = value
return dict_.urlencode()
@register.simple_tag
def orderby(request, field, attr):
dict_ = request.GET.copy()
if dict_[field] == attr:
if not dict_[field].startswith("-"):
dict_[field] = "-" + attr
else:
dict_[field] = attr
else:
dict_[field] = attr
return dict_.urlencode()

View File

@@ -1,6 +1,8 @@
from django.http.response import HttpResponseRedirect from django.http.response import HttpResponseRedirect
from django.core.urlresolvers import reverse_lazy from django.core.urlresolvers import reverse_lazy
from django.views import generic from django.views import generic
from django.views.decorators.csrf import csrf_exempt
from django.db.models import Q
from RIGS import models from RIGS import models
# Create your views here. # Create your views here.
@@ -28,6 +30,17 @@ class PersonIndex(generic.ListView):
model = models.Person model = models.Person
paginate_by = 20 paginate_by = 20
def get_queryset(self):
q = self.request.GET.get('q', "")
if len(q) >= 3:
object_list = self.model.objects.filter(Q(name__icontains=q) | Q(email__icontains=q))
else:
object_list = self.model.objects.all()
orderBy = self.request.GET.get('orderBy', None)
if orderBy is not None:
object_list = object_list.order_by(orderBy)
return object_list
class PersonDetail(generic.DetailView): class PersonDetail(generic.DetailView):
model = models.Person model = models.Person

View File

@@ -1,14 +1,15 @@
{% if is_paginated %} {% if is_paginated %}
{% load url_replace from filters %}
<div> <div>
<ul class="pagination"> <ul class="pagination">
{% if has_previous %} {% if has_previous %}
<li><a href="?page={{ previous }}{{ getvars }}{{ hashtag }}" class="prev">&lsaquo;&lsaquo;</a></li> <li><a href="?{% url_replace request 'page' previous %}" class="prev">&lsaquo;&lsaquo;</a></li>
{% else %} {% else %}
<li class="disabled prev"><a href="#">&lsaquo;&lsaquo;</a></li> <li class="disabled prev"><a href="#">&lsaquo;&lsaquo;</a></li>
{% endif %} {% endif %}
{% if show_first %} {% if show_first %}
<li><a href="?page={{ first }}{{ getvars }}{{ hashtag }}" class="prev">{{ first }}</a></li> <li><a href="?{% url_replace request 'page' first %}" class="prev">{{ first }}</a></li>
<li><span>&hellip;</span></li> <li><span>&hellip;</span></li>
{% endif %} {% endif %}
@@ -16,17 +17,17 @@
{% ifequal page page_obj.number %} {% ifequal page page_obj.number %}
<li class="current page active"><a href="#">{{ page }}</a></li> <li class="current page active"><a href="#">{{ page }}</a></li>
{% else %} {% else %}
<li><a href="?page={{ page }}{{ getvars }}{{ hashtag }}" class="page">{{ page }}</a></li> <li><a href="?{% url_replace request 'page' page %}" class="page">{{ page }}</a></li>
{% endifequal %} {% endifequal %}
{% endfor %} {% endfor %}
{% if show_last %} {% if show_last %}
<li><span>&hellip;</span></li> <li><span>&hellip;</span></li>
<li><a href="?page={{ last }}{{ getvars }}{{ hashtag }}" class="prev">{{ last }}</a></li> <li><a href="?{% url_replace request 'page' last %}" class="prev">{{ last }}</a></li>
{% endif %} {% endif %}
{% if has_next %} {% if has_next %}
<li><a href="?page={{ next}}{{ getvars }}{{ hashtag }}" class="next">&rsaquo;&rsaquo;</a></li> <li><a href="?{% url_replace request 'page' next %}" class="next">&rsaquo;&rsaquo;</a></li>
{% else %} {% else %}
<li class="disabled next"><a href="#">&rsaquo;&rsaquo;</a></li> <li class="disabled next"><a href="#">&rsaquo;&rsaquo;</a></li>
{% endif %} {% endif %}