Skip to content

Instantly share code, notes, and snippets.

@mesuutt
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mesuutt/9271770 to your computer and use it in GitHub Desktop.
Save mesuutt/9271770 to your computer and use it in GitHub Desktop.
Bootstrap style table. Fixed several bugs related pagination and column sorting.
{% spaceless %}
{% load django_tables2 %}
{% load i18n %}
<div class="grid">
<div class="grid-title">
<div class="pull-left">
<div class="icon-title"><i class="icon-eye-open"></i></div>
<span>{{ table.Meta.title }}</span>
<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</div>
<div class="grid-content overflow">
<div class="table-container">
{% block table %}
<table{% if table.attrs %} {{ table.attrs.as_html }} {% endif %}>
{% nospaceless %}
{% block table.thead %}
<thead>
<tr>
{% for column in table.columns %}
{% if column.orderable %}
{% with "-"|add:column.name as sort_col_name %}
<th {{ column.attrs.th.as_html }}>
<a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.opposite|default:sort_col_name %}">{{ column.header }} </a>
</th>
{% endwith %}
{% else %}
<th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
{% endif %}
{% endfor %}
</tr>
</thead>
{% endblock table.thead %}
{% block table.tbody %}
<tbody>
{% for row in table.page.object_list|default:table.rows %} {# support pagination #}
{% block table.tbody.row %}
<tr class="{{ forloop.counter|divisibleby:2|yesno:"even,odd" }}"> {# avoid cycle for Django 1.2-1.6 compatibility #}
{% for column, cell in row.items %}
<td {{ column.attrs.td.as_html }}>{% if column.localize == None %}{{ cell }}{% else %}{% if column.localize %}{{ cell|localize }}{% else %}{{ cell|unlocalize }}{% endif %}{% endif %}</td>
{% endfor %}
</tr>
{% endblock table.tbody.row %}
{% empty %}
{% if table.empty_text %}
{% block table.tbody.empty_text %}
<tr><td colspan="{{ table.columns|length }}">{{ table.empty_text }}</td></tr>
{% endblock table.tbody.empty_text %}
{% endif %}
{% endfor %}
</tbody>
{% endblock table.tbody %}
{% block table.tfoot %}
<tfoot></tfoot>
{% endblock table.tfoot %}
{% endnospaceless %}
</table>
{% endblock table %}
{% if table.page.paginator.count %}
{% with table.page.paginator.count as total %}
{% block pagination %}
<ul class="pagination">
<li>
<span>Total page: {{ table.page.paginator.num_pages }}</span>
</li>
{% nospaceless %}
{% block pagination.previous %}
<li class="previous">
{% if table.page.has_previous %}
<a class="btn" href="{% querystring table.prefixed_page_field=table.page.previous_page_number %}">{% trans "Previous" %}</a>
{% else %}
<a class="btn disabled" href="#">{% trans "Previous" %}</a>
{% endif %}
</li>
{% endblock pagination.previous %}
{% endnospaceless %}
{% nospaceless %}
{% block pagination.current %}
<li class="current">
{% with table.page.number as current and table.paginator.num_pages as total %}
<a class="btn disabled">{{ current }}</a>
{% endwith %}
</li>
{% endblock pagination.current %}
{% endnospaceless %}
{% nospaceless %}
{% block pagination.next %}
<li class="next">
{% if table.page.has_next %}
<a class="btn" href="{% querystring table.prefixed_page_field=table.page.next_page_number %}">{% trans "Next" %}</a>
{% else %}
<a class="btn disabled" href="#">{% trans "Next" %}</a>
{% endif %}
</li>
{% endblock pagination.next %}
{% endnospaceless %}
</ul>
{% endblock pagination %}
{% endwith %}
</div>
{% endif %}
{% endspaceless %}
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment