【问题标题】:How to apply Datatables plugin to a rendered django-tables2?如何将 Datatables 插件应用于渲染的 django-tables2?
【发布时间】:2019-01-19 20:03:36
【问题描述】:

将 Datatables 插件实现到 'Django-tables2' 渲染表的问题。

我是 Django 的初学者;我正在开发一个显示数据上传到数据库(postgresql)的应用程序,寻找通过普通 django CVB 呈现包含许多数据的表的高时间解决方案,我找到了 django-tables2。 我已经能够实现它并显示渲染表(对加载速度有很大的改进),此外,对于我所有的表,我已经实现了“Datatables”插件和一些聚合,但我还没有弄清楚如何让它们一起工作用 django-tables2 呈现的表与我所有其他数据表一样(不是由 django-tables2 呈现的);直到那一刻,唯一部分起作用的部分是 CSS,但响应式和 colReorder 不起作用。

我已尝试按照官方文档的说明直接在模板中指定“静态”文件路由,但它不起作用。

我正在使用的 HTML 模板继承自一个“基础”模板,该模板实现了所有 css/js 静态文件并覆盖了一个内容块,该内容块是呈现数据表的位置。

作为参考,我的代码是:

Views.py

class Data_t_zq70(tables.Table):
    class Meta:
        model = datos
        attrs = {
            'class': 'table table-sm text-center table-striped table-
bordered table-hover id=dataTable'}
        fields = ['Insp_Lot', 'Description', 'Date', 'Material',
        'Batch', 'Mean_Valuation', 'Mean', 'Lower_Limit', 'Target',
        'Upper_Limit', 'Delvry_Quantity']
per_page = 10

class zq_70(LoginRequiredMixin, SingleTableView):
    model = datos
    table_class = Data_t_zq70
    template_name = 'data_list_zq70.html'
    login_url = 'base:login'

HTML Template:

{% extends 'base/base.html' %}
{% load static %}
{% load django_tables2 %}
{% block contenido %}
<div class="panel panel-info">
    <div class="panel-heading">
        <a href="{% url 'data:data_upload' %}" class="btn btn-
               info"><span class="fa fa-plus-circle"> <strong>
                              Cargar datos</strong></span></a>
    </div>
    <div class="panel-body">
                    {% render_table table %}
    </div>
</div>
{% endblock contenido %}

尽管渲染了数据表,但“数据表”插件中的某些元素并未显示,例如“搜索”框、“显示 xx 条目”框等。

Other tables rendered with Datatables plugin

Table rendered with Django-tables2

【问题讨论】:

    标签: datatables-1.10 django-tables2


    【解决方案1】:

    与 django-tables2 提供的表格模板相比,datatables 需要不同的 html 元素。我认为实现自定义模板派生自the supplied templates 之一,添加数据表所需的元素是可行的方法。

    django-tables2 可能不是呈现数据表的最佳解决方案,您是否查看过django-datatables-view

    【讨论】:

      【解决方案2】:

      我知道django-datatables-view可以直接用于渲染表格,但是如果你真的想通过django-table2之上的插件来使用datatable的功能,那么你可以在@中写attrs 987654325@.

      from django_tables2 import tables
      from .models import MyModel
      
      class MyModelTable(tables.Table):
      
          class Meta:
              model = MyModel
      
              # This is a hack for using datatable at the frontend
              attrs = {
                  'id' : 'example',  #Rename the value to match with datatable id
                  'class': 'display', #Change this class value according to the documentation
                  'style': 'width:100%'
                  } 
      

      并将数据包特定的 css/js 包含到您的 html 模板中。

      同样,这绝对不是在 django 中使用数据表的理想方式,但玩得开心!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-20
        • 2017-01-31
        • 2013-08-03
        • 2018-10-28
        • 2019-02-19
        • 1970-01-01
        • 2017-11-09
        • 1970-01-01
        相关资源
        最近更新 更多