【问题标题】:Adding rows breaks datatable in Django在 Django 中添加行会破坏数据表
【发布时间】:2017-06-16 16:05:50
【问题描述】:

我在 Django 中有一个数据表,在硬编码时可以正常工作,但是当我添加我的 Django 模板标签时它会中断。页面上的检查说:Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined in jquery.datatables.min.js

只有当我在表中有多个用户时才会发生这种情况,或者尝试使用 django 在表中添加一列。因为我将在我的项目中使用多个数据表,所以我需要弄清楚我做错了什么。我使用的数据表 JS 代码来自模板,我不确定错误是在模板代码中还是在我的 Django 模板代码中。所以请原谅长代码块。

employees.html(django 模板):

<div class="card-body collapse in">
                    <div class="card-block card-dashboard">
                        <button id="addRow" class="btn btn-primary mb-2 js-create-employee"><i class="ft-plus"></i>&nbsp; Add New Employee</button>
                        <table class="table table-striped table-bordered zero-configuration">
                            <thead>
                                <tr>
                                    <th>Name</th>
                                    <th>Username</th>
                                    <th>Roles</th>
                                    <th>Email</th>
                                    <th>Mobile Contact</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>

                                {% for profile in user_profile_list %}
                                    <tr>
                                    {% if not profile.user.is_superuser %}
                                        <td><a href="{% url 'dispatch:profile_detail' pk=profile.user_id %}">{{ profile.user.get_full_name }}</a></td>
                                        <td>{{ profile.user.username }}</td>
                                        <td>
                                            {% for g in profile.user.groups.all %}
                                               <div class="tag tag-default">{{ g.name|split:'_'|title }}</div>
                                            {% endfor %}
                                        </td>
                                        <td>{{ profile.user.email }}</td>
                                        <td>{{ profile.mobile_phone }}</td>
                                        <td><a href="#" alt="Edit"><i class="fa fa-pencil"></i></a>&nbsp;&nbsp;<a href="#" alt="Assign"><i class="fa fa-link"></i><a/>&nbsp;&nbsp;<a href="#" alt="delete"><i class="fa fa-times"></i><a/></td>

                                        {% endif %}
                                    </tr>
                                    {% endfor %}
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th>Name</th>
                                    <th>Username</th>
                                    <th>Roles</th>
                                    <th>Email</th>
                                    <th>Mobile Contact</th>
                                    <th>Actions</th>
                                </tr>
                            </tfoot>
                        </table>

数据表实例化:

$(document).ready(function() {

/****************************************
*       js of zero configuration        *
****************************************/

$('.zero-configuration').DataTable();
});

也使用了 Jquery.datatables.min.js 和 dataTables.bootstrap4.min.js,但它们来自 bootstrap 4。除非需要,否则我不会在此处添加它们,无论如何它们都会被缩小。

【问题讨论】:

    标签: django datatables django-templates


    【解决方案1】:

    仅当表格或标签的数据不可用时才会出现此问题。

    您在使用 django templatestag 的模板中有条件因此,表中每个 &lt;td&gt; 元素的数量是 &lt;tr&gt; 元素的子元素的数量与 &lt;th&gt; 元素的数量不匹配元素。 请确保&lt;tbody&gt;标签中的&lt;td&gt;标签数量相同。

    编辑:

    也许 {% if not profile.user.is_superuser %} 这个条件造成了这个问题。如果用户是超级用户,则不会创建与 &lt;thead&gt; 中相同数量的 &lt;th&gt; 不匹配的 &lt;td&gt; 标签

    【讨论】:

    • 我明白,但每个用户的数据都是相同的。那么为什么会出现问题呢?
    • 如果用户是超级用户,那么 {% if not profile.user.is_superuser %} 这种情况会导致该问题。 的数量不会与
    • 如果我只有一个额外的用户,超级用户代码就可以工作。当我向用户添加第二个非超级用户时,表格会中断
    • 您能否添加有问题的 html 输出代码以帮助理解表格的结构?
    • 删除了超级用户检查,它可以工作。但我不明白为什么它适用于一个非超级用户而不是超过一个。但是你的答案是正确的,我标记了它。谢谢!真的很感激!
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签