Задать вопрос

Тел: +7 965 3737 888

558

Просмотров

10

Ответов

Sort Table Headers

<p>Handles creation of order_by criteria based on GET parameters and provides context variables to be used when generating table header sort links which respect the current sort field and direction, reversing the direction when the same header is sorted by again.</p>
<p>Sample view:</p>
from somewhere import SortHeaders
from django.contrib.auth.models import User
from django.shortcuts import render_to_response

LIST_HEADERS = (
    ('Username', 'username'),
    ('First Name', 'first_name'),
    ('Last Name', 'last_name'),
    ('Email', None),
)

def user_list(request):
    sort_headers = SortHeaders(request, LIST_HEADERS)
    users = User.objects.order_by(sort_headers.get_order_by())
    return render_to_response('users/user_list.html', {
        'users': users,
        'headers': list(sort_headers.headers()),
    })

<p>Sample template:</p>
{% load my_tags %}
&lt;table cellspacing="0"&gt;
&lt;thead&gt;
  &lt;tr&gt;
{% table_header headers %}
  &lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
  {% for user in users %}&lt;tr class="{% cycle odd,even %}"&gt;
    &lt;td&gt;&lt;a href="{{ user.get_absolute_url|escape }}"&gt;{{ user.username|escape }}&lt;/a&gt;&lt;/td&gt;
    &lt;td&gt;{{ user.first_name|escape }}&lt;/td&gt;
    &lt;td&gt;{{ user.last_name|escape }}&lt;/td&gt;
    &lt;td&gt;{{ user.email|escape }}&lt;/td&gt;
  &lt;/tr&gt;
  {% endfor %}
&lt;/tbody&gt;
&lt;/table&gt;

<p>Sample inclusion tag:</p>
from django import template

def table_header(context, headers):
    return {
        'headers': headers,
    }

register = template.Library()
register.inclusion_tag('table_header.html', takes_context=True)(table_header)

<p>Sample inclusion tag template:</p>
{% for header in headers %}&lt;th{{ header.class_attr }}&gt;
  {% if header.sortable %}&lt;a href="{{ header.url|escape }}"&gt;{% endif %}
  {{ header.text }}
  {% if header.sortable %}&lt;/a&gt;{% endif %}
&lt;/th&gt;{% endfor %}

Вопрос полезен? Да0/Нет0
file_4006.py(4.5Кб)
None

Ответы (10):

Ответverycool:03.05.2011
Ответ полезен? Да0/Нет0

if ORDER_TYPE_VAR in params and params[ORDER_TYPE_VAR] in ('asc', 'desc'): self.order_type = params[ORDER_TYPE_VAR]

Ответverycool:03.05.2011
Ответ полезен? Да0/Нет0

I had a search form in the list page with the method get. I had to add some extra lines to include the request get query string in the sorted link path. enjoy

[HTML_REMOVED]

Ответdarrinmc:26.03.2011
Ответ полезен? Да0/Нет0

I figured out my problem. It was unrelated to this code.

Ответdarrinmc:21.03.2011
Ответ полезен? Да0/Нет0

I'm using this code with django 1.2.5 and I get the following error in a simple list view:

Exception Value: 'generator' object has no attribute 'GET'
Exception Location: /Users/darrinmc/Projects/django/djangotest/myapp/sort_headers.py in __init__, line 60

Line 60 of sort_headers.py is this line:

params = dict(request.GET.items())

In the debug output, the request object is a generator object when this method is called. Has anyone else seen this? Thanks.

Ответblcarmadillo:17.06.2010
Ответ полезен? Да0/Нет0

Is there anyway to independently sort multiple tables on a single page?

Ответpowerfox:28.01.2009
Ответ полезен? Да0/Нет0

Nice work! Thanks a lot.

Ответmirobe:07.02.2008
Ответ полезен? Да0/Нет0

I'm affraid there would be a problem when using pagination.

Do you have pagination enabled version of sorted headers list ?

Thanks

Ответsebastianmacias:04.09.2007
Ответ полезен? Да0/Нет0

Would it be possible to use ordering criteria that span relationships?

Lets say I have article model and each article belogs to a user.

I would like to order the artcles by user email

but I get an error when I add ('Email Address', 'user__email') to LIST_HEADERS.

Best,

Sebastian Macias

Ответinsin:13.07.2007
Ответ полезен? Да0/Нет0

Whoops, thanks for pointing that out - fixed it now.

Ответskam:13.07.2007
Ответ полезен? Да0/Нет0

line 66: except (IndexError, ValueError) instead of IndexError, ValueError

great work!