【问题标题】:not all my column in 'render_table' are orderable, what can i do?并非我在“render_table”中的所有列都是可订购的,我该怎么办?
【发布时间】:2021-10-14 21:41:30
【问题描述】:

我使用 django_tables2 中的 render_table 编写了一个包含 4 个标题(标题、删除、视图和导出)的表,标题是可排序的,但另一个不是,问题是 render_table 在所有元素中都使用可排序的类,我该如何编辑?

在 HTML 中我这样调用函数:

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

这是我的 table.py 脚本:

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")

(只有 ENTRIES_TEMPLATE 需要可订购)

【问题讨论】:

    标签: django django-tables2


    【解决方案1】:

    您可以为不想订购的列设置orderable=False

    # 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)
    

    有关更多信息,请参阅 django-tables2 docs on ordering

    【讨论】:

      猜你喜欢
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 2018-07-22
      • 1970-01-01
      • 2021-10-01
      • 2022-07-25
      相关资源
      最近更新 更多