Search code examples
djangodjango-tables2

not all my column in 'render_table' are orderable, what can i do?


I use render_table from django_tables2 to write a table that contain 4 headers (Title, Delete, View, and Export), the title is orderable, but the other one are not, the problem is that render_table use class orderable in all elements, how can I edit that ?

in HTML I call the function like this :

    <!--  table -->
<div class="mt-3">
    {% render_table table %}
</div>

and this is my table.py script :

ENTRIES_TEMPLATE = "<a href='{% url 'form-entries' form=record.pk %}' class='btn btn-outline-info btn-small'><i class='fas fa-file'></i></a>"   
DELETE_TEMPLATE = "<a href='{% url 'dashboard-topic-delete' pk=record.pk %}' class='btn btn-outline-danger btn-small'><i class='fas fa-trash'></i></a>"
VIEW_TEMPLATE   = "<a href='{{record.pk}}' class='btn btn-outline-info btn-small'><i class='fas fa-edit'></i></a>"

EXPORT_TEMPLATE = """
<div class="btn-group">
    <button type="button" class="btn btn-outline-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Export
    </button>
    <div class="dropdown-menu">
        <a class="dropdown-item" href="{{record.get_export_url}}?type=CSV"><i class='fas fa-file-csv'></i> csv</a>
        <a class="dropdown-item" href="{{record.get_export_url}}?type=XLS"><i class='fas fa-file-excel'></i> xlsx</a>
    </div>
</div>
"""


# # id          = tables.LinkColumn('forms:form-update',kwargs={"pk":A("pk")})
responses     = tables.TemplateColumn(ENTRIES_TEMPLATE)
delete      = tables.TemplateColumn(DELETE_TEMPLATE)
view        = tables.TemplateColumn(VIEW_TEMPLATE)

export     = tables.TemplateColumn(EXPORT_TEMPLATE)



class Meta:
    model = Topic
    template_name = "django_tables2/bootstrap.html"
    fields = ("Title","view","responses","delete","export")

(Only ENTRIES_TEMPLATE need to be Orderable)


Solution

  • You can set orderable=False for columns that you don’t want to be orderable:

    # responses will be orderable
    responses = tables.TemplateColumn(ENTRIES_TEMPLATE)
    # the following three fields won't be orderable
    delete = tables.TemplateColumn(DELETE_TEMPLATE, orderable=False)
    view = tables.TemplateColumn(VIEW_TEMPLATE, orderable=False)
    export = tables.TemplateColumn(EXPORT_TEMPLATE, orderable=False)
    

    For more info see the django-tables2 docs on ordering.